-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve code element node meta for rehype syntax highlighters (#5335)
- Loading branch information
Showing
7 changed files
with
71 additions
and
1 deletion.
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,5 @@ | ||
--- | ||
'@astrojs/mdx': patch | ||
--- | ||
|
||
Preserve code element node `data.meta` in `properties.metastring` for rehype syntax highlighters, like `rehype-pretty-code`` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { visit } from 'unist-util-visit'; | ||
|
||
/** | ||
* Moves `data.meta` to `properties.metastring` for the `code` element node | ||
* as `rehype-raw` strips `data` from all nodes, which may contain useful information. | ||
* e.g. ```js {1:3} => metastring: "{1:3}" | ||
*/ | ||
export default function rehypeMetaString() { | ||
return function (tree: any) { | ||
visit(tree, (node) => { | ||
if (node.type === 'element' && node.tagName === 'code' && node.data?.meta) { | ||
node.properties ??= {}; | ||
node.properties.metastring = node.data.meta; | ||
} | ||
}); | ||
}; | ||
} |
2 changes: 1 addition & 1 deletion
2
...ges/integrations/mdx/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Syntax highlighting | ||
|
||
```astro | ||
```astro {2} | ||
--- | ||
const handlesAstroSyntax = true | ||
--- | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.