cmark icon indicating copy to clipboard operation
cmark copied to clipboard

Source position seems off

Open chriseidhof opened this issue 6 years ago • 9 comments

Give the following input:

X
    **p**

The cmark library reports (through cmark_node_get_start_column) that the strong node starts at line 2, column 1, and ends at column 5. I would have expected it to start at column 5. Is this a bug, or is my interpretation of start column wrong?

chriseidhof avatar Apr 24 '19 21:04 chriseidhof

Looks like this is related to the soft breaks:

image

honghaoz avatar Apr 24 '19 22:04 honghaoz

I'll refer this to @kivikakk who added inline source positions in #228.

jgm avatar Apr 25 '19 14:04 jgm

Thank you. I added a passing test case in #297, hopefully that makes it easier to get started (or if this is expected behavior, we can merge it as-is).

chriseidhof avatar Apr 25 '19 18:04 chriseidhof

For more information, I found that for md like

A
  B

For the 1st pass when generating the blocks. The container block's string content already skips the spaces. So the container block node has text A\nB\n not A\n B\n, that's probably why the source position is off for the lazy continuation lines.

honghaoz avatar Apr 26 '19 00:04 honghaoz

Related issue: https://github.com/commonmark/cmark/issues/204

honghaoz avatar Apr 26 '19 00:04 honghaoz

This issue becomes more pronouncedly odd when you try the inverse:

  A
B

produces

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document SYSTEM "CommonMark.dtd">
<document sourcepos="1:1-2:1" xmlns="http://commonmark.org/xml/1.0">
  <paragraph sourcepos="1:3-2:1">
    <text sourcepos="1:3-1:3" xml:space="preserve">A</text>
    <softbreak />
    <text sourcepos="2:3-2:3" xml:space="preserve">B</text>
  </paragraph>
</document>

The lazy continuation means we end up treating everything as though it started at column 3.

kivikakk avatar Apr 26 '19 05:04 kivikakk

@kivikakk I think that's because the first line has two leading spaces. If you try to change it to 3 or 1, you will see the start column of following lines changed.

I briefly went through the parsing logic: https://github.com/commonmark/cmark/blob/master/src/blocks.c#L706 this line advances the parser->offset and parser->column for the 2nd line.

https://github.com/commonmark/cmark/blob/master/src/blocks.c#L186-L187 this line only adds the skipped text to the container node.

For example:

  A
  B

The text of the paragraph node is changed from A\n B\n to A\nB\n

honghaoz avatar Apr 26 '19 19:04 honghaoz

Seems to me that the inline parser should receive a list of starting columns for each input line, which it can use to adjust source positions. This would avoid the need to strip whitespace (as in #298) which imposes a performance penalty. This adds a bit of complexity, which is why I didn't implement inline source positions originally.

jgm avatar Apr 28 '19 17:04 jgm

I would also like to see this feature, as it would allow me to easily extract sections from a bigger markdown file. E.g. API descriptions, where I wanna parse the general format of the documentation (API call definitions, parameter tables), but leave the description in markdown to be processed later on (if even necessary).

bibo38 avatar Jan 13 '23 22:01 bibo38