AlexxNB / highlightjs-svelte

Svelte Language Definition for Highlight.js
MIT License
16 stars 3 forks source link

Inconsistent API with `highlight.js` #7

Open milkbump opened 3 years ago

milkbump commented 3 years ago

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.

azmeuk commented 3 years ago

@milkbump Would you be interested by providing a patch?

azmeuk commented 2 years ago

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

dbushell commented 5 months ago

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