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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import local plugins into markdown #2534

Merged
merged 9 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
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
Next Next commit
Replaced "UnifiedPluginImport" for a function
  • Loading branch information
JuanM04 committed Feb 3, 2022
commit 55efcddb44976850a65c869a97ec07ee5f0dd0ff
13 changes: 7 additions & 6 deletions packages/markdown/remark/src/load-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import * as unified from 'unified';
import type { Plugin, UnifiedPluginImport } from './types';

async function importPlugin(p: string | UnifiedPluginImport): UnifiedPluginImport {
async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin> {
if (typeof p === 'string') {
return await import(p);
const importResult = await import(p);
return importResult.default;
}

return await p;
return p;
}

export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin] | [unified.Plugin, any]>[] {
export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin, any?]>[] {
return items.map((p) => {
return new Promise((resolve, reject) => {
if (Array.isArray(p)) {
const [plugin, opts] = p;
return importPlugin(plugin)
.then((m) => resolve([m.default, opts]))
.then((m) => resolve([m, opts]))
.catch((e) => reject(e));
}

return importPlugin(p)
.then((m) => resolve([m.default]))
.then((m) => resolve([m]))
.catch((e) => reject(e));
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as unified from 'unified';
import type * as shiki from 'shiki';

export type UnifiedPluginImport = Promise<{ default: unified.Plugin }>;
export type Plugin = string | [string, any] | UnifiedPluginImport | [UnifiedPluginImport, any];
export type Plugin = string | [string, any] | unified.Plugin | [unified.Plugin, any];

export interface AstroMarkdownOptions {
mode?: 'md' | 'mdx';
Expand Down