Skip to content

Commit

Permalink
chore(deps): update [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Jan 6, 2024
1 parent d74f3af commit f3b3cfc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 65 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ There are multiple ways to support light and dark themes. Here's one way to do t
@import "https://raw.githubusercontent.com/Microflash/rehype-starry-night/main/index.css";
```

> **Warning** URL imports for external styles is not recommended. You should either self-host them or bundle them, or copy-paste the entire CSS in one single file.
> [!WARNING]
> URL imports for external styles is not recommended. You should either self-host them, bundle them, or copy-paste the entire CSS in one single file.
## Examples

Expand Down
6 changes: 3 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.1.0",
"version": "3.2.0",
"description": "rehype plugin to highlight codeblocks with Starry Night ",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -39,12 +39,12 @@
},
"dependencies": {
"@microflash/fenceparser": "^2.6.2",
"@wooorm/starry-night": "^3.1.0",
"@wooorm/starry-night": "^3.2.0",
"hast-util-to-string": "^3.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"ava": "^6.0.0",
"ava": "^6.0.1",
"cheerio": "1.0.0-rc.12",
"rehype-stringify": "^10.0.0",
"remark-parse": "^11.0.0",
Expand Down
54 changes: 32 additions & 22 deletions pnpm-lock.yaml

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

78 changes: 39 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
import { createStarryNight, all } from "@wooorm/starry-night"
import { visit } from "unist-util-visit"
import { toString } from "hast-util-to-string"
import fenceparser from "@microflash/fenceparser"
import starryNightHeader from "./hast-util-starry-night-header.js"
import starryNightHeaderLanguageExtension from "./hast-util-starry-night-header-language-extension.js"
import starryNightHeaderCaptionExtension from "./hast-util-starry-night-header-caption-extension.js"
import starryNightGutter, { search } from "./hast-util-starry-night-gutter.js"
import { createStarryNight, all } from "@wooorm/starry-night";
import { visit } from "unist-util-visit";
import { toString } from "hast-util-to-string";
import fenceparser from "@microflash/fenceparser";
import starryNightHeader from "./hast-util-starry-night-header.js";
import starryNightHeaderLanguageExtension from "./hast-util-starry-night-header-language-extension.js";
import starryNightHeaderCaptionExtension from "./hast-util-starry-night-header-caption-extension.js";
import starryNightGutter, { search } from "./hast-util-starry-night-gutter.js";

const prefix = "language-"
const prefix = "language-";

function extractMetadata(node) {
let metadata
let metadata;

try {
const { meta } = node.data || {}
metadata = fenceparser(meta).metadata
} catch (e) {}
const { meta } = node.data || {};
metadata = fenceparser(meta).metadata;
} catch (e) { }

return metadata || {}
return metadata || {};
}

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

return async function (tree) {
const starryNight = await starryNightPromise
const starryNight = await starryNightPromise;

visit(tree, "element", (node, index, parent) => {
if (!parent || index === null || node.tagName !== "pre") {
return
return;
}

const head = node.children[0]
const head = node.children[0];

if (!head || head.type !== "element" || head.tagName !== "code" || !head.properties) {
return
return;
}

const classes = head.properties.className
const classes = head.properties.className;

if (!Array.isArray(classes)) return
if (!Array.isArray(classes)) return;

const language = classes.find((d) => typeof d === "string" && d.startsWith(prefix))
const language = classes.find((d) => typeof d === "string" && d.startsWith(prefix));

if (typeof language !== "string") return
if (typeof language !== "string") return;

const metadata = extractMetadata(head)
const metadata = extractMetadata(head);

const languageFragment = language.slice(prefix.length)
const languageId = aliases[languageFragment] || languageFragment || "txt"
const scope = starryNight.flagToScope(languageId)
const code = toString(head)
const languageFragment = language.slice(prefix.length);
const languageId = aliases[languageFragment] || languageFragment || "txt";
const scope = starryNight.flagToScope(languageId);
const code = toString(head);

let children
let children;

if (scope) {
const fragment = starryNight.highlight(code, scope)
children = fragment.children
const fragment = starryNight.highlight(code, scope);
children = fragment.children;
} else {
console.warn(`Grammar not found for ${languageId}; rendering the code without syntax highlighting`)
children = head.children
console.warn(`Grammar not found for ${languageId}; rendering the code without syntax highlighting`);
children = head.children;
}

const headerOptions = {
id: btoa(Math.random()).replace(/=/g,"").substring(0, 12),
id: btoa(Math.random()).replace(/=/g, "").substring(0, 12),
language: languageFragment,
metadata: metadata,
extensions: headerExtensions
}
};

parent.children.splice(index, 1, {
type: "element",
Expand All @@ -94,7 +94,7 @@ export default function rehypeStarryNight(userOptions = {}) {
]
}
]
})
})
}
});
});
};
}

0 comments on commit f3b3cfc

Please sign in to comment.