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 1 commit
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
31 changes: 15 additions & 16 deletions src/loaders/markdown/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,21 @@ 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
const fn: Plugin = mod.default || mod;

opts.processor.use(fn, options);
let mod = typeof plugin === 'function' ? plugin : undefined;
PeachScript marked this conversation as resolved.
Show resolved Hide resolved
if (!mod && typeof plugin === 'string') {
mod = await import(plugin).then((module) => module.default);
}

opts.processor.use(mod as Plugin, options);
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
}

export default async (raw: string, opts: IMdTransformerOptions) => {
Expand Down Expand Up @@ -149,13 +149,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 +188,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