Skip to content

Extract the code snippet parser from server/remark-code-snippet.ts - #857

Draft
aatuvai wants to merge 1 commit into
mainfrom
aatuvai/2026-10-23-code-snippet-refactor
Draft

aatuvai wants to merge 1 commit into
mainfrom
aatuvai/2026-10-23-code-snippet-refactor

Conversation

@aatuvai

@aatuvai aatuvai commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

server/remark-code-snippet.ts turns ```code fences into copyable command snippets. It parses the snippet text and builds the MDX nodes in the same pass, which means the parsing could only run inside the remark plugin.

This PR moves the parser out of the plugin, so a follow-up PR can add a runtime component that can use the same parsing logic in the browser.

Summary

  • Adds src/utils/codeSnippet.ts, a parser with no dependencies that turns snippet text into a list of commands, comments, code lines and line breaks, with inline <Var /> tags.
  • remark-code-snippet now builds its MDX nodes from the parser's output instead of parsing the text itself.
  • The rendered output is the same for well-formed snippets. A few edge cases now behave differently (see below).

Changes

  • src/utils/codeSnippet.ts (new file)
    • parseCodeSnippet handles $ commands, > host $ commands, # descriptions, ## comments, multiline commands ending in \, heredocs and empty lines.
    • parseInline splits a line into text and <Var /> tags.
    • Problems are returned as diagnostics with a severity instead of being reported directly. The plugin decides how to report them.
  • server/remark-code-snippet.ts
    • The parsing code is replaced by a call to parseCodeSnippet and a mapping from its nodes to the existing snippet, command, commandline, commandcomment, codeline and br elements.
    • Reporting is unchanged: with lint, the first diagnostic fails the file. Without it, errors are logged and warnings are ignored.
  • Edge cases that behave differently
    • <Var isGlobal="false" /> and <Var isGlobal={false} /> are no longer treated as global. Before, any isGlobal attribute made the variable global.
    • <Var /> attributes can use single quotes or {...}. Before, a name or description written that way crashed the build.
    • A <Var /> without a name is reported as an error and left as text. Before, it crashed the build with a TypeError.
    • A heredoc marker with an unclosed quote, such as <<'EOF, is now reported as an unclosed heredoc. Before, it crashed the build.
    • The build log message for an unclosed heredoc now reads "No closing line for heredoc format" instead of naming the marker.
  • Tests
    • src/utils/codeSnippet.test.ts (new) covers the parser, including the edge cases above.
    • server/remark-code-snippet.test.ts is unchanged and passes.

@aatuvai
aatuvai deployed to docs-amplify October 23, 2026 14:11 — with GitHub Actions Active
@aatuvai aatuvai changed the title Extract the code snippet parser from remark-code-snippet Extract the code snippet parser from server/remark-code-snippet.ts Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Amplify deployment status

Branch Commit Job ID Status Preview Updated (UTC)
aatuvai/2026-10-23-code-snippet-refactor b159a49 3 ✅SUCCEED aatuvai-2026-10-23-code-snippet-refactor 2026-10-30 13:19:25

Move the parsing of ```code snippets (commands, comments, multiline and
heredoc commands, and inline <Var /> tags) out of
server/remark-code-snippet.ts into src/utils/codeSnippet.ts, so that runtime
components can use the same logic.

The Remark plugin now builds its MDX nodes from the parser's output instead of
parsing the text itself.
@aatuvai
aatuvai force-pushed the aatuvai/2026-10-23-code-snippet-refactor branch from 6f863f2 to b159a49 Compare October 30, 2026 13:10
@aatuvai
aatuvai deployed to docs-amplify October 30, 2026 13:10 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
docs-amplify — b159a492 Deployed Oct 30, 2026 by aatuvai via Amplify Preview #2285
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant