Skip to content

Commit 474c758

Browse files
CopilotSnooz82
andcommitted
Optimize GlossaryTable: pre-compute sanitized HTML in useMemo
- Added `definitionHtml` field to DisplayEntry type - Compute sanitized HTML once when creating entries in useMemo - Use pre-computed HTML in render instead of calling sanitizeMarkdown on every render - This avoids re-parsing and re-sanitizing markdown on every render for every filtered entry Co-authored-by: Snooz82 <41592183+Snooz82@users.noreply.github.com>
1 parent a3aefef commit 474c758

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

website/src/components/Glossary/GlossaryTable.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type DisplayEntry = {
1717
term: string;
1818
abbreviation: string;
1919
definition: string;
20+
definitionHtml: string;
2021
canonicalTerm: string;
2122
isAlias: boolean;
2223
slug: string;
@@ -52,6 +53,7 @@ const GlossaryTable: React.FC = () => {
5253
term: item.term,
5354
abbreviation: item.abbreviation,
5455
definition: item.definition,
56+
definitionHtml: sanitizeMarkdown(item.definition, purify),
5557
canonicalTerm: item.term,
5658
isAlias: false,
5759
slug,
@@ -68,6 +70,7 @@ const GlossaryTable: React.FC = () => {
6870
term: alias,
6971
abbreviation: '',
7072
definition: `See ${item.term}`,
73+
definitionHtml: '', // Alias entries don't need HTML since they use a link
7174
canonicalTerm: item.term,
7275
isAlias: true,
7376
slug: slugify(alias),
@@ -82,7 +85,7 @@ const GlossaryTable: React.FC = () => {
8285

8386
const combined = [...canonicalEntries, ...aliasEntries].sort((a, b) => a.term.localeCompare(b.term));
8487
return { entries: combined, aliasToCanonicalSlug: aliasMap };
85-
}, []);
88+
}, [purify]);
8689

8790
const fuse = useMemo(
8891
() =>
@@ -202,7 +205,6 @@ const GlossaryTable: React.FC = () => {
202205
</thead>
203206
<tbody>
204207
{filteredEntries.map((entry) => {
205-
const definitionHtml = sanitizeMarkdown(entry.definition, purify);
206208
const handleClick = () => focusEntry(entry.targetSlug, entry.canonicalTerm);
207209

208210
return (
@@ -245,7 +247,7 @@ const GlossaryTable: React.FC = () => {
245247
<>
246248
<div
247249
className={styles.definitionText}
248-
dangerouslySetInnerHTML={{ __html: definitionHtml }}
250+
dangerouslySetInnerHTML={{ __html: entry.definitionHtml }}
249251
/>
250252
<div className={styles.pillRow}>
251253
{entry.abbreviation ? (

0 commit comments

Comments
 (0)