aboutsummaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src/import-plugin-default.ts
diff options
context:
space:
mode:
authorGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
committerGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
commite586d7d704d475afe3373a1de6ae20d504f79d6d (patch)
tree7e3fa24807cebd48a86bd40f866d792181191ee9 /packages/markdown/remark/src/import-plugin-default.ts
downloadastro-latest.tar.gz
astro-latest.tar.zst
astro-latest.zip
Sync from a8e1c0a7402940e0fc5beef669522b315052df1blatest
Diffstat (limited to 'packages/markdown/remark/src/import-plugin-default.ts')
-rw-r--r--packages/markdown/remark/src/import-plugin-default.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/packages/markdown/remark/src/import-plugin-default.ts b/packages/markdown/remark/src/import-plugin-default.ts
new file mode 100644
index 000000000..2b898aa93
--- /dev/null
+++ b/packages/markdown/remark/src/import-plugin-default.ts
@@ -0,0 +1,22 @@
+import path from 'node:path';
+import { pathToFileURL } from 'node:url';
+// This file should be imported as `#import-plugin`
+import { resolve as importMetaResolve } from 'import-meta-resolve';
+import type * as unified from 'unified';
+
+let cwdUrlStr: string | undefined;
+
+// In non-browser environments, we can try to resolve from the filesystem too
+export async function importPlugin(p: string): Promise<unified.Plugin> {
+ // Try import from this package first
+ try {
+ const importResult = await import(/* @vite-ignore */ p);
+ return importResult.default;
+ } catch {}
+
+ // Try import from user project
+ cwdUrlStr ??= pathToFileURL(path.join(process.cwd(), 'package.json')).toString();
+ const resolved = importMetaResolve(p, cwdUrlStr);
+ const importResult = await import(/* @vite-ignore */ resolved);
+ return importResult.default;
+}