You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Preserve link reference definitions in parser output #423
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.
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.
@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.
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.
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.
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.
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.
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.
add a new event that can be used to map the link's name to its definition
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.
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.
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.
Activity
[-]Preserve link reference definitions in parser[/-][+]Preserve link reference definitions in parser output[/+]ScottAbbey commentedon Feb 3, 2020
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:
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?
This would be relatively complex, see
scan_refdef()...it's not the simplest function out there.dsherret commentedon Feb 3, 2020
@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
Parserobject (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.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.Gozala commentedon Apr 6, 2020
Quoting another use case for separating reference links and reference definitions (from #435)
Gozala commentedon Apr 6, 2020
I think it is worth considering how currently
Event::FootnoteReference(CowStr<'a>)andTag::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.
bugadani commentedon Jan 1, 2021
cc @jyn514
rustdocreports 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 aBorrowedlink destination, where we can calculate the span of the link definition via pointer arithmagic. Unfortunately, this requiresrustdocto 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:
I believe the ideal case for
rustdocwould be that Pulldown's offset iterator provide an (Event::LinkDefinition, "range pointing at the link definition") pair.jyn514 commentedon Jan 1, 2021
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 thatReferenceandShortcutvariants always have a span, but don't force other events to have a second one.marcusklaas commentedon Jan 1, 2021
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
ReferenceandShortcutlink 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.jyn514 commentedon Jan 1, 2021
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.
bugadani commentedon Jan 1, 2021
@jyn514 I must have explained myself poorly, I would prefer a new event akin to
FootnoteDefinitionin 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.
marcusklaas commentedon Jan 1, 2021
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.
marcusklaas commentedon Jan 2, 2021
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.