Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.

Commit c6c501d

Browse files
committed
(#17) Add language styling to Dynamic Code Block
This adds the ability to pass in a language to the Dynamic Code block component and set that in the class on the code block to have the appropriate styling or highlighting in the syntax of the code.
1 parent 2f313da commit c6c501d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
---
2+
import { z } from 'zod';
3+
4+
const dynamicCodeBlockSchema = z.object({
5+
language: z.enum(['markup', 'css', 'clike', 'javascript', 'bash', 'csharp', 'diff', 'json', 'powershell', 'puppet', 'python', 'ruby', 'scss', 'shell-session', 'xml-doc', 'yaml'])
6+
});
7+
8+
type DynamicCodeBlock = z.infer<typeof dynamicCodeBlockSchema>;
9+
type DynamicCodeBlockLanguage = DynamicCodeBlock['language'];
10+
11+
interface Props {
12+
language: DynamicCodeBlockLanguage
13+
}
14+
15+
const { language } = Astro.props;
16+
17+
const content = {
18+
language
19+
}
20+
21+
dynamicCodeBlockSchema.parse(content);
22+
223
const generateRandomClass = () => {
324
const randomNumber = Math.floor(Math.random() * 1000);
425
return `dynamic-code-block-${randomNumber}`;
526
};
627
---
728

829
<div class="dynamic-code-block-container copy-command">
9-
<pre class="language-powershell"><code class={generateRandomClass()}>
30+
<pre class=`language-${language}`><code class={generateRandomClass()}>
1031
<slot />
1132
</code></pre>
1233
</div>

0 commit comments

Comments
 (0)