highlightjs-svelte icon indicating copy to clipboard operation
highlightjs-svelte copied to clipboard

Inconsistent API with `highlight.js`

Open milkbump opened this issue 4 years ago • 3 comments
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 avatar Jun 09 '21 06:06 milkbump

@milkbump Would you be interested by providing a patch?

azmeuk avatar Jul 20 '21 20:07 azmeuk

Ping @AlexxNB Would you have a clue to solve this? :pray:

azmeuk avatar Apr 13 '22 13:04 azmeuk

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);

dbushell avatar Feb 07 '24 09:02 dbushell