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
5 changes: 5 additions & 0 deletions .changeset/bright-apples-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-satteri': patch
---

Fix syntax highlighting for fenced code blocks when raw HTML is enabled.
8 changes: 7 additions & 1 deletion packages/markdown/satteri/src/satteri-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ export function createHighlightPlugin(
) as HastNode | undefined;
if (!codeChild || codeChild.type !== 'element') return;

const lang = (codeChild.data as any)?.lang ?? 'plaintext';
const languageClass = Array.isArray(codeChild.properties?.className)
? codeChild.properties.className.find(
(className) => typeof className === 'string' && className.startsWith('language-'),
)
: undefined;
const lang =
(codeChild.data as any)?.lang ?? languageClass?.slice('language-'.length) ?? 'plaintext';
const meta = (codeChild.data as any)?.meta ?? undefined;

if (
Expand Down
10 changes: 10 additions & 0 deletions packages/markdown/satteri/test/highlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ describe('satteri highlight', () => {
assert.match(code, /background-color:/);
});

it('preserves fence languages when raw HTML is enabled', async () => {
for (const rawHtml of [false, true]) {
const processor = await createSatteriMarkdownProcessor({
features: { rawHtml },
});
const { code } = await processor.render('```ts\nconst x = 1\n```');
assert.match(code, /data-language="ts"/);
}
});

it('does not highlight math code blocks by default', async () => {
const processor = await createSatteriMarkdownProcessor();
const { code } = await processor.render('```math\n\\frac{1}{2}\n```');
Expand Down
Loading