-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support highlighting inline code
- Loading branch information
1 parent
f3b3cfc
commit 3ee12be
Showing
6 changed files
with
209 additions
and
34 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 @@ | ||
import { createStarryNight, all } from "@wooorm/starry-night"; | ||
import { visit } from "unist-util-visit"; | ||
import { toString } from "hast-util-to-string"; | ||
|
||
export default function rehypeStarryNightInline(userOptions = {}) { | ||
const { aliases = {}, grammars = all } = userOptions; | ||
const starryNightPromise = createStarryNight(grammars); | ||
|
||
return async function (tree) { | ||
const starryNight = await starryNightPromise; | ||
|
||
visit(tree, "element", (node, index, parent) => { | ||
const annotatedInlineCode = node.tagName === "code" && ("dataCodeLang" in node.properties); | ||
|
||
if (!parent || index === null || node.tagName !== "code" || !annotatedInlineCode) { | ||
return; | ||
} | ||
|
||
const language = node.properties["dataCodeLang"]; | ||
const languageId = aliases[language] || language; | ||
const scope = starryNight.flagToScope(languageId); | ||
|
||
if (scope) { | ||
const fragment = starryNight.highlight(toString(node), scope); | ||
node.children = fragment.children; | ||
} | ||
}); | ||
}; | ||
} |
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,24 @@ | ||
import { visit } from "unist-util-visit"; | ||
import { h } from "hastscript"; | ||
|
||
export default function remarkCodeDirective() { | ||
return (tree) => { | ||
visit(tree, (node) => { | ||
if ( | ||
node.type === "textDirective" || | ||
node.type === "leafDirective" | ||
) { | ||
if (!node.attributes["syntax"]) { | ||
return false; | ||
} | ||
|
||
const { syntax, ...attribs } = node.attributes; | ||
const data = node.data || (node.data = {}); | ||
|
||
const tagName = "code"; | ||
data.hName = tagName; | ||
data.hProperties = h(tagName, { "data-code-lang": syntax, ...attribs }).properties; | ||
} | ||
}); | ||
}; | ||
} |
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.