Skip to content

Escape special characters in default value table cells#104

Closed
greymoth-jp wants to merge 1 commit into
jsdoc2md:masterfrom
greymoth-jp:fix-escape-default-value-table-cells
Closed

Escape special characters in default value table cells#104
greymoth-jp wants to merge 1 commit into
jsdoc2md:masterfrom
greymoth-jp:fix-escape-default-value-table-cells

Conversation

@greymoth-jp

Copy link
Copy Markdown

Problem

When a parameter or property default value contains a pipe, it is written raw into the Default column of the generated table. The extra | is read as a column separator, so the row gets one cell too many. GitHub-flavoured markdown then drops the overflowing cell, which means the Description is silently lost.

This happens for any default value with a | in it, for example:

/**
 * @param {string} [sep="|"] - the separator to use
 * @param {RegExp} [re=/a|b/] - split pattern
 */
function join (sep, re) {}

Generated table (the descriptions never make it into the rendered output):

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [sep] | <code>string</code> | <code>"|"</code> | the separator to use |
| [re] | <code>RegExp</code> | <code>/a|b/</code> | split pattern |

Why this column and not the others

The Type column in the same table already protects against this. Union types are joined with an escaped \| delimiter, and each type name is run through the escape helper in link.hbs. So {string|number} renders as string \| number and stays inside its cell. The Default cell is the only value cell that emits its content without that escaping.

Fix

Run the default value through the existing escape helper, the same one the type cells use. I also changed escape() to return non-string input unchanged. It returned null before, and since default values can be numbers or booleans, escaping them directly would otherwise blank them out. The helper is only ever called on strings today, so this does not change any existing output.

After the change the pipe is escaped, the rows keep four cells, and the descriptions survive. Numeric and boolean defaults are unaffected. partials/partial-cache.js is regenerated via npm run partials.

npm test passes.

A default value containing a pipe (e.g. `[sep="|"]` or a RegExp default
such as `/a|b/`) was written raw into the Default column of the params
and properties tables. The stray `|` splits the table row, so GFM drops
the trailing Description cell and that row's description is lost.

The sibling Type column already guards against this: union types use an
escaped ` \| ` delimiter and type names pass through the escape helper in
link.hbs. Run the default value through the same escape helper. escape()
now returns non-string input unchanged so numeric and boolean defaults
are preserved (it previously returned null, blanking them out).
@greymoth-jp

Copy link
Copy Markdown
Author

Closing this to clean up my open PRs. It's a small change and not really worth taking up your review time. Sorry for the noise.

@greymoth-jp greymoth-jp closed this Jul 4, 2026
@75lb

75lb commented Jul 5, 2026

Copy link
Copy Markdown
Member

We've come across this issue before and there was a reason the code remained the way it is - I can't just can't remember why so my first job was to track down the previous issue thread. But if you are closing, no big deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants