Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
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
11 changes: 6 additions & 5 deletions packages/astro-markflow/src/transforms/inject-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,19 @@ export default function Content() {
expect(result).toContain(ASTRO_COMPONENTS_MODULE);
});

it('should inject Prism component', () => {
it('should not inject Prism (not a built-in Astro component)', () => {
const code = `
export default function Content() {
return <Prism lang="js">const x = 1;</Prism>;
}`;

const result = injectAstroComponents(code);

expect(result).toContain('import { Prism }');
expect(result).toContain(ASTRO_COMPONENTS_MODULE);
expect(result).not.toContain('import { Prism }');
expect(result).toBe(code);
});

it('should inject both Code and Prism', () => {
it('should only inject Code when both Code and Prism are used', () => {
const code = `
export default function Content() {
return (
Expand All @@ -277,7 +277,8 @@ export default function Content() {

const result = injectAstroComponents(code);

expect(result).toContain('import { Code, Prism }');
expect(result).toContain('import { Code }');
expect(result).not.toContain('import { Code, Prism }');
});

it('should not inject if no Astro components used', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/astro-markflow/src/transforms/inject-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { collectImportedNames, insertAfterImports } from '../utils/imports.js';
import { resolveStarlightConfig, type StarlightUserConfig } from '../utils/config.js';
import { stripHeadingsMeta } from '../utils/validation.js';

/** Strip set:html={...} string content to avoid false component matches in code blocks */
function stripSetHtmlContent(code: string): string {
return code.replace(/set:html=\{("(?:[^"\\]|\\.)*")\}/g, 'set:html={""}');
}

/**
* Generic component import injection.
* Scans code for component usage and injects missing imports.
Expand All @@ -30,7 +35,7 @@ export function injectComponentImports(
if (!code || typeof code !== 'string' || components.length === 0) {
return code;
}
const scanTarget = stripHeadingsMeta(code);
const scanTarget = stripSetHtmlContent(stripHeadingsMeta(code));

// PERF: Use single combined regex instead of per-component regex
// This reduces from O(n) regex compilations to O(1)
Expand Down Expand Up @@ -123,7 +128,7 @@ export function injectComponentImportsFromRegistry(
return code;
}

const scanTarget = stripHeadingsMeta(code);
const scanTarget = stripSetHtmlContent(stripHeadingsMeta(code));
const imported = collectImportedNames(code);

// PERF: Use single combined regex instead of per-component regex
Expand Down
1 change: 0 additions & 1 deletion packages/markflow/src/registry/presets/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const astroLibrary: ComponentLibrary = {
defaultModulePath: 'astro/components',
components: [
{ name: 'Code', modulePath: 'astro/components', exportType: 'named' },
{ name: 'Prism', modulePath: 'astro/components', exportType: 'named' },
],
directiveMappings: [],
};
Loading