Skip to content

Commit

Permalink
feat: support highlighting inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Jan 7, 2024
1 parent f3b3cfc commit 3ee12be
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 34 deletions.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microflash/rehype-starry-night",
"version": "3.2.0",
"version": "3.5.0",
"description": "rehype plugin to highlight codeblocks with Starry Night ",
"license": "MIT",
"keywords": [
Expand All @@ -24,15 +24,19 @@
".": "./src/index.js",
"./css": "./src/index.css",
"./header-caption-extension": "./src/hast-util-starry-night-header-caption-extension.js",
"./header-language-extension": "./src/hast-util-starry-night-header-language-extension.js"
"./header-language-extension": "./src/hast-util-starry-night-header-language-extension.js",
"./remark-code-directive": "./src/remark-code-directive.js",
"./rehype-starry-night-inline": "./src/rehype-starry-night-inline.js"
},
"files": [
"src/index.js",
"src/hast-util-starry-night-gutter.js",
"src/hast-util-starry-night-header.js",
"src/hast-util-starry-night-header-caption-extension.js",
"src/hast-util-starry-night-header-language-extension.js",
"src/index.css"
"src/index.css",
"src/remark-code-directive.js",
"src/rehype-starry-night-inline.js"
],
"scripts": {
"test": "ava"
Expand All @@ -41,12 +45,14 @@
"@microflash/fenceparser": "^2.6.2",
"@wooorm/starry-night": "^3.2.0",
"hast-util-to-string": "^3.0.0",
"hastscript": "^8.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"ava": "^6.0.1",
"cheerio": "1.0.0-rc.12",
"rehype-stringify": "^10.0.0",
"remark-directive": "^3.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.0.0",
"unified": "^11.0.4"
Expand Down
119 changes: 110 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/rehype-starry-night-inline.js
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;
}
});
};
}
24 changes: 24 additions & 0 deletions src/remark-code-directive.js
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;
}
});
};
}
6 changes: 6 additions & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,11 @@ output: `<div class="highlight highlight-nux"><div class="highlight-header"><div
`,
output: `<div class="highlight highlight-html"><div class="highlight-header"><button class="highlight-copy">Copy to clipboard</button></div><pre><code tabindex="0"><span class="line">&lt;<span class="pl-ent">mark</span>&gt;highlighted&lt;/<span class="pl-ent">mark</span>&gt;</span>
</code></pre></div>`
},
{
title: "inline code",
input: `You can print to console like this: :code[console.log("Hello, world!");]{syntax=js}. This prints \`Hello, world!\``,
output: `<p>You can print to console like this: <code data-code-lang="js"><span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">"</span>Hello, world!<span class="pl-pds">"</span></span>);</code>. This prints <code>Hello, world!</code></p>`,
inline: true
}
]
Loading

0 comments on commit 3ee12be

Please sign in to comment.