-
Notifications
You must be signed in to change notification settings - Fork 108
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
[Question] Use async function for highlighter or how to use shiki for highlighting? #205
Comments
I have also tried disabling the built in highlighter and passing a custom rehype plugin: // https://mdsvex.com/docs#options
const visit = require("unist-util-visit");
const shiki = require("shiki");
function rehypeShiki() {
return async (tree) => {
const highlighter = await shiki.getHighlighter({
theme: "material-theme-palenight",
});
visit(tree, "element", (node) => {
if (
node?.tagName === "code" &&
node.properties.className[0].startsWith("language-")
) {
const lang = node.properties.className[0].split("-")[1];
const code = node.children[0].value;
const shikiOutput = highlighter.codeToHtml(code, lang);
node.type = "text";
node.value = shikiOutput;
}
});
return tree;
};
}
module.exports = {
extensions: [".svx", ".md"],
smartypants: {
dashes: "oldschool",
},
highlight: false,
remarkPlugins: [
[
require("remark-github"),
{
// Use your own repository
repository: "https://github.com/svelte-add/mdsvex.git",
},
],
require("remark-abbr"),
],
rehypePlugins: [
require("rehype-slug"),
[
require("rehype-autolink-headings"),
{
behavior: "wrap",
},
],
rehypeShiki,
],
}; |
Do you have the markdown snippet that goes along with this output, so I can test? I've been meaning to deal with the eager code block chomping, I'll take a look at this today. |
---
title: Testing shiki
---
# {title}
```python
import os
key = os.environ["SECRET_KEY"]
```
I am happy to test if it helps! |
@michael0liver Can you try It also correct an issue where markdown |
The async function support works but there seems to be something going on still. JavaScript code blocks failCode block
Output
Spaces getting removed from code blockCode block
Interestingly when I remove the language from the code block (
Here is my config: const shiki = require("shiki");
async function highlighter(code, lang) {
const highlighter = await shiki.getHighlighter({ theme: "github-dark" });
return highlighter.codeToHtml(code, lang || "text");
}
module.exports = {
extensions: [".svx", ".md"],
highlight: {
highlighter: highlighter,
}
}; BTW I am using Svelte Kit. |
Here's another example with a shell script:
See how the spaces between Also its quite common to use
It looks like this conflicts with MDsveX/Svelte template syntax and never loads for me. |
Okay, this is because the tags that svelte interprets as expressions aren't being escaped (because it is a custom highlight function). I'll try to handle this internally but in the meantime you can do something like this: const escape_svelty = (str) =>
str
.replace(
/[{}`]/g,
(c) => ({ '{': '{', '}': '}', '`': '`' }[c])
)
.replace(/\\([trn])/g, '\$1');
async function highlighter(code, lang) {
const highlighter = await shiki.getHighlighter({ theme: "github-dark" });
return escape_svelty(highlighter.codeToHtml(code, lang || "text"));
} I think this should work okay. Could you open a new issue for this? |
Thanks for the workaround. Looks like we need to use the import { codeToHtml, getSingletonHighlighter } from 'shiki';
const THEME = 'github-dark';
const WIDTH = '800px';
/**
* @param code {string} - code to highlight
* @param lang {string} - code language
* @returns {Promise<string>} - highlighted html
*/
export async function highlighter(code: string, lang: string) {
await getSingletonHighlighter({
langs: [lang],
themes: [THEME]
});
const html = await codeToHtml(code, {
lang,
theme: THEME
});
const wrappedHtml = `
<div style="width: ${WIDTH}; overflow-x: auto;">
${html}
</div>
`;
return `{@html \`<code>${wrappedHtml}</code>\`}`;
} I'm not sure about the |
Just a heads up I am using the latest sveltekit, shiki and MDsveX; I had to wrap my shiki highlighter in your escape_svelty function as I was getting pre-transform errors: |
This also worked for me as well, wrapping my highlight functions return in @html |
Hi,
I would like to highlight my codeblocks using
shiki
. It doesn't seem to support a synchronous API. I have tried using the following:as the value for the
highlight
option but it doesn't work.Thanks for this project 🚀 its great.
The text was updated successfully, but these errors were encountered: