-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Generated Markdown is missing line breaks between heading and image #59
Comments
Hi! Please follow the Duplicate of #58 |
This comment has been minimized.
This comment has been minimized.
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, |
Hi team! It seems this post is a duplicate, but hasn’t been marked as such. Please post a comment w/ Thanks, |
Duplicate of #58 |
Thanks for the quick response! I can confirm that wrapping the image in a paragraph solves the issue, but I noticed that this causes the image alt text to get escaped in a way I did not expect. I would have expected the escaping of the image alt text to be treated in a similar way to the title text. While this sounds like a separate issue, I thought it would make sense to bring it up in this context as I did not encounter this issue when not wrapping the image inside a paragraph (which I now understand is not spec compliant). Here is an example for reference: import {toMarkdown} from 'mdast-util-to-markdown'
const tree = {
type: 'root',
children: [
{ type: 'paragraph', children: [
{
type: 'image',
url: 'https://example.com/image.png',
alt: '*hello* [world]',
title: '*hello* "world"',
}
]},
]
}
console.log(toMarkdown(tree)) Output:
Expected:
Please let me know if the current image alt text escaping is by design or if I should open a separate issue for this. |
Your expected markdown doesn’t work. Try pasting here: ![\*hello\* \[world\]](https://example.com/image.png "*hello* \"world\"")
![*hello* [world\]](https://example.com/image.png "*hello* \"world\"") ![hello world]
Escaping is definitely by design. That being sad, as this case shows, escaping is incredibly hard. If it can be improved, that’s of course great, but maybe it can’t. |
If this is your reduced test case: ![*alpha*](xxx) and ![\*bravo\*](yyy) you should check out the Note that the |
Thanks for the additional details and examples. I did not expect image alt text to be interpreted as Markdown as the While the import {fromMarkdown} from 'mdast-util-from-markdown'
console.log(JSON.stringify(fromMarkdown('![*alt*](xxx "*title*")'), null, 2)) Output of image node: {
"type": "image",
"title": "*title*",
"url": "xxx",
"alt": "alt"
}
I am wondering if it actually makes sense to interpret the image alt text as Markdown if the information gets discarded and is not even part of the generated AST. The only use case I could come up with so far, would be for Markdown syntax highlighting where the alt text of an image could be rendered in a stylized way. But when for example trying this in Visual Studio Code, the image alt text does not seem to be interpreted as Markdown and instead as simple text (which also matches my initial expectation): As you can see in the screenshot, the alt text of the image does not use italics or bold while the content of the link does. At the same time it looks like the current logic matches GitHub's behavior, which might be reason enough to keep it as is. |
This isn’t about This is about how markdown works. Markdown prescribes this. CommonMark-compliant markdown processors do this. On the parsing side, this is because links, images, but also emphasis, are not parsed the way you think. If such a valid closing is found (e.g., So you see, this parsing is already done. Only afterwards do we know that these things occurred in an image. |
More on how parsing works in https://github.com/wooorm/markdown-rs/blob/main/src/construct/label_start_image.rs and https://github.com/wooorm/markdown-rs/blob/main/src/construct/label_end.rs. |
Thanks for the additional explanation and the links. I wasn't aware of how much of the image description logic is already defined in the CommonMark spec. That's very helpful! I am finally able to understand why escaping in the image description is absolutely required as it supports much more than just simple text. Reading the following in the CommonMark spec:
I think trying to more closely reflect this spec in the
I think this AST would be closer to the spec and it would be up to the renderer to decide how to best render the spec compliant image description as HTML. The renderer could follow the recommendation and use an I assume that this approach has already been evaluated and I can see how a simpler AST might be the better overall trade-off if the majority of real-world cases involve the image description being set to a simple text anyways. But only exposing a simple |
:) A bit about the spec: it’s also a bit of a vague document. It’s nice that it provides many examples, more than we had before, but there are still many edge cases. So we still need to look at what parsers actually do: how does GFM for example actually work? Yeah, I’ve thought about adding One alternative is to remove Perhaps tho, you have interesting needs, for such rich content. By hearing them I might be able to advise better? E.g., perhaps you can use directives, which do allow rich content, and compile to some other element that has a |
I agree with the downside of having two sources of truths and I like the simplicity of the current AST. My needs are actually quite simple, which is why I would personally prefer the image description to simply work like the I guess the goal of the current image description spec was to make it consistent with the way links work as the syntax is so similar. So I think I understand the trade-off that was being made in the spec even if there might not be that many good use cases for all these inline elements in the image description. Or maybe there are common use cases that I just haven't encountered yet. After having thought through several use cases and now having a better understanding of the CommonMark spec, I think you picked really good trade-offs for this library. The result is a spec compliant system, which hides some of the flexibility/complexity of CommonMark image descriptions while making it very easy to use for the 99% of cases where a simple Thanks so much for taking the time to answer all my questions and helping me better understand the design decisions of this library :) |
Initial checklist
Affected packages and versions
mdast-util-to-markdown 1.5.0
Link to runnable example
No response
Steps to reproduce
Expected behavior
It should log the following output:
Actual behavior
It logs the following output:
It looks like this issue was introduced with this commit: 122101f
Affected runtime and version
[email protected]
Affected package manager and version
No response
Affected OS and version
No response
Build and bundle tools
No response
The text was updated successfully, but these errors were encountered: