-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: remark-code-import and remark-shiki-twoslash working
But not together! See shikijs/twoslash#125 (comment)
- Loading branch information
Showing
5 changed files
with
468 additions
and
4 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
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,40 @@ | ||
import {createLoader} from '@mdx-js/node-loader' | ||
import codeImport from 'remark-code-import' | ||
import remarkShikiTwoslash from 'remark-shiki-twoslash' | ||
|
||
// https://github.com/shikijs/twoslash/issues/125#issuecomment-1710838839 | ||
function remarkHtmlToJsx() { | ||
async function transform(...args: any[]) { | ||
// Async import since these packages are all in ESM | ||
const {visit, SKIP} = await import('unist-util-visit') | ||
const {mdxFromMarkdown} = await import('mdast-util-mdx') | ||
const {fromMarkdown} = await import('mdast-util-from-markdown') | ||
const {mdxjs} = await import('micromark-extension-mdxjs') | ||
|
||
// This is a horror show, but it's the only way I could get the raw HTML into MDX. | ||
const [ast] = args | ||
visit(ast, 'html', (node) => { | ||
const escapedHtml = JSON.stringify(node.value) | ||
const jsx = `<div dangerouslySetInnerHTML={{__html: ${escapedHtml} }}/>` | ||
const rawHtmlNode = fromMarkdown(jsx, { | ||
extensions: [mdxjs()], | ||
mdastExtensions: [mdxFromMarkdown()], | ||
}).children[0] | ||
|
||
Object.assign(node, rawHtmlNode) | ||
|
||
return SKIP | ||
}) | ||
} | ||
|
||
return transform | ||
} | ||
|
||
// This extra file is needed due to limitation of the `register` api in node | ||
export const {load} = createLoader({ | ||
remarkPlugins: [ | ||
[codeImport], | ||
[remarkShikiTwoslash.default], | ||
[remarkHtmlToJsx], | ||
], | ||
}) |
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.