From a1e49597b3792d819757fd60986ebac013d07c7f Mon Sep 17 00:00:00 2001 From: pngwn Date: Thu, 15 Aug 2024 15:55:46 +0100 Subject: [PATCH 1/3] boo --- packages/mdsvex/src/transformers/index.ts | 96 +++++++++++------------ 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/packages/mdsvex/src/transformers/index.ts b/packages/mdsvex/src/transformers/index.ts index 429938ea..54177fc9 100644 --- a/packages/mdsvex/src/transformers/index.ts +++ b/packages/mdsvex/src/transformers/index.ts @@ -488,46 +488,42 @@ interface Meta { } function load_language_metadata() { - if (!(process as RollupProcess).browser) { - const { - meta, - ...languages - }: Record & - Meta = require('prismjs/components.json').languages; - - for (const lang in languages) { - const [lang_info, aliases] = get_lang_info( - lang, - languages[lang], - meta.path - ); + const { + meta, + ...languages + }: Record & + Meta = require('prismjs/components.json').languages; + + for (const lang in languages) { + const [lang_info, aliases] = get_lang_info( + lang, + languages[lang], + meta.path + ); - langs[lang] = lang_info; - aliases.forEach((_n) => { - langs[_n] = langs[lang]; - }); - } + langs[lang] = lang_info; + aliases.forEach((_n) => { + langs[_n] = langs[lang]; + }); + } - const svelte_meta = { - name: 'svelte', - aliases: new Set(['sv']), - path: 'prism-svelte', - deps: new Set(['javscript', 'css']), - }; + const svelte_meta = { + name: 'svelte', + aliases: new Set(['sv']), + path: 'prism-svelte', + deps: new Set(['javscript', 'css']), + }; - langs.svelte = svelte_meta; - langs.sv = svelte_meta; - } + langs.svelte = svelte_meta; + langs.sv = svelte_meta; } function load_language(lang: string) { - if (!(process as RollupProcess).browser) { - if (!langs[lang]) return; + if (!langs[lang]) return; - langs[lang].deps.forEach((name) => load_language(name)); + langs[lang].deps.forEach((name) => load_language(name)); - require(langs[lang].path); - } + require(langs[lang].path); } export function highlight_blocks({ @@ -537,7 +533,7 @@ export function highlight_blocks({ highlighter?: Highlighter; alias?: { [x: string]: string }; } = {}): Transformer { - if (highlight_fn && !(process as RollupProcess).browser) { + if (highlight_fn) { load_language_metadata(); if (alias) { @@ -581,27 +577,23 @@ export const escape_svelty = (str: string): string => export const code_highlight: Highlighter = (code, lang) => { const normalised_lang = lang?.toLowerCase(); - if (!(process as RollupProcess).browser) { - let _lang = !!normalised_lang && langs[normalised_lang]; - if (!Prism) Prism = require('prismjs'); + let _lang = !!normalised_lang && langs[normalised_lang]; - if (_lang && !Prism.languages[_lang.name]) { - load_language(_lang.name); - } + if (!Prism) Prism = require('prismjs'); - if (!_lang && normalised_lang && Prism.languages[normalised_lang]) { - langs[normalised_lang] = { name: lang } as MdsvexLanguage; - _lang = langs[normalised_lang]; - } - const highlighted = escape_svelty( - _lang - ? Prism.highlight(code, Prism.languages[_lang.name], _lang.name) - : escape(code) - ); - return `
{@html \`${highlighted}\`}
`; - } else { - const highlighted = escape_svelty(escape(code)); - return `
{@html \`${highlighted}\`}
`; + if (_lang && !Prism.languages[_lang.name]) { + load_language(_lang.name); + } + + if (!_lang && normalised_lang && Prism.languages[normalised_lang]) { + langs[normalised_lang] = { name: lang } as MdsvexLanguage; + _lang = langs[normalised_lang]; } + const highlighted = escape_svelty( + _lang + ? Prism.highlight(code, Prism.languages[_lang.name], _lang.name) + : escape(code) + ); + return `
{@html \`${highlighted}\`}
`; }; From 37c26e066a65e04eb5ea3ccd525408e68f298051 Mon Sep 17 00:00:00 2001 From: pngwn Date: Thu, 15 Aug 2024 16:10:37 +0100 Subject: [PATCH 2/3] boo --- packages/mdsvex/src/transformers/index.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/mdsvex/src/transformers/index.ts b/packages/mdsvex/src/transformers/index.ts index 54177fc9..afa51bb2 100644 --- a/packages/mdsvex/src/transformers/index.ts +++ b/packages/mdsvex/src/transformers/index.ts @@ -2,6 +2,7 @@ import type { Transformer } from 'unified'; import type { Text, Code, HTML } from 'mdast'; import type { Element, Root } from 'hast'; import type { VFileMessage } from 'vfile-message'; +import { createRequire } from 'module'; import Message from 'vfile-message'; //@ts-ignore @@ -29,6 +30,8 @@ import type { LayoutMeta, } from '../types'; +const _require = import.meta.url ? createRequire(import.meta.url) : require; + // this needs a big old cleanup const newline = '\n'; @@ -488,11 +491,9 @@ interface Meta { } function load_language_metadata() { - const { - meta, - ...languages - }: Record & - Meta = require('prismjs/components.json').languages; + const { meta, ...languages }: Record & Meta = _require( + 'prismjs/components.json' + ).languages; for (const lang in languages) { const [lang_info, aliases] = get_lang_info( @@ -523,7 +524,7 @@ function load_language(lang: string) { langs[lang].deps.forEach((name) => load_language(name)); - require(langs[lang].path); + _require(langs[lang].path); } export function highlight_blocks({ @@ -580,7 +581,7 @@ export const code_highlight: Highlighter = (code, lang) => { let _lang = !!normalised_lang && langs[normalised_lang]; - if (!Prism) Prism = require('prismjs'); + if (!Prism) Prism = _require('prismjs'); if (_lang && !Prism.languages[_lang.name]) { load_language(_lang.name); From 03da5259ff114ba78547c58a0d74f9df1254bd1c Mon Sep 17 00:00:00 2001 From: pngwn Date: Thu, 15 Aug 2024 16:20:15 +0100 Subject: [PATCH 3/3] Create nine-donkeys-call.md --- .changeset/nine-donkeys-call.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nine-donkeys-call.md diff --git a/.changeset/nine-donkeys-call.md b/.changeset/nine-donkeys-call.md new file mode 100644 index 00000000..60ccbacf --- /dev/null +++ b/.changeset/nine-donkeys-call.md @@ -0,0 +1,5 @@ +--- +"mdsvex": patch +--- + +Fix the builds