Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't highlight code? #152

Open
SiddharthShyniben opened this issue Apr 25, 2022 · 3 comments
Open

Doesn't highlight code? #152

SiddharthShyniben opened this issue Apr 25, 2022 · 3 comments

Comments

@SiddharthShyniben
Copy link

I'm using remark-shiki-twoslash:

const rendered = await unified()
	.use(remarkParse)
	.use(remarkTwoslash, ({theme: 'dark-plus'}))
	.use(remarkGfm)
	.use(remarkRehype)

	.use(rehypeStringify)
	.process(`
\`\`\`ts
const msg = "this";
//    ^?
\`\`\`
`);

console.log(String(rendered));

For some reason, this only wraps the code in a pre>code block:

<pre><code class="language-ts">const msg = "this";
//    ^?
</code></pre>

Is this a bug or am I doing something completely wrong?

@orta
Copy link
Contributor

orta commented Apr 25, 2022

Hard to say, I don't know that API very well, but perhaps the order of your plugins is wrong - shiki twoslash will highlight code outside of ```ts twoslash codeblocks.

@SiddharthShyniben
Copy link
Author

I think I have outlined the problem with a ton of console.logs.
Since shiki uses async, remark finishes parsing before shiki gets to finish the async load call. The node is parsed to html, but too late.

@SiddharthShyniben
Copy link
Author

SiddharthShyniben commented Apr 25, 2022

Fixed it! 🎉

const highlighter = await createShikiHighlighter({theme: 'dark-plus'});

const rendered = await unified()
	.use(remarkParse)
	.use(remarkGfm)
	.use(remarkTwoslash)
	.use(() => tree => {
		visit(tree, 'code', node => {
			const code = `
const msg = 'Hello, world!';
//    ^?
`;
			const twoslash = runTwoSlash(code, 'ts', {})
			const html = renderCodeToHTML(twoslash.code, 'ts', {twoslash: true}, {}, highlighter, twoslash);

			node.value = html;
			node.type = 'html';
			node.children = [];
		})
	})
	.use(remarkRehype, {allowDangerousHtml: true})
	.use(rehypeStringify, {allowDangerousHtml: true})
	.process(`
\`\`\`ts
const msg = "this";
//    ^?
\`\`\`
`);

console.log(String(rendered));

It's a hardcoded example, yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants