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

fix: cannot apply pure esm unified plugin #1893

Merged
merged 7 commits into from
Sep 18, 2023
Merged
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
26 changes: 12 additions & 14 deletions src/loaders/markdown/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,17 @@ function keepSoftBreak(pkg: IApi['pkg']) {
return !semver.subset(ver, VERSION_2_DEPRECATE_SOFT_BREAKS);
}

function applyUnifiedPlugin(opts: {
async function applyUnifiedPlugin(opts: {
processor: Processor;
plugin: NonNullable<IMdTransformerOptions['extraRemarkPlugins']>[0];
cwd: IMdTransformerOptions['cwd'];
}) {
const [plugin, options] = Array.isArray(opts.plugin)
? opts.plugin
: [opts.plugin];
const mod =
typeof plugin === 'function'
? plugin
: require(require.resolve(plugin, { paths: [opts.cwd] }));
PeachScript marked this conversation as resolved.
Show resolved Hide resolved

let mod = typeof plugin === 'function' ? plugin : await import(plugin);

const fn: Plugin = mod.default || mod;

opts.processor.use(fn, options);
Expand Down Expand Up @@ -149,13 +148,13 @@ export default async (raw: string, opts: IMdTransformerOptions) => {
}

// apply extra remark plugins
opts.extraRemarkPlugins?.forEach((plugin) =>
applyUnifiedPlugin({
for (const plugin of opts.extraRemarkPlugins ?? []) {
await applyUnifiedPlugin({
plugin,
processor,
cwd: opts.cwd,
}),
);
});
}

// apply internal rehype plugins
processor
Expand Down Expand Up @@ -188,14 +187,13 @@ export default async (raw: string, opts: IMdTransformerOptions) => {
// collect all texts for content search, must be the last rehype plugin
.use(rehypeText);

// apply extra rehype plugins
opts.extraRehypePlugins?.forEach((plugin) =>
applyUnifiedPlugin({
for (const plugin of opts.extraRehypePlugins ?? []) {
await applyUnifiedPlugin({
plugin,
processor,
cwd: opts.cwd,
}),
);
});
}

const result = await processor.use(rehypeJsxify).process(raw);

Expand Down
Loading