Skip to content

Commit 8d90d30

Browse files
committed
refactor: streamline markdown processing by removing hashtag handling in headings and enhancing sanitization logic
1 parent cb46d89 commit 8d90d30

2 files changed

Lines changed: 13 additions & 26 deletions

File tree

app/globals.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@
349349
font-size: smaller;
350350
}
351351

352+
.markdown-body [id]::before {
353+
content: attr(id);
354+
display: none;
355+
}
356+
352357
iframe {
353358
width: 100% !important;
354359
height: 35rem !important;

components/ArticleContent.tsx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,19 @@ const ArticleContent: React.FC<ArticleContentProps> = ({ content }) => {
1212
const [sanitizedContent, setSanitizedContent] = useState<string>("");
1313

1414
useEffect(() => {
15-
async function processContent() {
16-
// Convert markdown to HTML with Marked and await the result
17-
const rawHtml = await marked.parse(content);
18-
19-
// Remove #hashtags specifically from heading text using a regex on <hN> blocks
20-
const htmlNoHashtagsInHeadings = rawHtml.replace(
21-
/(<h[1-6][^>]*>)([^<]+)(<\/h[1-6]>)/g,
22-
(match, openTag, headingText, closeTag) => {
23-
// Remove all occurrences of #something
24-
const textWithoutHashtags = headingText.replace(/#[^\s]+/g, "");
25-
return `${openTag}${textWithoutHashtags}${closeTag}`;
26-
}
27-
);
28-
29-
// Sanitize final HTML
30-
const cleanContent = DOMPurify.sanitize(htmlNoHashtagsInHeadings, {
15+
async function sanitizeContent() {
16+
// Convert markdown to HTML
17+
const rawContent = await marked.parse(content);
18+
19+
// Configure DOMPurify to allow iframes
20+
const cleanContent = DOMPurify.sanitize(rawContent, {
3121
ADD_TAGS: ["iframe"],
32-
ADD_ATTR: [
33-
"allow",
34-
"allowfullscreen",
35-
"frameborder",
36-
"height",
37-
"scrolling",
38-
"src",
39-
"width",
40-
],
22+
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "height", "scrolling", "src", "width"],
4123
});
4224

4325
setSanitizedContent(cleanContent);
4426
}
45-
processContent();
27+
sanitizeContent();
4628
}, [content]);
4729

4830
return (

0 commit comments

Comments
 (0)