-
Notifications
You must be signed in to change notification settings - Fork 109
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
Any change to default highlighter breaks SvelteKit #514
Comments
Digging into source code a bit... MDsveX/packages/mdsvex/src/transformers/index.ts Lines 545 to 579 in 095d108
... I was able to work around the issue by doing: import escape from 'escape-html';
// escape curlies, backtick, \t, \r, \n to avoid breaking output of {@html `here`} in .svelte
const escape_svelty = (str) => str
.replace(
/[{}`]/g,
(c) => ({ '{': '{', '}': '}', '`': '`' }[c])
)
.replace(/\\([trn])/g, '\$1');
...
highlight: {
highlighter: function (code, lang) {
const html = escape_svelty(escape(code));
return `<pre>{@html \`<code>${html}</code>\`}</pre>`;
}
} ... probably with a bit more tweaking I can get this working for highlight.js too, but, am I going about this the wrong way? Or should MDsvex do this escaping automatically when a custom highlighter is provided? Would be happy to make a PR if that's the case. |
Further update — I have a workaround for highlight.js, which requires writing the import hljs from 'highlight.js';
...
mdsvex({
extensions: ['.md'],
highlight: {
highlighter: function (code, lang) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
const html = hljs.highlight(code, { language }).value;
return `<pre class="language-${lang}">{@html \`<code class="language-${lang}">${html}</code>\`}</pre>`;
}
}
}) |
Beginning from the template here:
https://github.com/josh-collinsworth/sveltekit-blog-starter
I'm finding that any change to the default highlighter ...
svelte.config.js
... will break the page entirely, with errors like these:
Originally I wanted to add highlight.js to my own app (not based on the blog starter above), and ran into these errors. But it's easier to reproduce using the recommended method to disable syntax highlighting, beginning from any SvelteKit+MDsveX template, and appears to be the same problem. Possibly the default syntax highlighter is escaping characters that these alternatives do not? Returning static text like "hello" from the function works fine.
Thanks!
The text was updated successfully, but these errors were encountered: