microsoft / vscode-markdown-languageservice

The language service that powers VS Code's Markdown support, extracted so that it can be reused by other editors and tools
MIT License
408 stars 12 forks source link

Markdown footnote validation #100

Open mail2nnv opened 1 year ago

mail2nnv commented 1 year ago

Does this issue occur when all extensions are disabled?: Yes/No Yes

Steps to Reproduce:

  1. Create new markdown file
  2. Type a text:
    text[^1]
  3. See problems: (×) No link definition found: '^1' (link.no-such-reference) [Ln 1, Col 6] 2022-11-22 14_23_58-test md - 555 - Visual Studio Code
GeorgyKwe commented 1 year ago

Try separating footnotes and citations by one line.

mail2nnv commented 1 year ago

Try separating footnotes and citations by one line.

Nothing changed. 2022-11-24 11_34_58-test md - 555 - Visual Studio Code

benblank commented 1 year ago

I'm sure this needs a more thorough look from someone familiar with the parser, but a quick check suggests that this behavior is the result of a conflict between how reference links are parsed and how link definitions are parsed.

Specifically, link definitions are explicitly not recognized if the reference name starts with a caret. (Note the (?!\^).)

https://github.com/microsoft/vscode-markdown-languageservice/blob/984eeb799db77906dd14115837abba2534890369/src/languageFeatures/documentLinks.ts#L323

Conversely, reference links aren't subject to that exclusion. (Called out here as the "link def".)

https://github.com/microsoft/vscode-markdown-languageservice/blob/984eeb799db77906dd14115837abba2534890369/src/languageFeatures/documentLinks.ts#L295-L313

As a result, footnotes appear to be interpreted as [shorthand]-style reference links which simply happen to start with a caret. But because the matching footnote definition isn't recognized as a link definition, the language service sees this as a link without a definition.

By my read, a fix for this could be as simple as removing (?!\^) from definitionPattern. Unless there's something else in the way, that should allow the parser to find the link definition. It'll still think the footnote is a link, though, and I don't know whether that could cause other issues.

villeodell commented 1 year ago

Just for additional context, even though most people's use case for this is footnotes, the following minimal example is parsed fine by commonmark.js (see here)

[^1]

[^1]: /url

However, vscode complains with

No link definition found: '^1'(link.no-such-reference)

hellt commented 2 months ago

footnotes are often used in rich documentation sites powered by mkdocs and the likes. Would be great to see this fixed

a few examples with footnotes that are not captured as a link in the current vscode markdown lsp:

[`Makefile`][makefile][^1]

`make`[^4]

[`text`](url)[^5]