Skip to content

Support for including source spans on block nodes - #185

Merged
robinst merged 8 commits into
masterfrom
issue-1-source-positions-generic
Sep 8, 2020
Merged

robinst merged 8 commits into
masterfrom
issue-1-source-positions-generic

Conversation

@robinst

@robinst robinst commented Sep 8, 2020

Copy link
Copy Markdown
Collaborator
  • Answer for "Where in the source input (line/column position and length) does this block come from?"
  • Useful for things like editors that want to keep the input and rendered output scrolled to the same lines,
    or start editing on the block that was selected.
  • Use includeSourceSpans on Parser.Builder to enable, read data with Node.getSourceSpans

Fixes #1 for block nodes. Will look into inline nodes too after #180, which might require more API changes.

The previous attempt required every block parser to implement source
positions, which is not ideal. This one is for trying to see if we can
get it done inside DocumentParser.

Note that SourceSpan initially used 1-based indexes and
beginIndex/endIndex, but it was changed to 0-based and a length because:

0-based fits better with the existing API and other Java APIs. And
instead of having an endIndex, having a length would allow us to easily
extend the SourceSpan to include a character offset in addition to
line/column offset.
The block parsers will still need to be extended so that they can
communicate where the source started, in case they return positions
after marker characters. I think we can put that into BlockStart and
BlockContinue though.
This means that e.g. this:

```
 paragraph starting with a space
```

Will include the starting space in the source positions. I was initially
thinking it should not be included, but that makes a lot of things
harder, for not much gain IMO.

E.g. for a code block with 5 spaces:

```
     code
```

Where should the source position start, at index 0, 4, or 5? This
implements 0. 4 is kind of hard to implement due to tabs being treated
as 4-space tabstops.
Just better to not accidentally modify it.
@robinst
robinst merged commit cffd26b into master Sep 8, 2020
@robinst
robinst deleted the issue-1-source-positions-generic branch September 8, 2020 05:31
@Mi-La

Mi-La commented Nov 18, 2020

Copy link
Copy Markdown

@robinst Hi! Just a note. Knowing the position in source is also very helpful when you need to report warnings/errors.

E.g. I'm using HTMLRenderer and I'm trying to verify that links lead to "somewhere", and when the linked resource is missing, I would like to report a warning. But currently I've no chance to get source location from e.g. Link node. Looking forward for the new release! :-). Do you have any plans already?

Or is there any other workaround how to get position in source markdown? (I'm using lastest release 0.15.2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add source position/maps to AST

3 participants