highlightjs-svelte
highlightjs-svelte copied to clipboard
Inconsistent API with `highlight.js`
trafficstars
The highlight.js API for registering only the languages you need is:
import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
hljs.registerLanguage('javascript', javascript);
This makes it friendly for importing only the language you need in a syntax highlighting component.
For example (Svelte):
<script>
import hljs from 'highlight.js/lib/core';
export let language;
export let code;
const highlighterPromise = (async function () {
const highlighter = await import(`./languages/${language}.js`);
hljs.registerLanguage(language, highlighter);
return hljs.highlight;
})();
</script>
{#await highlighterPromise then highlight}
<code>{highlight(code)}</code>
{/await}
Please also export src/svelte.js:#hljsDefineSvelte as part of the highlightjs-svelte public API. This matches the highlight.js API for other languages to allow the above pattern.
@milkbump Would you be interested by providing a patch?
Ping @AlexxNB Would you have a clue to solve this? :pray:
Hi, I think all you would have to add at the end of index.js is:
export {hljsDefineSvelte as svelte};
Then it would be possible to do:
import hljs from 'highlight.js/lib/core';
import {svelte} from 'highlightjs-svelte';
hljs.registerLanguage('svelte', svelte);