Skip to content

Commit dce22c5

Browse files
committed
Add syntax highlighting for code blocks in reference
1 parent 8520709 commit dce22c5

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

src/layouts/ReferenceItemLayout.astro

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { p5Version } from "../globals/p5-version";
2222
import flask from "@src/content/ui/images/icons/flask.svg?raw";
2323
import warning from "@src/content/ui/images/icons/warning.svg?raw";
2424
import _ from 'lodash';
25+
import { Code } from 'astro:components';
2526
2627
const { entry, relatedEntries } = Astro.props;
2728
const currentLocale = getCurrentLocale(Astro.url.pathname);
@@ -79,7 +80,7 @@ const relatedReferences = [
7980
const seenParams: Record<string, true> = {};
8081
8182
const descriptionParts = description.split(
82-
/(<pre><code class="language-js example">[\s\S]+?<\/code><\/pre>)/gm
83+
/(<pre><code class="language-js(?: example)?">[\s\S]+?<\/code><\/pre>)/gm
8384
);
8485
8586
---
@@ -120,8 +121,8 @@ const descriptionParts = description.split(
120121
<p set:html={entry.data.deprecated} />
121122
</div>
122123
)}
123-
{descriptionParts.map((part) => {
124-
if (part.startsWith('<pre')) {
124+
{descriptionParts.map((part, i) => {
125+
if (part.startsWith('<pre><code class="languuage-js example">')) {
125126
const exampleCode = _.unescape(part
126127
.replace(/<pre><code class="language-js example">/, '')
127128
.replace(/<\/code><\/pre>/, ''));
@@ -137,6 +138,22 @@ const descriptionParts = description.split(
137138
includeSound={entry.data.module === 'p5.sound'}
138139
/>
139140
)
141+
} else if (part.startsWith('<pre>')) {
142+
const code = _.unescape(part
143+
.replace(/<pre><code( class="[^+]*")?>/, '')
144+
.replace(/<\/code><\/pre>/, ''));
145+
const langMatch = /<code class="language-(\w+)[^"]*">/.exec(part);
146+
const lang = langMatch ? langMatch[1] : undefined;
147+
return (
148+
<Code code={code} lang={lang} theme="github-light" />
149+
)
150+
const html = descriptionPartRenderedCode[i];
151+
return (
152+
<div
153+
set:html={html}
154+
class="[&_a]:text-type-magenta-dark [&_a]:!decoration-type-magenta-dark mb-xl reference-item rendered-markdown"
155+
/>
156+
);
140157
} else {
141158
return (
142159
<div

0 commit comments

Comments
 (0)