-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codeblock language as title remark plugin (#4418)
* chore: update dependencies and add remark-codeblock-language-as-title plugin * feat: add README for languageAsTitleRemarkPlugin with features and usage details * fix: rename plugin to codeblockLanguageAsTitleRemarkPlugin for consistency
- Loading branch information
1 parent
8578e1b
commit a1babcf
Showing
7 changed files
with
94 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# codeblockLanguageAsTitleRemarkPlugin | ||
|
||
A custom [remark](https://github.com/remarkjs/remark) plugin that automatically sets the `title` metadata for code blocks in Markdown files based on the block's language. | ||
|
||
## Features | ||
|
||
- **Adds `title` Metadata**: For code blocks with a specified language (e.g., `js`, `python`), the plugin sets the `title` metadata to match the language. | ||
- **Preserves Existing Metadata**: If the code block already contains metadata (`meta`) but lacks a `title`, the plugin appends the `title` without removing the existing metadata. | ||
- **Ignores Code Blocks with Existing `title`**: If the `meta` field already includes a `title` property, the plugin does nothing to avoid overwriting it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@react-native-website/remark-codeblock-language-as-title", | ||
"version": "0.0.1", | ||
"private": true, | ||
"description": "Remark plugin for using codeblock language as title", | ||
"main": "src/index.js", | ||
"type": "module", | ||
"keywords": [ | ||
"remark", | ||
"react-native", | ||
"lint" | ||
], | ||
"files": [ | ||
"src/*" | ||
], | ||
"scripts": { | ||
"prettier": "prettier --write '{src}/**/*.{md,js,jsx,ts,tsx}'" | ||
}, | ||
"dependencies": { | ||
"unist-util-visit": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"remark": "^15.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export default function codeblockLanguageAsTitleRemarkPlugin() { | ||
/** | ||
* @param {import('mdast').Root} root - The root node of the Markdown AST | ||
* @returns {Promise<void>} | ||
*/ | ||
return async root => { | ||
const {visit} = await import('unist-util-visit'); | ||
visit(root, 'code', node => { | ||
if (node.lang) { | ||
if (node.meta) { | ||
if (node.meta.includes('title=')) { | ||
return; | ||
} | ||
node.meta = `title="${node.lang}" ${node.meta}`; | ||
} else { | ||
node.meta = `title="${node.lang}"`; | ||
} | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.