Skip to content
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
45 changes: 45 additions & 0 deletions src/__tests__/unit/enrichers/cbp-enricher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,48 @@ describe('CBP enricher — wrapper font-family sync', () => {
expect(second).toBe(unchanged);
});
});

describe('CBP enricher — front-end rendering contract', () => {
/**
* CBP's front-end script loads `build/fonts/<data attribute>.woff2`. With the
* fallback stack in the attribute it requests a file that does not exist, the
* webfont never loads, and the wider fallback font wraps two-digit line numbers.
*/
it('writes the bare font name to the webfont data attribute', async () => {
const result = await enrichBlock({
name: 'kevinbatdorf/code-block-pro',
attributes: { code: 'const a = 1;', language: 'javascript', fontFamily: 'Code-Pro-JetBrains-Mono' },
innerHTML:
'<div class="wp-block-kevinbatdorf-code-block-pro" ' +
'data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" ' +
'style="font-family:Code-Pro-JetBrains-Mono;font-size:1rem">' +
'<pre class="shiki gravitykit-dark"><code>stale</code></pre></div>',
});
expect(result.innerHTML).toContain('data-code-block-pro-font-family="Code-Pro-JetBrains-Mono"');
expect(result.innerHTML).toContain(
'font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace',
);
});

/**
* A token span without its own color inherits the page's `pre`/`code` color,
* which on BetterDocs docs is dark gray on the navy code background.
*/
it('gives every token span an explicit color', async () => {
for (const [code, language] of [
['// plain text\nadd_filter( "a", "b" );', 'plaintext'],
["add_filter( 'a', '__return_false' );", 'php'],
]) {
const result = await enrichBlock({
name: 'kevinbatdorf/code-block-pro',
attributes: { code, language, theme: 'gravitykit-dark' },
});
const codeHTML = result.attributes?.codeHTML as string;
expect(codeHTML).not.toMatch(/<span>/);
if (language === 'plaintext') {
expect(codeHTML).toContain('<span style="color: var(--shiki-color-text)">');
}
expect(result.innerHTML).not.toMatch(/<span>/);
}
});
});
17 changes: 12 additions & 5 deletions src/enrichers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ export async function shikiHighlight(code: string, language: string, themeName?:
let html = hl.codeToHtml(code, { lang, theme: 'css-variables' })
.replace(/var\(--shiki-foreground\)/g, 'var(--shiki-color-text)')
.replace(/var\(--shiki-background\)/g, 'var(--shiki-color-background)');
// Shiki leaves default-color tokens as bare <span>s, which inherit theme colors
// (dark gray on BetterDocs). CBP's own output colors every token span.
html = html.replace(/<span>/g, '<span style="color: var(--shiki-color-text)">');
if (themeName && themeName !== 'css-variables') {
// Replacer function so a `$` in themeName is not read as a replacement token.
html = html.replace(/(<pre[^>]*class="shiki) css-variables(")/, (_m, p1, p2) => `${p1} ${themeName}${p2}`);
Expand Down Expand Up @@ -339,11 +342,15 @@ function syncWrapperFontFamily(innerHTML: string, fontFamily: unknown): string {
tag = tag.replace(/>$/, () => ` style="font-family:${nextFont}">`);
}

// CBP's front-end script reads this attribute to decide which webfont to load.
tag = tag.replace(
/data-code-block-pro-font-family="[^"]*"/,
() => `data-code-block-pro-font-family="${nextFont}"`,
);
// CBP's front-end script loads `build/fonts/<this value>.woff2`, so this takes
// the bare attribute. A fallback stack here names a file that does not exist.
if (attrFont !== null) {
const dataFont = escapeAttr(attrFont.trim());
tag = tag.replace(
/data-code-block-pro-font-family="[^"]*"/,
() => `data-code-block-pro-font-family="${dataFont}"`,
);
}

return innerHTML.replace(openTagPattern, () => tag);
}
Expand Down
12 changes: 8 additions & 4 deletions wordpress-plugin/gk-block-mcp/assets/mcp-server/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -52453,6 +52453,7 @@ async function shikiHighlight(code, language, themeName) {
const { hl, langs } = await getHighlighter();
const lang29 = langs.has(language) ? language : "plaintext";
let html5 = hl.codeToHtml(code, { lang: lang29, theme: "css-variables" }).replace(/var\(--shiki-foreground\)/g, "var(--shiki-color-text)").replace(/var\(--shiki-background\)/g, "var(--shiki-color-background)");
html5 = html5.replace(/<span>/g, '<span style="color: var(--shiki-color-text)">');
if (themeName && themeName !== "css-variables") {
html5 = html5.replace(/(<pre[^>]*class="shiki) css-variables(")/, (_m, p1, p2) => `${p1} ${themeName}${p2}`);
}
Expand Down Expand Up @@ -52529,10 +52530,13 @@ function syncWrapperFontFamily(innerHTML, fontFamily) {
} else {
tag = tag.replace(/>$/, () => ` style="font-family:${nextFont}">`);
}
tag = tag.replace(
/data-code-block-pro-font-family="[^"]*"/,
() => `data-code-block-pro-font-family="${nextFont}"`
);
if (attrFont !== null) {
const dataFont = escapeAttr(attrFont.trim());
tag = tag.replace(
/data-code-block-pro-font-family="[^"]*"/,
() => `data-code-block-pro-font-family="${dataFont}"`
);
}
return innerHTML.replace(openTagPattern, () => tag);
}
registerBlockEnricher("kevinbatdorf/code-block-pro", async (block) => {
Expand Down
2 changes: 2 additions & 0 deletions wordpress-plugin/gk-block-mcp/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Visit Settings → Block MCP. Set the score for a namespace to less than 10 to m

* Tool calls now return a clear error when a login gateway or security plugin answers in place of WordPress.
* WordPress credentials are no longer sent when a site redirects a request to another subdomain.
* Code Block Pro blocks written through Block MCP now show their text in the theme's code color. Before, plain text in them could take the page's own text color, such as dark gray on a navy code background.
* Code Block Pro blocks written through Block MCP now load their code font. Before, they showed in a wider fallback font, and line numbers 10 and up broke onto two lines.

#### 💻 Developer Updates

Expand Down
Loading