Skip to content
Merged
Prev Previous commit
Next Next commit
chore: dry
  • Loading branch information
avivkeller committed Oct 8, 2026
commit 0919affea12e92523e6c9e00562217001c90c0a5
41 changes: 23 additions & 18 deletions packages/core/src/plugins/shiki/highlighter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,11 @@ const highlighters = new Map();
* @returns {Promise<Array<T>>}
*/
const importList = async options => {
const imported = [];

for (const option of options) {
if (typeof option === 'string') {
imported.push(await importFromURL(option));
} else {
imported.push(option);
}
}
const imported = await Promise.all(
options.map(option =>
typeof option === 'string' ? importFromURL(option) : option
)
);

return imported.flat();
};
Expand Down Expand Up @@ -113,23 +109,32 @@ const importHighlighter = async ({
}) => {
engine ??= createEngine();

const [regexEngine, importedLangs, importedTransformers, importedThemes] =
await Promise.all([
engine,
importList(langs),
importList(transformers),
themes &&
Promise.all([
importTheme(themes.light, 'light'),
importTheme(themes.dark, 'dark'),
]),
]);

const coreOptions = {
engine: await engine,
langs: [...LANGS, ...(await importList(langs))],
engine: regexEngine,
langs: [...LANGS, ...importedLangs],
// A copy, as Shiki adds the aliases of the languages it bundles to it
langAlias: { ...langAlias },
};

const highlighterOptions = {
transformers: await importList(transformers),
};
const highlighterOptions = { transformers: importedTransformers };

// Without themes of its own, the highlighter has a default light and dark one
if (themes) {
const light = await importTheme(themes.light, 'light');
const dark = await importTheme(themes.dark, 'dark');
if (importedThemes) {
const [light, dark] = importedThemes;

coreOptions.themes = [light, dark];
coreOptions.themes = importedThemes;
highlighterOptions.themes = { light: light.name, dark: dark.name };
highlighterOptions.defaultColor = 'light';
}
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/utils/configuration/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import {
CONFIGURED_PLUGINS,
PLUGIN_LISTS,
} from '#utils/markdown/constants.mjs';
import {
resolveMarkdown,
resolveMarkdownPipeline,
} from '#utils/markdown/plugins.mjs';
import { resolveMarkdown } from '#utils/markdown/plugins.mjs';
import { deepMerge } from '#utils/misc.mjs';

import { DEFAULT_CHUNK_SIZE, DEFAULT_MAX_THREADS } from './constants.mjs';
Expand Down Expand Up @@ -157,7 +154,7 @@ export const loadConfigFile = async filePath => {
* @returns {Partial<import('./types').MarkdownConfiguration> | undefined}
*/
const configureMarkdown = (generator, markdown = {}, global) => {
const pipeline = resolveMarkdownPipeline(generator);
const pipeline = generator.markdown ?? {};
const renders = Boolean(pipeline.rehypePlugins || pipeline.recmaPlugins);
const configured = {};

Expand Down
46 changes: 30 additions & 16 deletions packages/core/src/utils/markdown/plugins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const resolveMarkdown = (markdown, label, filePath) => {
* @param {GeneratorMetadata} generator - The generator
* @returns {import('../configuration/types').MarkdownPipeline}
*/
export const resolveMarkdownPipeline = generator => {
const resolveMarkdownPipeline = generator => {
const module = getGeneratorModule(generator);

return resolveMarkdown(
Expand Down Expand Up @@ -112,19 +112,30 @@ const configureList = (own = [], configured = []) => {
const plugins = own.map(entry => ({ entry, added: false }));
const added = [];

// Each plugin by its specifier: the first listing of it takes the options
const listed = new Map();

for (const plugin of plugins) {
const [specifier] = enforceArray(plugin.entry);

if (!listed.has(specifier)) {
listed.set(specifier, plugin);
}
}

for (const entry of configured) {
const [specifier, options] = enforceArray(entry);
const plugin = listed.get(specifier);

const listed = [...plugins, ...added].find(
plugin => enforceArray(plugin.entry)[0] === specifier
);
if (!plugin) {
const addedPlugin = { entry, added: true };

if (!listed) {
added.push({ entry, added: true });
added.push(addedPlugin);
listed.set(specifier, addedPlugin);
} else if (options !== undefined) {
const [, listedOptions] = enforceArray(listed.entry);
const [, listedOptions] = enforceArray(plugin.entry);

listed.entry = [specifier, mergeOptions(listedOptions, options)];
plugin.entry = [specifier, mergeOptions(listedOptions, options)];
}
}

Expand Down Expand Up @@ -183,16 +194,19 @@ export const loadMarkdownPlugins = async (generator, markdown = {}) => {
const configured = {};
const own = {};

for (const list of PLUGIN_LISTS) {
const plugins = configureList(pipeline[list], markdown[list]);
// The lists import at once, so a slow plugin (Shiki) doesn't hold the rest
await Promise.all(
PLUGIN_LISTS.map(async list => {
const plugins = configureList(pipeline[list], markdown[list]);

const imported = await Promise.all(
plugins.map(({ entry }) => importPlugin(entry))
);
const imported = await Promise.all(
plugins.map(({ entry }) => importPlugin(entry))
);

configured[list] = imported;
own[list] = imported.filter((_, index) => !plugins[index].added);
}
configured[list] = imported;
own[list] = imported.filter((_, index) => !plugins[index].added);
})
);

loadedPipelines.set(generator.name, { generator, key, configured, own });
};
Expand Down
5 changes: 3 additions & 2 deletions packages/node-legacy/src/legacy-html/utils/buildContent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ export default (headNodes, metadataEntries) => {
})
);

const processedNodes = getProcessor('legacy-html').runSync(parsedNodes);
const processor = getProcessor('legacy-html');
const processedNodes = processor.runSync(parsedNodes);

// Stringifies the processed nodes to return the final Markdown content
return getProcessor('legacy-html').stringify(processedNodes);
return processor.stringify(processedNodes);
};
5 changes: 3 additions & 2 deletions packages/node-legacy/src/legacy-json/utils/buildSection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ export const createSectionBuilder = () => {
return;
}

const rendered = getProcessor('legacy-json').stringify(
getProcessor('legacy-json').runSync({ type: 'root', children: nodes })
const processor = getProcessor('legacy-json');
const rendered = processor.stringify(
processor.runSync({ type: 'root', children: nodes })
);

section.shortDesc = section.desc || undefined;
Expand Down
Loading