Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add injected scripts to markdown pages #2848

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-kiwis-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

add missing injected "page" scripts into markdown pages
32 changes: 23 additions & 9 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import type { Plugin } from 'vite';
import type { AstroConfig } from '../@types/astro';

import { transform } from '@astrojs/compiler';
import ancestor from 'common-ancestor-path';
import esbuild from 'esbuild';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { transform } from '@astrojs/compiler';
import type { Plugin } from 'vite';
import type { AstroConfig } from '../@types/astro';

interface AstroPluginOptions {
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 {
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 {
name: 'astro:markdown',
enforce: 'pre', // run transforms before other plugins can
Expand Down Expand Up @@ -50,12 +60,16 @@ ${setup}`.trim();
astroResult = `${prelude}\n${astroResult}`;
}

const filenameURL = new URL(`file://${id}`);
const pathname = filenameURL.pathname.substr(config.projectRoot.pathname.length - 1);
const filename = normalizeFilename(id);
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`
let { code: tsResult } = await transform(astroResult, {
pathname,
pathname: fileUrl.pathname.substr(config.projectRoot.pathname.length - 1),
projectRoot: config.projectRoot.toString(),
site: config.buildOptions.site,
sourcefile: id,
Expand Down