Skip to content

Commit

Permalink
fix: cannot apply pure esm unified plugin (#1893)
Browse files Browse the repository at this point in the history
* refactor: improved plugin loading

* docs: update docs

* chore: update

* chore: update logic

* chore: update

* Revert "docs: update docs"

This reverts commit 406775f.

# Conflicts:
#	docs/guide/markdown.md

* chore: update
  • Loading branch information
Wxh16144 authored Sep 18, 2023
1 parent 2124b19 commit f4670fd
Showing 1 changed file with 12 additions and 14 deletions.
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] }));

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,
}),
);
});
}

// info available to all plugins
processor.data('fileAbsPath', opts.fileAbsPath);
Expand Down

0 comments on commit f4670fd

Please sign in to comment.