Skip to content

Commit 0905dc4

Browse files
authored
Merge pull request #1065 from processing/syntax-highlight
Add syntax highlighting for code blocks in reference
2 parents 30c65da + 2df4851 commit 0905dc4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/layouts/ReferenceItemLayout.astro

Lines changed: 13 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="language-js example">')) {
125126
const exampleCode = _.unescape(part
126127
.replace(/<pre><code class="language-js example">/, '')
127128
.replace(/<\/code><\/pre>/, ''));
@@ -137,6 +138,15 @@ 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 as any} theme="github-light" />
149+
);
140150
} else {
141151
return (
142152
<div

0 commit comments

Comments
 (0)