apple/swift-markdown

Unexpected symbol link source range

Closed this issue · 2 comments

Summary

When parsing a symbol link with multiline content, the source range is unexpected, new lines are replaced by whitespaces.

For instance, parsing the following symbol link:

Test of a ``multi
line symbolink``

gives a symbol link with a range of 1:11-1:35. Which is the equivalent of:

Test of a ``multi line symbolink``

However, I would rather expect either:

  • A range of 1:11-2:17
  • An invalid symbol link

Steps to reproduce

let source = """
Test of a ``multi
line symbolink``
"""
let document = Document(parsing: source, options: .parseSymbolLinks)
print(document.debugDescription(options: .printEverything))
Document @1:1-2:17 Root #1 #0
└─ Paragraph @1:1-2:17 #1
   ├─ Text @1:1-1:11 #2 "Test of a "
   └─ SymbolLink @1:11-1:35 #3 destination: multi line symbolink

Notes

This is a suspected cause of a crash in DocC diagnostic formatter. apple/swift-docc#678 (comment)

Looks like the bug is in the upstream cmark 🤔

cmark_node_get_end_line(node) for the symbollink node is giving 1 instead of 2.

image

The issue seems to be adjust_subj_node_newlines in inlines.c

https://github.com/apple/swift-cmark/blob/3bc2f3e25df0cecc5dc269f7ccae65d0f386f06a/src/inlines.c#L402-L423

If we comment out the following lines, the output will become expected.

if (!(options & CMARK_OPT_SOURCEPOS)) {
  return;
}