Skip to content

Commit f6dd62e

Browse files
morinokamiclaude
andcommitted
fix(pages): register islands that only Markdown pages import
scriptBlocks() passed a whole Markdown page to the parser as JavaScript. Parsing failed at the leading <script> block and the error was dropped, so no island imports were found. An island that only a Markdown page imported was missing from the islands client manifest, and the page shipped no islands script: it rendered on the server but never hydrated. Read only the leading <script> block of a Markdown page, as @void/md does. The page scanner's copy of that rule moves to island-imports.ts so both use the same one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J8F8maQyB5pkE9PF6GPUae
1 parent d95b95f commit f6dd62e

5 files changed

Lines changed: 143 additions & 8 deletions

File tree

‎packages/react/test/integration/md-layout-frontmatter.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,50 @@ export default function Title() {
107107
rmSync(root, { recursive: true, force: true });
108108
}
109109
});
110+
111+
it('an island that only the Markdown page imports hydrates', async () => {
112+
const root = mkdtempSync(join(import.meta.dirname, '__tmp_md_page_island_'));
113+
mkdirSync(join(root, 'pages'));
114+
mkdirSync(join(root, 'components'));
115+
writeFileSync(
116+
join(root, 'components', 'Title.tsx'),
117+
`import { createElement } from "react";
118+
import { useFrontmatter } from "@void/md";
119+
export default function Title() {
120+
return createElement("p", { id: "island-fm" }, String(useFrontmatter().title));
121+
}`,
122+
);
123+
writeFileSync(
124+
join(root, 'pages', 'index.md'),
125+
`<script>
126+
import Title from "../components/Title" with { island: "load" };
127+
</script>
128+
129+
---
130+
title: Hello from frontmatter
131+
---
132+
133+
<Title />
134+
`,
135+
);
136+
137+
let visit: IslandPageVisit | undefined;
138+
try {
139+
visit = await openIslandPage(root, {
140+
alias: { '@void/react': join(import.meta.dirname, '../../src/runtime/index.ts') },
141+
plugins: [
142+
...voidMarkdown(),
143+
{ name: 'void-react:pages' },
144+
...islandsPlugin(join(root, 'pages')),
145+
],
146+
});
147+
expect(visit.html).toContain(ISLAND_FRONTMATTER_SCRIPT);
148+
await waitForIslandsHydrated(visit.page);
149+
expect(await visit.page.textContent('#island-fm')).toBe('Hello from frontmatter');
150+
expect(visit.diagnostics().toLowerCase()).not.toContain('hydrat');
151+
expect(visit.diagnostics()).not.toContain('pageerror');
152+
} finally {
153+
await visit?.close();
154+
rmSync(root, { recursive: true, force: true });
155+
}
156+
});

‎packages/void/src/pages/island-imports.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,25 @@ export type StaticCssImport = { statement: string; specifier: string };
1717

1818
type ScriptBlock = { source: string; offset: number; filename: string };
1919

20-
/** The whole JS/TS module, or each `<script>` block of a Vue/Svelte SFC with its offset in the file. */
20+
// Same leading-script rule as @void/md's extractScript: a markdown page may open
21+
// with a <script> block, and its frontmatter starts right after that block.
22+
export const MARKDOWN_SCRIPT_RE: RegExp = /^<script\b[^>]*>([\s\S]*?)<\/script>\s*\n?/;
23+
24+
/**
25+
* The whole JS/TS module, each `<script>` block of a Vue/Svelte SFC, or the
26+
* leading `<script>` block of a Markdown page, with its offset in the file.
27+
*/
2128
export function scriptBlocks(code: string, filename: string): Array<ScriptBlock> {
29+
if (filename.endsWith('.md')) {
30+
const match = MARKDOWN_SCRIPT_RE.exec(code);
31+
if (!match) {
32+
return [];
33+
}
34+
const source = match[1]!;
35+
return [
36+
{ source, offset: match[0].indexOf(source), filename: filename.replace(/\.md$/, '.ts') },
37+
];
38+
}
2239
if (!/\.(?:vue|svelte)$/.test(filename)) {
2340
return [{ source: code, offset: 0, filename }];
2441
}
@@ -86,7 +103,7 @@ export function editIslandSource(
86103
return { code: ms.toString(), map: ms.generateMap({ hires: true }) };
87104
}
88105

89-
/** Read island import attributes from JS/TS, or script blocks inside an SFC. */
106+
/** Read island import attributes from JS/TS, or the script blocks of an SFC or a Markdown page. */
90107
export function findIslandImports(code: string, filename: string): Array<IslandImportSource> {
91108
if (!code.includes('import') || !code.includes('island')) {
92109
return [];

‎packages/void/src/pages/scan.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { ROUTE_EXTENSIONS, ROUTE_EXTENSIONS_SET } from '../shared/route-extensions.ts';
2424
import { deriveFromSource } from '../shared/source-analysis-cache.ts';
2525
import { staticExportInitializers, staticLiteral } from '../shared/static-exports.ts';
26-
import { scriptBlocks } from './island-imports.ts';
26+
import { MARKDOWN_SCRIPT_RE, scriptBlocks } from './island-imports.ts';
2727
import { resolveNamedLayout } from './layout-resolution.ts';
2828
import { patternToRegex } from './manifest.ts';
2929
import type {
@@ -551,13 +551,9 @@ function warnShadowedPages(pagesDir: string, pages: Array<PageDefinition>): void
551551
}
552552
}
553553

554-
// Same leading-script rule as @void/md's extractScript: a markdown page may open
555-
// with a <script> block, and its frontmatter starts right after that block.
556-
const LEADING_SCRIPT_RE = /^<script\b[^>]*>[\s\S]*?<\/script>\s*\n?/;
557-
558554
/** Return the raw YAML frontmatter of markdown source, after any leading <script> block. */
559555
function markdownFrontmatter(source: string): string | undefined {
560-
const body = source.replace(LEADING_SCRIPT_RE, '');
556+
const body = source.replace(MARKDOWN_SCRIPT_RE, '');
561557
return body.match(/^---\r?\n([\s\S]*?)\r?\n---/)?.[1];
562558
}
563559

‎packages/void/test/unit/island-imports.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,31 @@ import Actual from './Actual' with { island: 'visible' };`;
2828
{ importName: 'Counter', specifier: './Counter', hydrate: 'load' },
2929
]);
3030
});
31+
32+
it('finds imports only in the leading script block of a Markdown page', () => {
33+
const code = `<script>
34+
import Badge from '../components/Badge' with { island: 'load' };
35+
</script>
36+
37+
\`\`\`ts
38+
import Fenced from './Fenced' with { island: 'load' };
39+
\`\`\`
40+
41+
<script>import Later from './Later' with { island: 'load' };</script>
42+
43+
<Badge />
44+
`;
45+
const imports = findIslandImports(code, 'page.md');
46+
expect(imports).toMatchObject([
47+
{ importName: 'Badge', specifier: '../components/Badge', hydrate: 'load' },
48+
]);
49+
expect(code.slice(imports[0]!.start, imports[0]!.end)).toBe(
50+
"import Badge from '../components/Badge' with { island: 'load' };",
51+
);
52+
});
53+
54+
it('treats a Markdown page without a leading script block as text', () => {
55+
const code = "import Counter from './Counter' with { island: 'load' };\n";
56+
expect(findIslandImports(code, 'page.md')).toEqual([]);
57+
});
3158
});

‎packages/vue/test/integration/md-island-frontmatter.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,51 @@ const frontmatter = useFrontmatter();
6060
rmSync(root, { recursive: true, force: true });
6161
}
6262
});
63+
64+
it('an island that only the Markdown page imports hydrates', async () => {
65+
const root = mkdtempSync(join(import.meta.dirname, '__tmp_md_page_island_'));
66+
mkdirSync(join(root, 'pages'));
67+
mkdirSync(join(root, 'components'));
68+
writeFileSync(
69+
join(root, 'components/Title.vue'),
70+
`<script setup>
71+
import { useFrontmatter } from "@void/md";
72+
const frontmatter = useFrontmatter();
73+
</script>
74+
<template><p id="island-fm">{{ String(frontmatter.title) }}</p></template>
75+
`,
76+
);
77+
writeFileSync(
78+
join(root, 'pages', 'index.md'),
79+
`<script>
80+
import Title from "../components/Title.vue" with { island: "load" };
81+
</script>
82+
83+
---
84+
title: Hello from frontmatter
85+
---
86+
87+
<Title />
88+
`,
89+
);
90+
91+
let visit: IslandPageVisit | undefined;
92+
try {
93+
visit = await openIslandPage(root, {
94+
plugins: [
95+
...voidMarkdown(),
96+
vue(),
97+
{ name: 'void-vue:pages' },
98+
...islandsPlugin(join(root, 'pages')),
99+
],
100+
});
101+
expect(visit.html).toContain(ISLAND_FRONTMATTER_SCRIPT);
102+
await waitForIslandsHydrated(visit.page);
103+
expect(await visit.page.textContent('#island-fm')).toBe('Hello from frontmatter');
104+
expect(visit.diagnostics().toLowerCase()).not.toContain('hydrat');
105+
expect(visit.diagnostics()).not.toContain('pageerror');
106+
} finally {
107+
await visit?.close();
108+
rmSync(root, { recursive: true, force: true });
109+
}
110+
});

0 commit comments

Comments
 (0)