Skip to content

Commit 093eb65

Browse files
mishig25claude
andauthored
feat: add language-* class to rendered code blocks (#787)
* feat: add language-* class to rendered code blocks The fence language identifier was parsed by mdsvex and used only to pick the highlighter, then dropped before rendering. Forward it to CodeBlock / CodeBlockFw and emit it as `class="language-<lang>"` on the `<pre>`, so downstream consumers can target code blocks by language in CSS or external tooling. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * style: apply prettier formatting Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8991858 commit 093eb65

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

kit/preprocessors/mdsvex/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ const _mdsvexPreprocess = mdsvex({
338338
const base64 = (val) => btoa(encodeURIComponent(val));
339339
const escape = (code) =>
340340
code.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/}/g, "\\}").replace(/\$/g, "\\$");
341+
const safeLang = lang && /^[\w-]+$/.test(lang) ? lang : "";
341342
const REGEX_FRAMEWORKS_SPLIT = /\s*===(PT-TF|STRINGAPI-READINSTRUCTION)-SPLIT===\s*/gm;
342343

343344
// Handle mermaid diagrams
@@ -380,6 +381,7 @@ const _mdsvexPreprocess = mdsvex({
380381
code: \`${base64(codeGroup2)}\`,
381382
highlighted: \`${escape(highlightedTf)}\`
382383
}}
384+
lang="${safeLang}"
383385
wrap={${wrapCodeBlocks}}
384386
/>`;
385387
} else {
@@ -394,9 +396,10 @@ const _mdsvexPreprocess = mdsvex({
394396
.join("\n");
395397
}
396398
return `
397-
<CodeBlock
399+
<CodeBlock
398400
code={\`${base64(code)}\`}
399401
highlighted={\`${escape(highlighted)}\`}
402+
lang="${safeLang}"
400403
wrap={${wrapCodeBlocks}}
401404
/>`;
402405
}

kit/src/lib/CodeBlock.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
let hideCopyButton = true;
44
export let code = "";
55
export let highlighted = "";
6+
export let lang = "";
67
export let wrap = false;
78
export let classNames = "";
89
@@ -28,5 +29,8 @@
2829
value={code}
2930
/>
3031
</div>
31-
<pre class={wrap ? "whitespace-pre-wrap" : ""}>{@html highlighted}</pre>
32+
<pre
33+
class="{lang ? `language-${lang}` : ''} {wrap
34+
? 'whitespace-pre-wrap'
35+
: ''}">{@html highlighted}</pre>
3236
</div>

kit/src/lib/CodeBlockFw.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
export let group1: { id: string; code: string; highlighted: string };
77
export let group2: { id: string; code: string; highlighted: string };
8+
export let lang = "";
89
export let wrap = false;
910
1011
const ids = [group1.id, group2.id];
@@ -36,7 +37,8 @@
3637
value={group1.code}
3738
/>
3839
</div>
39-
<pre class={wrap ? "whitespace-pre-wrap" : ""}><FrameworkSwitch
40+
<pre
41+
class="{lang ? `language-${lang}` : ''} {wrap ? 'whitespace-pre-wrap' : ''}"><FrameworkSwitch
4042
{ids}
4143
/>{@html group1.highlighted}</pre>
4244
{:else}
@@ -47,7 +49,8 @@
4749
value={group2.code}
4850
/>
4951
</div>
50-
<pre class={wrap ? "whitespace-pre-wrap" : ""}><FrameworkSwitch
52+
<pre
53+
class="{lang ? `language-${lang}` : ''} {wrap ? 'whitespace-pre-wrap' : ''}"><FrameworkSwitch
5154
{ids}
5255
/>{@html group2.highlighted}</pre>
5356
{/if}

0 commit comments

Comments
 (0)