syntax-tree / mdast-util-from-markdown

mdast utility to parse markdown
MIT License
212 stars 20 forks source link

Shorthand link references not recognized when there's whitespace between [] #39

Closed nzakas closed 1 month ago

nzakas commented 3 months ago

Initial checklist

Affected packages and versions

mdast-util-from-markdown@2.0.1

Link to runnable example

https://stackblitz.com/edit/stackblitz-starters-ifpvft?file=index.js

Steps to reproduce

  1. Create a link reference with whitespace in between the []:
[foo][ ]

[foo]: https://example.com
  1. Pass into fromMarkdown()

Expected behavior

The returned AST should identify [foo][ ] as a linkReference. (It appears that GitHub does this. I'm unsure if that's standard behavior for Markdown or not. The Micromark GFM extension exhibits the same behavior.)

Actual behavior

The returned AST identifies [foo][ ] as text rather than a link reference.

Affected runtime and version

node@20.13.0

Affected package manager and version

No response

Affected OS and version

No response

Build and bundle tools

No response

ChristianMurphy commented 3 months ago

Hey @nzakas! 👋 Commonmark specifies that spaces are significant and it would not match https://spec.commonmark.org/dingus/?text=%5Bfoo%5D%5B%20%5D%0A%0A%5Bfoo%5D%3A%20https%3A%2F%2Fexample.com

I don't see any spec changes in https://github.github.com/gfm/#link-reference-definitions to reference links This may be a bug in https://github.com/github/cmark-gfm

wooorm commented 3 months ago

This seems to be https://github.com/commonmark/commonmark-spec/issues/653, which shows that most tools work differently than the spec.

I think the spec is currently saying that this is not allowed:

I do consider this to be something that can improved in CM.

But I also consider this a current bug in GFM, which says it sticks to CM, so I don’t feel good about “solving” it in micromark-extension-gfm.

nzakas commented 3 months ago

To make sure I'm following, it sounds like you're saying that the spec disallows this behavior, so this is a bug in GitHub's Markdown parser?

wooorm commented 3 months ago

Correct

fasttime commented 3 months ago

I think that in this example, spaces and line breaks are not significant:

[foo][
  bar ]

[bar
]: https://example.com

https://spec.commonmark.org/dingus/?text=%5Bfoo%5D%5B%20%5D%0A%0A%5Bfoo%5D%3A%20https%3A%2F%2Fexample.com

But they are significant if there are only spacing characters?

wooorm commented 3 months ago

Correct. There’s indeed a “gap” between the different shortcut links and how they are defined in CM. Shortcut ([x]), collapsed ([x][]), vs. full ([x][y]). Making nzakas’s case ambiguous/vague/weird.

wooorm commented 1 month ago

Closing as behavior is intentional, I believe according to spec!

github-actions[bot] commented 1 month ago

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