svelte-multiselect
Version:
Svelte multi-select component
27 lines (26 loc) • 1.49 kB
JavaScript
import { escape_html_text, hast_to_html } from './hast.js';
const optional_peer_error = `svelte-multiselect/live-examples requires optional peer dependency @wooorm/starry-night`;
// Starry-night highlighter for mdsvex
async function create_starry_night() {
try {
const [{ common, createStarryNight }, { default: source_svelte }] = await Promise.all([import(`@wooorm/starry-night`), import(`@wooorm/starry-night/source.svelte`)]);
return await createStarryNight([...common, source_svelte]);
}
catch (cause) {
throw new Error(optional_peer_error, { cause });
}
}
// Escape characters that would be interpreted as Svelte template syntax
const escape_svelte = (html) => html.replaceAll(`{`, `{`).replaceAll(`}`, `}`);
// Shared starry-night instance (grammars loaded once at build time)
// Uses common bundle (34 grammars) + Svelte
export const starry_night = await create_starry_night();
// mdsvex highlighter function
export function starry_night_highlighter(code, lang) {
const lang_key = lang?.toLowerCase();
const scope = lang_key ? starry_night.flagToScope(lang_key) : undefined;
// fall back to plain escaped code when the language is missing or unsupported
const html = escape_svelte(scope ? hast_to_html(starry_night.highlight(code, scope)) : escape_html_text(code));
const class_name = scope ? `highlight highlight-${lang_key}` : `highlight`;
return `<pre class="${class_name}"><code>${html}</code></pre>`;
}