Skip to content

Commit a647769

Browse files
davepagurekNalinDalal
authored andcommitted
Fix escaped characters in inline examples + instance mode examples
1 parent 85a2f91 commit a647769

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/components/CodeEmbed/frame.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface CodeBundle {
77
js?: string;
88
base?: string;
99
scripts?: string[];
10+
instanceMode?: boolean;
1011
}
1112

1213
/*
@@ -44,7 +45,7 @@ ${code.css || ""}
4445
</style>
4546
<!-- If we need an addon script, load p5 the usual way with no caching to make sure
4647
the import order doesn't get messed up. -->
47-
${((code.scripts?.length ?? 0) > 0 ? [cdnLibraryUrl, ...(code.scripts ?? [])] : []).map((src) => `<script type="text/javascript" src="${src}"></script>`).join('\n')}
48+
${(code.instanceMode || (code.scripts?.length ?? 0) > 0 ? [cdnLibraryUrl, ...(code.scripts ?? [])] : []).map((src) => `<script type="text/javascript" src="${src}"></script>`).join('\n')}
4849
<body>${code.htmlBody || ""}</body>
4950
<script id="code" type="text/javascript">${wrapSketch(code.js) || ""}</script>
5051
${(code.scripts?.length ?? 0) > 0 ? '' : `
@@ -161,6 +162,7 @@ export const CodeFrame = (props: CodeFrameProps) => {
161162
htmlBody: props.htmlBodyCode,
162163
base: props.base,
163164
scripts: props.scripts,
165+
instanceMode: props.jsCode.includes('new p5'),
164166
}) : undefined}
165167
sandbox="allow-scripts allow-popups allow-modals allow-forms allow-same-origin"
166168
aria-label="Code Preview"

src/components/CodeEmbed/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const CodeEmbed = (props) => {
3636
);
3737

3838
let { previewWidth, previewHeight } = props;
39-
const canvasMatch = /createCanvas\(\s*(\d+),\s*(\d+)\s*(?:,\s*(?:P2D|WEBGL)\s*)?\)/m.exec(initialCode);
39+
const canvasMatch = /createCanvas\(\s*(\d+),\s*(\d+)\s*(?:,\s*(?:\w+\.)?(?:P2D|WEBGL)\s*)?\)/m.exec(initialCode);
4040
if (canvasMatch) {
4141
previewWidth = previewWidth || parseFloat(canvasMatch[1]);
4242
previewHeight = previewHeight || parseFloat(canvasMatch[2]);

src/layouts/ReferenceItemLayout.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { setJumpToState } from "../globals/state";
2121
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";
24+
import _ from 'lodash';
2425
2526
const { entry, relatedEntries } = Astro.props;
2627
const currentLocale = getCurrentLocale(Astro.url.pathname);
@@ -121,9 +122,9 @@ const descriptionParts = description.split(
121122
)}
122123
{descriptionParts.map((part) => {
123124
if (part.startsWith('<pre')) {
124-
const exampleCode = part
125+
const exampleCode = _.unescape(part
125126
.replace(/<pre><code class="language-js example">/, '')
126-
.replace(/<\/code><\/pre>/, '');
127+
.replace(/<\/code><\/pre>/, ''));
127128

128129
return (
129130
<CodeEmbed

0 commit comments

Comments
 (0)