-
-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non isomorphic parsing/formatting for bold/italic with spaces #908
Comments
To provide a bit more context, in our application users can select text which leading/trailing spaces and format it as bold/italic, basically something like: hello<bold> world </bold>! It was leading to issues when generating markdown with remark, because the following is not a valid markdown: hello** world **! So we implemented a custom logic to trim the inner content and move the spaces outside the bold/italic and other marks. But it can lead to more complex tree and remark generated the following markdown:
that it can't parse after. I'm seeing 2 issues:
|
Likely related to syntax-tree/mdast-util-to-markdown#12 |
I dunno on the first point. Your code here is generating an object model that is impossible to make with markdown syntax. Take the DOM: p = document.createElement('p')
h1 = document.createElement('h1')
h1.textContent = 'Hi!'
p.append(h1)
p.outerHTML // "<p><h1>Hi!</h1></p>"
d = document.createElement('div')
d.innerHTML = p.outerHTML;
d.outerHTML // "<div><p></p><h1>Hi!</h1><p></p></div>" Especially with a vague language like markdown, I think there will always be cases that can easily be represented by JSON but are impossible to serialize/parse. If you’re generating
Sure! Minimal repro: |
Yes, I was wondering if the case of trimming spaces in bold/italic should be something handled by remark or not. Maybe it's something we can implement as a plugin, similar to the Because I can imagine the confusion when the following tree generates an invalid markdown: {
type: 'paragraph',
children: [
{
type: 'strong',
children: [
{
type: 'text',
value: 'Hello ',
},
],
}
]
}
Yes, I'm looking at improving this on our side in our step which is going from our AST into the remark AST. |
What do you care most about? That it’s readable markdown? Or that it works? There might be something to be done in CommonMark, e.g., And a plugin as you mention might indeed be useful to a lot of folks. Alternatively, inject HTML instead. |
I came up with a way to do it, I think: syntax-tree/unist#60 (comment). |
This was fixed in https://github.com/syntax-tree/mdast-util-to-markdown/releases/tag/2.1.1: import {remark} from 'remark'
const sourceMarkdown = `
**Our **_**developer**_** guides** and APIs have a home of their own now.
`;
const markdown1 = String(
await remark().process(sourceMarkdown)
);
const markdown2 = String(
await remark().process(markdown1)
);
console.log(markdown1 === markdown2) // `true` Thanks :) |
Initial checklist
Affected packages and versions
[email protected]
Link to runnable example
https://codesandbox.io/s/cocky-meitner-88li6
Steps to reproduce
To reproduce, parse the following markdown:
Expected behavior
This markdown snippet works on GitHub:
Actual behavior
The markdown snipped is being reprocessed at:
Runtime
Node v14
Package manager
yarn v2
OS
Linux, macOS
Build and bundle tools
esbuild
The text was updated successfully, but these errors were encountered: