2021-09-03 17:42:20 +00:00
|
|
|
import * as unified from 'unified';
|
2022-02-04 18:49:50 +00:00
|
|
|
import type { Plugin } from './types';
|
2021-07-01 16:55:22 +00:00
|
|
|
|
2022-02-04 18:49:50 +00:00
|
|
|
async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin> {
|
2021-07-01 16:55:22 +00:00
|
|
|
if (typeof p === 'string') {
|
2022-02-04 18:49:50 +00:00
|
|
|
const importResult = await import(p);
|
|
|
|
return importResult.default;
|
2021-07-01 16:55:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:49:50 +00:00
|
|
|
return p;
|
2021-07-01 16:55:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:49:50 +00:00
|
|
|
export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin, any?]>[] {
|
2021-07-01 16:55:22 +00:00
|
|
|
return items.map((p) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (Array.isArray(p)) {
|
|
|
|
const [plugin, opts] = p;
|
|
|
|
return importPlugin(plugin)
|
2022-02-04 18:49:50 +00:00
|
|
|
.then((m) => resolve([m, opts]))
|
2021-07-01 16:55:22 +00:00
|
|
|
.catch((e) => reject(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
return importPlugin(p)
|
2022-02-04 18:49:50 +00:00
|
|
|
.then((m) => resolve([m]))
|
2021-07-01 16:55:22 +00:00
|
|
|
.catch((e) => reject(e));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|