Skip to content

fix(makeMarkdown): keep literal entity and tag text escaped - #1119

Open
goranai wants to merge 1 commit into
showdownjs:developfrom
goranai:fix/makemarkdown-entity-text
Open

goranai wants to merge 1 commit into
showdownjs:developfrom
goranai:fix/makemarkdown-entity-text

Conversation

@goranai

@goranai goranai commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #1114.

makeMarkdown.txt calls unescapeHTMLEntities on node.nodeValue. The DOM has already decoded entities there, so this decodes them a second time. Text the page shows as &lt; comes out as <. Text shown as <br> comes out as a real <br> tag.

const c = new showdown.Converter();

c.makeMarkdown('<p>Write &amp;lt; to get a less-than sign.</p>');
// before: 'Write < to get a less-than sign.\n\n'
// after:  'Write &amp;lt; to get a less-than sign.\n\n'

c.makeMarkdown('<p>Type &lt;br&gt; for a line break.</p>');
// before: 'Type <br> for a line break.\n\n'   makeHtml then gives a real <br>
// after:  'Type &lt;br> for a line break.\n\n'

The fix drops the second decode. It escapes & only when it starts something that looks like an entity (&name;, &#60;, &#x3C;). It escapes < only when a letter or / follows. Plain a & b, AT&T, 1 < 2 and x <3 stay as they are, so the existing escaping-html-entities fixture still passes.

The issue suggested backslash escapes (\&lt; and \<br>). Those also render right in current showdown. I used &amp; and &lt; because the docs put \<tag> escaping under the opt-in backslashEscapesHTMLTags option, and the entity form works in any Markdown parser. Happy to switch to the backslash form if you prefer it.

Behavior changes (makeMarkdown output only)

  • Literal text like &lt;, &amp;, &quot;, &copy;, &#60; or &#x3C; now comes out as &amp;lt; and so on. Before, it was decoded to the character, or left as an entity that makeHtml then decoded.
  • < followed by a letter or / now comes out as &lt;. This also hits harmless text such as a<b, which used to stay a<b. Both render the same.
  • Text that looks like an autolink (<https://e.x/>, <a@b.co>) no longer turns into a link on the way back.
  • Code spans and code blocks don't go through this function. They are unchanged.

Not covered

  • Text starting with <! or <? (for example a literal <!-- x -->) still passes through as is. Escaping it would change the existing html-comments fixture, so I left it out.
  • A literal backslash right before < or & is still lost, because txt.js does not escape backslashes. That was already wrong before this change and is a separate gap.

Tests

  • New fixture test/functional/makemarkdown/cases/standard/escaping-entity-like-text.{html,md}. It fails on develop and passes with the fix, in both the jsdom and node projects.
  • npx vitest run locally (Windows, Node 24): 25 files, 3176 tests passed.
  • npx eslint .: 0 errors. The one warning is an existing one in src/subParsers/makehtml/decodeEntities.js.
  • I also ran a round-trip check: makeHtml(makeMarkdown(html)) must show the same text and the same tags as html. On 20,000 random strings of entity-like and tag-like text inside p, h2, li, em, a, blockquote and table cells, 8,308 failed before and 12 failed after. All 12 are the <! case above. Nothing that passed before fails now.

Validated with Claude Opus 5.5.

This branch has not been deployed

No deployments
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