Skip to content

Commit 0919aff

Browse files
committed
chore: dry
1 parent fc40dff commit 0919aff

5 files changed

Lines changed: 61 additions & 43 deletions

File tree

‎packages/core/src/plugins/shiki/highlighter.mjs‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,11 @@ const highlighters = new Map();
6363
* @returns {Promise<Array<T>>}
6464
*/
6565
const importList = async options => {
66-
const imported = [];
67-
68-
for (const option of options) {
69-
if (typeof option === 'string') {
70-
imported.push(await importFromURL(option));
71-
} else {
72-
imported.push(option);
73-
}
74-
}
66+
const imported = await Promise.all(
67+
options.map(option =>
68+
typeof option === 'string' ? importFromURL(option) : option
69+
)
70+
);
7571

7672
return imported.flat();
7773
};
@@ -113,23 +109,32 @@ const importHighlighter = async ({
113109
}) => {
114110
engine ??= createEngine();
115111

112+
const [regexEngine, importedLangs, importedTransformers, importedThemes] =
113+
await Promise.all([
114+
engine,
115+
importList(langs),
116+
importList(transformers),
117+
themes &&
118+
Promise.all([
119+
importTheme(themes.light, 'light'),
120+
importTheme(themes.dark, 'dark'),
121+
]),
122+
]);
123+
116124
const coreOptions = {
117-
engine: await engine,
118-
langs: [...LANGS, ...(await importList(langs))],
125+
engine: regexEngine,
126+
langs: [...LANGS, ...importedLangs],
119127
// A copy, as Shiki adds the aliases of the languages it bundles to it
120128
langAlias: { ...langAlias },
121129
};
122130

123-
const highlighterOptions = {
124-
transformers: await importList(transformers),
125-
};
131+
const highlighterOptions = { transformers: importedTransformers };
126132

127133
// Without themes of its own, the highlighter has a default light and dark one
128-
if (themes) {
129-
const light = await importTheme(themes.light, 'light');
130-
const dark = await importTheme(themes.dark, 'dark');
134+
if (importedThemes) {
135+
const [light, dark] = importedThemes;
131136

132-
coreOptions.themes = [light, dark];
137+
coreOptions.themes = importedThemes;
133138
highlighterOptions.themes = { light: light.name, dark: dark.name };
134139
highlighterOptions.defaultColor = 'light';
135140
}

‎packages/core/src/utils/configuration/index.mjs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import {
1919
CONFIGURED_PLUGINS,
2020
PLUGIN_LISTS,
2121
} from '#utils/markdown/constants.mjs';
22-
import {
23-
resolveMarkdown,
24-
resolveMarkdownPipeline,
25-
} from '#utils/markdown/plugins.mjs';
22+
import { resolveMarkdown } from '#utils/markdown/plugins.mjs';
2623
import { deepMerge } from '#utils/misc.mjs';
2724

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

‎packages/core/src/utils/markdown/plugins.mjs‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const resolveMarkdown = (markdown, label, filePath) => {
6363
* @param {GeneratorMetadata} generator - The generator
6464
* @returns {import('../configuration/types').MarkdownPipeline}
6565
*/
66-
export const resolveMarkdownPipeline = generator => {
66+
const resolveMarkdownPipeline = generator => {
6767
const module = getGeneratorModule(generator);
6868

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

115+
// Each plugin by its specifier: the first listing of it takes the options
116+
const listed = new Map();
117+
118+
for (const plugin of plugins) {
119+
const [specifier] = enforceArray(plugin.entry);
120+
121+
if (!listed.has(specifier)) {
122+
listed.set(specifier, plugin);
123+
}
124+
}
125+
115126
for (const entry of configured) {
116127
const [specifier, options] = enforceArray(entry);
128+
const plugin = listed.get(specifier);
117129

118-
const listed = [...plugins, ...added].find(
119-
plugin => enforceArray(plugin.entry)[0] === specifier
120-
);
130+
if (!plugin) {
131+
const addedPlugin = { entry, added: true };
121132

122-
if (!listed) {
123-
added.push({ entry, added: true });
133+
added.push(addedPlugin);
134+
listed.set(specifier, addedPlugin);
124135
} else if (options !== undefined) {
125-
const [, listedOptions] = enforceArray(listed.entry);
136+
const [, listedOptions] = enforceArray(plugin.entry);
126137

127-
listed.entry = [specifier, mergeOptions(listedOptions, options)];
138+
plugin.entry = [specifier, mergeOptions(listedOptions, options)];
128139
}
129140
}
130141

@@ -183,16 +194,19 @@ export const loadMarkdownPlugins = async (generator, markdown = {}) => {
183194
const configured = {};
184195
const own = {};
185196

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

189-
const imported = await Promise.all(
190-
plugins.map(({ entry }) => importPlugin(entry))
191-
);
202+
const imported = await Promise.all(
203+
plugins.map(({ entry }) => importPlugin(entry))
204+
);
192205

193-
configured[list] = imported;
194-
own[list] = imported.filter((_, index) => !plugins[index].added);
195-
}
206+
configured[list] = imported;
207+
own[list] = imported.filter((_, index) => !plugins[index].added);
208+
})
209+
);
196210

197211
loadedPipelines.set(generator.name, { generator, key, configured, own });
198212
};

‎packages/node-legacy/src/legacy-html/utils/buildContent.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ export default (headNodes, metadataEntries) => {
241241
})
242242
);
243243

244-
const processedNodes = getProcessor('legacy-html').runSync(parsedNodes);
244+
const processor = getProcessor('legacy-html');
245+
const processedNodes = processor.runSync(parsedNodes);
245246

246247
// Stringifies the processed nodes to return the final Markdown content
247-
return getProcessor('legacy-html').stringify(processedNodes);
248+
return processor.stringify(processedNodes);
248249
};

‎packages/node-legacy/src/legacy-json/utils/buildSection.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ export const createSectionBuilder = () => {
125125
return;
126126
}
127127

128-
const rendered = getProcessor('legacy-json').stringify(
129-
getProcessor('legacy-json').runSync({ type: 'root', children: nodes })
128+
const processor = getProcessor('legacy-json');
129+
const rendered = processor.stringify(
130+
processor.runSync({ type: 'root', children: nodes })
130131
);
131132

132133
section.shortDesc = section.desc || undefined;

0 commit comments

Comments
 (0)