Skip to content

Commit 1ffb11d

Browse files
authored
Merge pull request #453 from processing/fix/html-in-code
Handle divs wrapping code examples
2 parents 6d126dc + 8aae5f8 commit 1ffb11d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/layouts/ReferenceItemLayout.astro

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Head from "@components/Head/index.astro";
55
import {
66
getRefEntryTitleConcatWithParen,
77
escapeCodeTagsContent,
8-
separateReferenceExamples,
8+
parseReferenceExamplesAndMetadata,
99
normalizeReferenceRoute,
1010
getRelatedEntriesinCollection,
1111
generateJumpToState,
@@ -22,9 +22,9 @@ import { setJumpToState } from "../globals/state";
2222
const { entry, relatedEntries } = Astro.props;
2323
const currentLocale = getCurrentLocale(Astro.url.pathname);
2424
25-
const examples = separateReferenceExamples(entry.data.example)
25+
const examples = parseReferenceExamplesAndMetadata(entry.data.example)
2626
// Remove empty lines at the beginning and end of the examples
27-
?.map((example) => example.trim());
27+
?.map((example) => ({ ...example, src: example.src.trim() }));
2828
const description = escapeCodeTagsContent(entry.data.description);
2929
const t = await getUiTranslator(currentLocale);
3030
const title = getRefEntryTitleConcatWithParen(entry);
@@ -95,12 +95,12 @@ const seenParams: Record<string, true> = {};
9595
examples && (
9696
<div class="mb-xl">
9797
<h2 class="text-h3">{t("Examples")}</h2>
98-
{examples.map((exampleCode: string, i: number) => {
98+
{examples.map(({ src: exampleCode, classes }, i: number) => {
9999
return (
100100
<CodeEmbed
101101
client:load
102102
initialValue={exampleCode}
103-
previewable
103+
previewable={!classes.norender}
104104
editable
105105
lazyLoad={i > 0}
106106
previewHeight="100px"

src/pages/_utils.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,23 @@ export const getLibraryLink = (library: CollectionEntry<"libraries">) =>
194194
* @param examples Reference example strings from MDX
195195
* @returns The examples separated into individual strings
196196
*/
197-
export const separateReferenceExamples = (examples: string[]): string[] =>
197+
// separateReferenceExamples
198+
export const parseReferenceExamplesAndMetadata = (examples: string[]): { src: string, classes: Record<string, any> }[] =>
198199
examples
199200
?.flatMap((example: string) => example.split("</div>"))
200-
.map((htmlFrag: string) => htmlFrag.replace(/<\/?div>|<\/?code>/g, ""))
201-
.filter((cleanExample: string) => cleanExample);
201+
.map((src: string) => {
202+
const matches = [...src.matchAll(/<div class=['"]([^"']*)['"]>/g)]
203+
const classes: Record<string, boolean> = {}
204+
for (const match of matches) {
205+
const tokens = match[1].split(/\s+/g)
206+
for (const token of tokens) {
207+
classes[token] = true
208+
}
209+
}
210+
return { classes, src }
211+
})
212+
.map(({ src, classes }) => ({ classes, src: src.replace(/<\/?div[^>]*>|<\/?code>/g, "") }))
213+
.filter(({ src }) => src);
202214

203215
/**
204216
* Returns the title concatenated with parentheses if the reference entry is a constructor or method

0 commit comments

Comments
 (0)