Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boo #630

Merged
merged 3 commits into from
Aug 15, 2024
Merged

boo #630

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-donkeys-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mdsvex": patch
---

Fix the builds
97 changes: 45 additions & 52 deletions packages/mdsvex/src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -488,46 +491,40 @@ interface Meta {
}

function load_language_metadata() {
if (!(process as RollupProcess).browser) {
const {
meta,
...languages
}: Record<string, PrismLanguage> &
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<string, PrismLanguage> & 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({
Expand All @@ -537,7 +534,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) {
Expand Down Expand Up @@ -581,27 +578,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 `<pre class="language-${normalised_lang}">{@html \`<code class="language-${normalised_lang}">${highlighted}</code>\`}</pre>`;
} else {
const highlighted = escape_svelty(escape(code));
return `<pre class="language-${normalised_lang}">{@html \`<code class="language-${normalised_lang}">${highlighted}</code>\`}</pre>`;
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 `<pre class="language-${normalised_lang}">{@html \`<code class="language-${normalised_lang}">${highlighted}</code>\`}</pre>`;
};
Loading