Skip to content
Merged
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
16 changes: 13 additions & 3 deletions src/layouts/ReferenceItemLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { p5Version } from "../globals/p5-version";
import flask from "@src/content/ui/images/icons/flask.svg?raw";
import warning from "@src/content/ui/images/icons/warning.svg?raw";
import _ from 'lodash';
import { Code } from 'astro:components';

const { entry, relatedEntries } = Astro.props;
const currentLocale = getCurrentLocale(Astro.url.pathname);
Expand Down Expand Up @@ -79,7 +80,7 @@ const relatedReferences = [
const seenParams: Record<string, true> = {};

const descriptionParts = description.split(
/(<pre><code class="language-js example">[\s\S]+?<\/code><\/pre>)/gm
/(<pre><code class="language-js(?: example)?">[\s\S]+?<\/code><\/pre>)/gm
);

---
Expand Down Expand Up @@ -120,8 +121,8 @@ const descriptionParts = description.split(
<p set:html={entry.data.deprecated} />
</div>
)}
{descriptionParts.map((part) => {
if (part.startsWith('<pre')) {
{descriptionParts.map((part, i) => {
if (part.startsWith('<pre><code class="language-js example">')) {
const exampleCode = _.unescape(part
.replace(/<pre><code class="language-js example">/, '')
.replace(/<\/code><\/pre>/, ''));
Expand All @@ -137,6 +138,15 @@ const descriptionParts = description.split(
includeSound={entry.data.module === 'p5.sound'}
/>
)
} else if (part.startsWith('<pre>')) {
const code = _.unescape(part
.replace(/<pre><code( class="[^+]*")?>/, '')
.replace(/<\/code><\/pre>/, ''));
const langMatch = /<code class="language-(\w+)[^"]*">/.exec(part);
const lang = langMatch ? langMatch[1] : undefined;
return (
<Code code={code} lang={lang as any} theme="github-light" />
);
} else {
return (
<div
Expand Down