Repository navigation
feat: add Markdown live preview tool - #358
Conversation
anurag629
left a comment
There was a problem hiding this comment.
Thanks @srikar-naidu, the editor itself works well. Lint, all 1288 tests, the build and the page check pass on top of develop, typing updates the preview, and the sanitizing holds up: <script>, onerror and javascript: links are all stripped.
A few things before I merge:
- In the built app the preview is empty when the page loads, even though the starter document is in the editor. The server renders
""(there's nowindowfor DOMPurify) and hydration keeps that, so nothing shows until the first edit. Setting amountedflag in auseEffectand only sanitizing once it's true fixes it, since the first client render then matches the server. - The new tool checklist from CONTRIBUTING.md:
- Add the tool to the "Which tool?" dropdown in
.github/ISSUE_TEMPLATE/bug_report.yml. - Move the pure logic (the marked setup with the code renderer, the stats, the insert helper) into
lib/markdown-preview.tswith tests inlib/__tests__/, plus a client test that the preview strips<script>,onerrorandjavascript:links. Right now there are no tests for the tool. - Step 5 of "Adding a tool": the README gallery and list,
.github/assets/og-markdown-preview.pngandmarkdown-previewin thepackage.jsonkeywords. You can leave the home share card as it is. --cyanis on two tools now (URL encoder and regex tester) and--greenonly on one, so use--greenwithaccentHex: "#3FDD6B".- Put the registry entry at the end of
TOOLS, after the regex tester.
- Add the tool to the "Which tool?" dropdown in
- The fence language goes into the
classattribute as is, so a fence like```"><img src=x>breaks out of it. DOMPurify removes the dangerous part, but the markup ends up broken. Keeping only[\w-]fromlangavoids that. metaDescriptionis 163 characters, so trim it a little to stay around 150 to 160.
|
@anurag629 , Addressed all feedback and pushed it to the PR branch. Changes include:
|
anurag629
left a comment
There was a problem hiding this comment.
Thanks @srikar-naidu, nice round. The preview shows on first load now, the sanitizing still holds (I typed script, onerror, javascript: and fence payloads into the built app), the fence language is cleaned up, and lint, all 1305 tests, the build and the page check pass on top of develop.
Two things left:
- The preview's background doesn't follow the toggle.
.cardsetsbackground: var(--bg-card), and that wins overbg-whiteandbg-[#17191f], so with the site in dark mode the light preview is dark text on a dark card, and with the site in light mode the dark preview is light text on a light card. Putting the background and text classes on an inner element (not the.carditself), or setting the background with an inline style, fixes both. .github/assets/og-markdown-preview.pngis the Regex Tester card. Please save the one from theog:imageof/markdown-preview.
And one optional thing, since it came from my suggestion: setMounted(true) in an effect adds a react-hooks/set-state-in-effect lint warning (12 now, develop has 11). useSyncExternalStore(() => () => {}, () => true, () => false) gives the same flag without it.
|
@anurag629 , I’ve addressed the remaining items:
I also ran targeted ESLint, TypeScript validation, and |
anurag629
left a comment
There was a problem hiding this comment.
Thanks @srikar-naidu, this one's ready. Both preview themes read well in both site themes now, the share image matches the built card, lint is back to the 11 warnings develop has, and all 1305 tests, the build and the page check pass on top of develop. Nice work on this one.
What and why
Adds a new
/markdown-previewtool for writing Markdown and viewing rendered HTML live.Features include:
.mdCloses #302
How I tested this
npm run dev/markdown-preview<script>alert("xss")</script>and confirmed it was sanitizedgit diff --checkScreenshots attached showing the editor, preview, GFM rendering, syntax highlighting, and toolbar.
AI assistance
AI was used to implement the Markdown preview tool, integrate the parser and sanitizer, add the UI, debug the SSR DOMPurify issue.
i tested and committed after reviewing myself
Type of change
Checklist
npm run lint && npm run test && npm run buildpasses locallylib/and have a test inlib/__tests__/page.tsx, client component,opengraph-image.tsx, and the bug report dropdown are all doneNotes for reviewers
The preview output is sanitized with DOMPurify before rendering and before HTML export. All processing happens client-side; Markdown content is not uploaded.
The UI was manually tested in the browser with GFM content, fenced code blocks, toolbar actions, theme switching, and unsafe HTML input.
Screenshots demonstrate the main editor/preview layout and GFM rendering.