-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
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
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,50 @@ | ||
import { visit, SKIP } from 'unist-util-visit' | ||
import { codeToHast } from 'shikiji' | ||
|
||
/** | ||
* @returns {import('unified').Transformer} | ||
*/ | ||
export function rehypeShikiji() { | ||
return async (tree) => { | ||
/** @type {typeof tree[]} */ | ||
const preNodes = [] | ||
|
||
// visit is sync only | ||
visit(tree, async (node) => { | ||
if (node.type === 'element' && node.tagName === 'pre') { | ||
preNodes.push(node) | ||
return SKIP | ||
} | ||
}) | ||
|
||
for (const node of preNodes) { | ||
const codeNode = node.children?.[0] | ||
|
||
const language = codeNode.properties?.className?.[0]?.replace( | ||
'language-', | ||
'' | ||
) | ||
if (!language) return | ||
|
||
const code = codeNode.children?.[0]?.value | ||
if (!code) return | ||
|
||
const codeHast = await codeToHast(code, { | ||
lang: language, | ||
themes: { | ||
light: 'light-plus', | ||
dark: 'dark-plus' | ||
} | ||
}) | ||
|
||
const preHast = codeHast.children[0] | ||
// too bright | ||
preHast.properties.style = preHast.properties.style.replace( | ||
'background-color:#FFFFFF', | ||
'background-color:#f3f0f0' | ||
) | ||
|
||
Object.assign(node, preHast) | ||
} | ||
} | ||
} |
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