Repository navigation
CurlShot API docs rendered with marked: async walkTokens for highlighting, TOC from the heading renderer #4143
SergiuPogor
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Sharing how we use marked in production, in case the async
walkTokenspattern helps someone.The docs of CurlShot, a screenshot API (URL, HTML or Markdown in; PNG, JPEG, WebP, PDF or video out), are plain Markdown files rendered server-side in Next.js with marked.
What worked well:
walkTokens. Withnew Marked({ async: true, gfm: true, async walkTokens(token) { ... } }), everycodetoken is highlighted (Shiki) before rendering and the HTML is stored on the token; thecoderenderer just returns it. No sync highlighter needed.headingrenderer. The renderer slugifies the heading text, adds a number when the same heading repeats on a page (for example "Common mistakes"), pushes h2/h3 into a TOC array and adds an anchor link. A#in the Markdown is rendered as<h2>because the page title is the only<h1>.rel="noopener noreferrer"; images become<figure>with known width/height so the page does not jump, and an example image that does not exist yet is left out instead of rendering broken; tables are wrapped for horizontal scrolling; a paragraph that only held a figure is not wrapped in<p>.Thanks for marked. The token/renderer API made all of this a few dozen lines.
All reactions