syntax-tree / mdast-util-to-markdown

mdast utility to serialize markdown
http://unifiedjs.com
MIT License
100 stars 20 forks source link

Needless escapes in Markdown since 2.1.1 #66

Closed kawanet closed 1 week ago

kawanet commented 1 week ago

Initial checklist

Affected packages and versions

mdast-util-to-markdown

Link to runnable example

No response

Steps to reproduce

import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {toHast} from 'mdast-util-to-hast'
import {toHtml} from 'hast-util-to-html'

const md = `foo***bar***buz`;
console.log(md) // original
console.log(toMarkdown(fromMarkdown(md))) // round trip
console.log(toHtml(toHast(fromMarkdown(md)))) // HTML

Expected behavior

until mdast-util-to-markdown version 2.1.0, or commit df0d6a6, it works correctly. toMarkdown(fromMarkdown(md)) runs the round trip from Markdown to Markdown.

foo***bar***buz
foo***bar***buz
<p>foo<em><strong>bar</strong></em>buz</p>

Actual behavior

since mdast-util-to-markdown version 2.1.1, or commit 97fb8181, it made characters escaped needlessly just before and after both * and **. toMarkdown(fromMarkdown(md)) cannot run the round trip from Markdown to Markdown. toHtml(toHast(fromMarkdown(md))) still has no trouble on the other hand.

foo***bar***buz
fo&#x6F;***bar***&#x62;uz
<p>foo<em><strong>bar</strong></em>buz</p>

It means version 2.1.1 has a breaking change. Version 2.1.2 has the same issue. Thank you maintaining this anyway.

Affected runtime and version

node@22.11.0

Affected package manager and version

No response

Affected OS and version

No response

Build and bundle tools

No response

wooorm commented 1 week ago

Hi! This looks like a duplicate of #65!

github-actions[bot] commented 1 week ago

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

github-actions[bot] commented 1 week ago

Hi! Thanks for taking the time to contribute!

Because we treat issues as our backlog, we close duplicates to focus our work and not have to touch the same chunk of code for the same reason multiple times. This is also why we may mark something as duplicate that isn’t an exact duplicate but is closely related.

Thanks, — bb

github-actions[bot] commented 1 week ago

Hi team! It seems this post is a duplicate, but hasn’t been marked as such. Please post a comment w/ Duplicate of #123(no final .) to do so. See GH docs for more info.

Thanks, — bb

kawanet commented 1 week ago

Thank you for pointing me to the related issue. My actual production app is to generate Markdown from mdast object without parsing Markdown. I'll then try to update the app to add leading/trailing spaces around strong nodes to avoid the needless escaping.

The commit 97fb818 might be able to give the new minor version number 2.2.0 with the breaking change, by the way.

wooorm commented 1 week ago

It depends on what you do, what I’d recommend. If you can generate spaces, I would indeed strongly recommend them.

Note that I do not consider escaping things a breaking change; that markdown still renders the same, and the HTML output is still the same. That wasn’t the case before. That escape is not needed in some cases. But it is needed in other cases. This patch fixes actual bugs!