summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src/load-plugins.ts
blob: b1e72ae361f5df448511272c47f9b69cfbb89487 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import * as unified from 'unified';
import type { Plugin, UnifiedPluginImport } from './types';

async function importPlugin(p: string | UnifiedPluginImport): UnifiedPluginImport {
	if (typeof p === 'string') {
		return await import(p);
	}

	return await p;
}

export function loadPlugins(items: Plugin[]): Promise<[unified.Plugin] | [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]))
					.catch((e) => reject(e));
			}

			return importPlugin(p)
				.then((m) => resolve([m.default]))
				.catch((e) => reject(e));
		});
	});
}