From 08dae161610c9c6b595002b385e8531d46a9128b Mon Sep 17 00:00:00 2001 From: Happydev <81974850+MoustaphaDev@users.noreply.github.com> Date: Thu, 22 Sep 2022 19:04:03 +0000 Subject: [PATCH] feat(vite-plugin-markdown): support importing markdown files from outside root project (#4780) * feat: support importing markdown from outside root project This change adds support for importing markdown located outside the root of directory of a project * refactor how the "jsx-runtime" module path is retrieved * refactor: split type imports and function import Pattern I've seen repeated a lot in the repo * fix: chaotic marge conflict mistake Co-authored-by: Matthew Phillips --- packages/astro/src/vite-plugin-markdown/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index e40fbf959..37cae5828 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -2,12 +2,14 @@ import { renderMarkdown } from '@astrojs/markdown-remark'; import fs from 'fs'; import matter from 'gray-matter'; import type { Plugin } from 'vite'; +import { normalizePath } from 'vite'; import type { AstroSettings } from '../@types/astro'; import { collectErrorMetadata } from '../core/errors.js'; import type { LogOptions } from '../core/logger/core.js'; import { warn } from '../core/logger/core.js'; import type { PluginMetadata } from '../vite-plugin-astro/types.js'; import { getFileInfo, safelyGetAstroData } from '../vite-plugin-utils/index.js'; +import { fileURLToPath } from 'node:url'; interface AstroPluginOptions { settings: AstroSettings; @@ -23,6 +25,11 @@ function safeMatter(source: string, id: string) { } } +// absolute path of "astro/jsx-runtime" +const astroJsxRuntimeModulePath = normalizePath( + fileURLToPath(new URL('../jsx-runtime/index.js', import.meta.url)) +); + export default function markdown({ settings, logging }: AstroPluginOptions): Plugin { return { enforce: 'pre', @@ -61,7 +68,7 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu } const code = escapeViteEnvReferences(` - import { Fragment, jsx as h } from 'astro/jsx-runtime'; + import { Fragment, jsx as h } from '${astroJsxRuntimeModulePath}'; ${layout ? `import Layout from ${JSON.stringify(layout)};` : ''} const html = ${JSON.stringify(html)};