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 <matthew@skypack.dev>
This commit is contained in:
Happydev 2022-09-22 19:04:03 +00:00 committed by GitHub
parent 9fb03b2e99
commit 08dae16161
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)};