add injected scripts to markdown pages (#2848)
* add injected scripts to markdown pages * Create twenty-kiwis-tease.md Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
parent
3b621f7a61
commit
981e2a839b
2 changed files with 28 additions and 9 deletions
5
.changeset/twenty-kiwis-tease.md
Normal file
5
.changeset/twenty-kiwis-tease.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"astro": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
add missing injected "page" scripts into markdown pages
|
|
@ -1,17 +1,27 @@
|
||||||
import type { Plugin } from 'vite';
|
import { transform } from '@astrojs/compiler';
|
||||||
import type { AstroConfig } from '../@types/astro';
|
import ancestor from 'common-ancestor-path';
|
||||||
|
|
||||||
import esbuild from 'esbuild';
|
import esbuild from 'esbuild';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { fileURLToPath } from 'url';
|
import type { Plugin } from 'vite';
|
||||||
import { transform } from '@astrojs/compiler';
|
import type { AstroConfig } from '../@types/astro';
|
||||||
|
|
||||||
interface AstroPluginOptions {
|
interface AstroPluginOptions {
|
||||||
config: AstroConfig;
|
config: AstroConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Transform .astro files for Vite */
|
// TODO: Clean up some of the shared logic between this Markdown plugin and the Astro plugin.
|
||||||
|
// Both end up connecting a `load()` hook to the Astro compiler, and share some copy-paste
|
||||||
|
// logic in how that is done.
|
||||||
export default function markdown({ config }: AstroPluginOptions): Plugin {
|
export default function markdown({ config }: AstroPluginOptions): Plugin {
|
||||||
|
function normalizeFilename(filename: string) {
|
||||||
|
if (filename.startsWith('/@fs')) {
|
||||||
|
filename = filename.slice('/@fs'.length);
|
||||||
|
} else if (filename.startsWith('/') && !ancestor(filename, config.projectRoot.pathname)) {
|
||||||
|
filename = new URL('.' + filename, config.projectRoot).pathname;
|
||||||
|
}
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'astro:markdown',
|
name: 'astro:markdown',
|
||||||
enforce: 'pre', // run transforms before other plugins can
|
enforce: 'pre', // run transforms before other plugins can
|
||||||
|
@ -50,12 +60,16 @@ ${setup}`.trim();
|
||||||
astroResult = `${prelude}\n${astroResult}`;
|
astroResult = `${prelude}\n${astroResult}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filenameURL = new URL(`file://${id}`);
|
const filename = normalizeFilename(id);
|
||||||
const pathname = filenameURL.pathname.substr(config.projectRoot.pathname.length - 1);
|
const fileUrl = new URL(`file://${filename}`);
|
||||||
|
const isPage = filename.startsWith(config.pages.pathname);
|
||||||
|
if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) {
|
||||||
|
source += `\n<script hoist src="astro:scripts/page.js" />`;
|
||||||
|
}
|
||||||
|
|
||||||
// Transform from `.astro` to valid `.ts`
|
// Transform from `.astro` to valid `.ts`
|
||||||
let { code: tsResult } = await transform(astroResult, {
|
let { code: tsResult } = await transform(astroResult, {
|
||||||
pathname,
|
pathname: fileUrl.pathname.substr(config.projectRoot.pathname.length - 1),
|
||||||
projectRoot: config.projectRoot.toString(),
|
projectRoot: config.projectRoot.toString(),
|
||||||
site: config.buildOptions.site,
|
site: config.buildOptions.site,
|
||||||
sourcefile: id,
|
sourcefile: id,
|
||||||
|
|
Loading…
Reference in a new issue