Skip to content

fix: escape markdown special characters in markdown reporter#6155

Open
deeferentleeg wants to merge 2 commits into
mochajs:mainfrom
deeferentleeg:fix/issue-2306-escape-markdown-special-chars
Open

fix: escape markdown special characters in markdown reporter#6155
deeferentleeg wants to merge 2 commits into
mochajs:mainfrom
deeferentleeg:fix/issue-2306-escape-markdown-special-chars

Conversation

@deeferentleeg

Copy link
Copy Markdown

PR Checklist

Overview

The markdown reporter (-R md) inserts suite and test titles verbatim into the generated markdown. When a title contains characters that have special meaning in GitHub-Flavored Markdown (e.g. *, _, #, [, ], (, ), backticks), the output's semantics are broken — headings render incorrectly, links point to wrong anchors, and emphasis marks are misinterpreted.

This PR adds an escapeMarkdown() helper that backslash-escapes the full set of GFM special characters and applies it in three places:

  1. TOC link text (stringifyTOC): suite titles in the table of contents are now escaped.
  2. Suite headings (title() -> EVENT_SUITE_BEGIN): suite titles used as markdown headings are now escaped.
  3. Test titles (EVENT_TEST_PASS): test titles in the body output are now escaped.

The utils.slug() function (used for anchor names) already strips all non-word characters, so anchor links remain unaffected — only the human-visible text is escaped.

Example

Before:

# TOC
   - [***](#)
     - [\\](#-)
# ***
## \\
>>> should be escaped.

After:

# TOC
   - [\*\*\*](#)
     - [\\](#-)
# \*\*\*
## \\
\>\>\> should be escaped.

AI Disclosure

Portions of this PR were generated with the assistance of AI tools. The code has been manually reviewed for correctness, security, and adherence to project conventions. I am responsible for understanding and maintaining this code.

Suite and test titles containing markdown special characters (e.g. *, _,
#, [], ()) were inserted verbatim into the generated markdown, breaking
its semantics. Add an escapeMarkdown() helper that backslash-escapes
GitHub-Flavored Markdown special characters and apply it to suite titles
in TOC links and headings, and to test titles in pass output.

closes mochajs#2306
Add test cases verifying that markdown special characters in suite and
test titles are properly escaped in the markdown reporter output.

Refs mochajs#2306
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

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.

🐛 Bug: Escape special characters for markdown reporter

1 participant