Skip to content

Commit

Permalink
feat: refactor rehype-starry-night plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Apr 28, 2024
1 parent fae2aca commit 78d140a
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 27 deletions.
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
"homepage": "https://github.com/Microflash/rehype-starry-night#readme",
"author": "Naiyer Asif (https://www.naiyerasif.com)",
"type": "module",
"main": "src/index.js",
"main": "src/rehype-starry-night/index.js",
"exports": {
".": "./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",
".": "./src/rehype-starry-night/index.js",
"./header-caption-extension": "./src/rehype-starry-night/hast-util-starry-night-header-caption-extension.js",
"./header-language-extension": "./src/rehype-starry-night/hast-util-starry-night-header-language-extension.js",
"./remark-inline-code-lang": "./src/remark-inline-code-lang/index.js",
"./rehype-starry-night-inline": "./src/rehype-starry-night-inline/index.js"
"./rehype-starry-night-inline": "./src/rehype-starry-night-inline/index.js",
"./css": "./src/index.css"
},
"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/rehype-starry-night/index.js",
"src/rehype-starry-night/hast-util-starry-night-gutter.js",
"src/rehype-starry-night/hast-util-starry-night-header.js",
"src/rehype-starry-night/hast-util-starry-night-header-caption-extension.js",
"src/rehype-starry-night/hast-util-starry-night-header-language-extension.js",
"src/remark-inline-code-lang/index.js",
"src/rehype-starry-night-inline/index.js"
],
Expand All @@ -50,6 +50,7 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"cheerio": "1.0.0-rc.12",
"rehype-stringify": "^10.0.0",
"remark-directive": "^3.0.0",
"remark-parse": "^11.0.0",
Expand Down
107 changes: 107 additions & 0 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
min-height: 1em;
}

code[data-code-lang] {
code.hl-inline {
--hlBackgroundColorInline: var(--highlight-background-color-inline, hsl(208, 21%, 14%));

font-family: var(--highlight-font-mono);
Expand Down
Empty file removed src/rehype-starry-night/.gitkeep
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export default function starryNightHeaderCaptionExtension({ metadata }, children) {
const caption = metadata["caption"]
export default function starryNightHeaderCaptionExtension({ metadata, classNamePrefix }, children) {
const caption = metadata["caption"];
if (caption) {
children.push({
type: "element",
tagName: "div",
properties: { className: ["highlight-caption"] },
properties: { className: [`${classNamePrefix}-caption`] },
children: [
{
type: "text",
value: caption
}
]
})
});
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export default function starryNightHeaderLanguageExtension({ language }, children) {
export default function starryNightHeaderLanguageExtension({ language, classNamePrefix }, children) {
if (language) {
children.push({
type: "element",
tagName: "div",
properties: { className: ["highlight-language"] },
properties: { className: [`${classNamePrefix}-language`] },
children: [
{
type: "text",
value: language
}
]
})
});
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export default function starryNightHeader(headerOptions) {
const children = []
const extensions = headerOptions.extensions
const children = [];
const { extensions, classNamePrefix } = headerOptions;

extensions.forEach(extension => extension(headerOptions, children))
extensions.forEach(extension => extension(headerOptions, children));

return {
type: "element",
tagName: "div",
properties: { className: ["highlight-header"] },
properties: { className: [`${classNamePrefix}-header`] },
children
}
};
}
19 changes: 16 additions & 3 deletions src/index.js → src/rehype-starry-night/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defu from "defu";
import { createStarryNight, all } from "@wooorm/starry-night";
import { visit } from "unist-util-visit";
import { toString } from "hast-util-to-string";
Expand All @@ -9,6 +10,9 @@ import starryNightGutter, { search } from "./hast-util-starry-night-gutter.js";

const fenceparser = new FenceParser();
const prefix = "language-";
const defaults = {
classNamePrefix: "hl"
};

function extractMetadata(node) {
let metadata;
Expand All @@ -22,7 +26,15 @@ function extractMetadata(node) {
}

export default function rehypeStarryNight(userOptions = {}) {
const { aliases = {}, grammars = all, headerExtensions = [ starryNightHeaderLanguageExtension, starryNightHeaderCaptionExtension ] } = userOptions;
const {
aliases = {},
grammars = all,
headerExtensions = [
starryNightHeaderLanguageExtension,
starryNightHeaderCaptionExtension
],
classNamePrefix
} = defu(userOptions, defaults);
const starryNightPromise = createStarryNight(grammars);

return async function (tree) {
Expand Down Expand Up @@ -68,14 +80,15 @@ export default function rehypeStarryNight(userOptions = {}) {
id: btoa(Math.random()).replace(/=/g, "").substring(0, 12),
language: languageFragment,
metadata: metadata,
extensions: headerExtensions
extensions: headerExtensions,
classNamePrefix
};

parent.children.splice(index, 1, {
type: "element",
tagName: "div",
properties: {
className: ["highlight", "highlight-" + languageId]
className: [classNamePrefix, `${classNamePrefix}-${languageId}`]
},
children: [
starryNightHeader(headerOptions),
Expand Down
Loading

0 comments on commit 78d140a

Please sign in to comment.