Skip to content

Preserve link reference definitions in parser output #423

Closed

Description

@dsherret

Hello,

I'm using pulldown-cmark to create a markdown code formatter. So far it's been working well and thanks for this library!

I've hit a bit of a hurdle though because the parser does not have events for link reference definitions.

For example, given the following text:

[testing][Some reference]

[Some reference]: https://github.com

testing

The parser output is the following:

Event::Start(Paragraph)
Event::Start(Link(Reference, Borrowed("https://github.com"), Borrowed("")))
Event::Text(Borrowed("testing"))
Event::End(Link(Reference, Borrowed("https://github.com"), Borrowed("")))
Event::End(Paragraph)
Event::Start(Paragraph)
Event::Text(Borrowed("testing"))
Event::End(Paragraph)

Is it possible to know that the reference definition appeared between the two paragraphs? Worst case scenario, I will just parse this information out of the file myself.

Thanks!

Activity

  1. changed the title [-]Preserve link reference definitions in parser[/-] [+]Preserve link reference definitions in parser output[/+] on Feb 2, 2020
  2. ScottAbbey commented on Feb 3, 2020

    @ScottAbbey
    Collaborator

    create a markdown code formatter.

    Aha. I was wondering what purpose this request could have. Then I realized you meant this is for markdown-to-markdown, not "formatting code blocks within markdown".

    In addition to where the link reference definition was, wouldn't you also want to know what the reference name was? Currently "Some reference" does not appear in the event list you showed.

    It seems like there could be some other requests alongside this, like:

    • Know more info about a list that was encountered...such as which marker or delimiter, and if it is tight or loose.
    • There is probably some situation where you would want to know whether or not there was a blank line in there somewhere
    • Was this Heading an ATX heading or a setext heading?

    So, it seems the request is more along the lines of "Can we have significantly more detail in Event?" Stuffing extra things into Event is probably going to have some sort of performance cost, maybe that is where the discussion would need to start?


    I will just parse this information out of the file myself.

    This would be relatively complex, see scan_refdef()...it's not the simplest function out there.

  3. dsherret commented on Feb 3, 2020

    @dsherret
    Author

    @ScottAbbey I think these details could be omitted by default and only returned when an option is specified... something like options.insert(Options::INCLUDE_LINK_DEFINITIONS);.

    Alternatively, perhaps they could be provided by request off the Parser object (and include their ranges in the document so I can know when to deal with them). That might be a bit better because then it wouldn't mean everyone else has to ignore some link reference event.

    In addition to where the link reference definition was, wouldn't you also want to know what the reference name was? Currently "Some reference" does not appear in the event list you showed.

    Yes, ideally, but that and the other scenarios you mentioned are not too big of a deal for me. The into_offset_iter() method provides me with the ranges of each event so I can manually check those things. Also, it's slightly easier to parse things out manually a second time when an assumption can be made because it was parsed correctly the first time.

  4. Gozala commented on Apr 6, 2020

    @Gozala
    Contributor

    Quoting another use case for separating reference links and reference definitions (from #435)

    Please consider following example:

    # Test
    [ref-link][link-id]
    [shortcut]
    [collapse][]
    
    
    [link-id]:https://my.link/foo "my link"
    [shortcut]:./shortcut
    [collapse]:https://my.link/collapse
    [obsolete]:https://my.link/obsolete-link
    

    Last link definition (obsolete) is not present in the parser output in any way. In my use case I would still like to know about such links so I could surface those in some way. Maybe there could be an option for the parser which would emit events for link definitions when user opts-in ?

    Even better (but more intrusive) alternative might be to make Tag::Links refer to it's definition. It could be either Inline, Autolink, Emailor a namedDefinition`. Although it does not appear to fit well current design.

  5. Gozala commented on Apr 6, 2020

    @Gozala
    Contributor

    I think it is worth considering how currently Event::FootnoteReference(CowStr<'a>) and Tag::FootnoteDefinition(CowStr<'a>) are decoupled from each other and how one can be present without the other.

    That is pretty much what I believe we need for link / image reference and their reference definitions.

  6. bugadani commented on Jan 1, 2021

    @bugadani

    cc @jyn514

    rustdoc reports broken intra-doc links and tries to highlight the link definition itself when printing the error. When using reference-style links, Pulldown's offset iterator provides a range that marks the link usage, not the definition. Because of this, we can only provide an accurate and useful error message when Pulldown gives us a Borrowed link destination, where we can calculate the span of the link definition via pointer arithmagic. Unfortunately, this requires rustdoc to include a bunch of ugly unsafe workaround code that could be avoided if Pulldown provided more info about reference-style link definitions.

    An example of an error we consider inaccurate and not ideal:

    // Test that errors point to the reference, not to the title text.
    #![deny(broken_intra_doc_links)]
    //! Link to [a]
    //!
    //! [a]: std::process::Comman
    
    warning: unresolved link to `std::process::Comman`
     --> link.rs:1:14
      |
    1 | //! Link to [a]
      |             ^^^ no item named `Comman` in module `process`
      |
      = note: `#[warn(broken_intra_doc_links)]` on by default
    

    I believe the ideal case for rustdoc would be that Pulldown's offset iterator provide an (Event::LinkDefinition, "range pointing at the link definition") pair.

  7. jyn514 commented on Jan 1, 2021

    @jyn514
    Contributor

    I believe the ideal case for rustdoc would be that Pulldown's offset iterator provide an (Event::LinkDefinition, "range pointing at the link definition") pair.

    Hmm, well that second span only makes sense for reference links; pulldown would have to make something up the rest of the time. I think it makes sense to put it on LinkType, so that Reference and Shortcut variants always have a span, but don't force other events to have a second one.

  8. marcusklaas commented on Jan 1, 2021

    @marcusklaas
    Collaborator

    Currently the span of reference definitions are not even stored internally, so they would have to be recorded first. Then there's the question of how to relay this span information. Definition spans could be tacked onto Reference and Shortcut link events, or we could simply enumerate the link definitions and their spans as separate events. Maybe we could also expose a reference to the internal reference definition hashmap.

  9. jyn514 commented on Jan 1, 2021

    @jyn514
    Contributor

    Separate events sound fine to me. I'd be fine with the hashmap if it has a span, but that might be a little limiting for pulldown if it wants to use a different internal reprentation in the future.

  10. bugadani commented on Jan 1, 2021

    @bugadani

    @jyn514 I must have explained myself poorly, I would prefer a new event akin to FootnoteDefinition in addition to the current set of events.

    I think what would fit into the current mindset of Pulldown is:

    My concern with this approach would be the ordering of the events - definitions must precede usage events, which means the emitted events won't align with how they are described in the markdown source.

    Accessing the refdef hashmap would also work, but it feels like a hack. I don't think Pulldown exposes any other data, only the events. Granted, what I have in mind necessitates us rebuilding the refdef hashmap anyway, externally from the captured events.

  11. marcusklaas commented on Jan 1, 2021

    @marcusklaas
    Collaborator

    It would probably be best to expose the definitions as a separate iterator, both to evade the ordering problem you mention and not to emit events that many renderers would have no direct use for.

  12. marcusklaas commented on Jan 2, 2021

    @marcusklaas
    Collaborator

    I've sent a pull request to implement access to the reference definition tracker. It provides lookup access, so you shouldn't have to rebuild your own hashmap in most cases. As mentioned by @bugadani, it will be more useful when #434 is addressed, but this should be a good start.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions