Skip to content

Include reference link identifier in the events #434

Closed

Description

@Gozala

Currently parsing following markdown content

[link][link-id]

[link-id]:https://my.link/foo "my link"

outputs following events:

Start(Paragraph)
Start(Link(Reference, Borrowed("https://my.link/foo"), Borrowed("my link")))
Text(Borrowed("link"))
End(Link(Reference, Borrowed("https://my.link/foo"), Borrowed("my link")))
End(Paragraph)

In my use case I would like to know the reference link identifier, in this example link-id and would like to propose changing output to produce following events instead:

Start(Paragraph)
Start(Link(Reference(Borrow("link-id")), Borrowed("https://my.link/foo"), Borrowed("my link")))
Text(Borrowed("link"))
End(Link(Reference(Borrow("link-id")), Borrowed("https://my.link/foo"), Borrowed("my link")))
End(Paragraph)

Unfortunately due to #430 emitted ranges also can not be used to capture reference identifiers.

Activity

  1. Gozala commented on Mar 29, 2020

    @Gozala
    ContributorAuthor

    I have started digging into the code base to try and implement proposed change (I am however new to Rust so it's not going all that fast), it would be good to know if what's proposed does make sense and if it would be welcomed.

    In the meantime I'll try to post comments as I struggle through in a hope to get some insights.

  2. Gozala commented on Mar 29, 2020

    @Gozala
    ContributorAuthor

    My current changes are:

    diff --git a/src/parse.rs b/src/parse.rs
    index 5ec712b..6fa4044 100644
    --- a/src/parse.rs
    +++ b/src/parse.rs
    @@ -99,41 +99,41 @@ pub enum Tag<'a> {
         Strikethrough,
     
         /// A link. The first field is the link type, the second the destination URL and the third is a title.
    -    Link(LinkType, CowStr<'a>, CowStr<'a>),
    +    Link(LinkType<'a>, CowStr<'a>, CowStr<'a>),
     
         /// An image. The first field is the link type, the second the destination URL and the third is a title.
    -    Image(LinkType, CowStr<'a>, CowStr<'a>),
    +    Image(LinkType<'a>, CowStr<'a>, CowStr<'a>),
     }
     
     /// Type specifier for inline links. See [the Tag::Link](enum.Tag.html#variant.Link) for more information.
    -#[derive(Clone, Debug, PartialEq, Copy)]
    -pub enum LinkType {
    +#[derive(Clone, Debug, PartialEq)]
    +pub enum LinkType<'a> {
         /// Inline link like `[foo](bar)`
         Inline,
         /// Reference link like `[foo][bar]`
    -    Reference,
    +    Reference(CowStr<'a>),
         /// Reference without destination in the document, but resolved by the broken_link_callback
    -    ReferenceUnknown,
    +    ReferenceUnknown(CowStr<'a>),
         /// Collapsed link like `[foo][]`
    -    Collapsed,
    +    Collapsed(CowStr<'a>),
         /// Collapsed link without destination in the document, but resolved by the broken_link_callback
    -    CollapsedUnknown,
    +    CollapsedUnknown(CowStr<'a>),
         /// Shortcut link like `[foo]`
    -    Shortcut,
    +    Shortcut(CowStr<'a>),
         /// Shortcut without destination in the document, but resolved by the broken_link_callback
    -    ShortcutUnknown,
    +    ShortcutUnknown(CowStr<'a>),
         /// Autolink like `<http://foo.bar/baz>`
         Autolink,
         /// Email address in autolink like `<[email protected]>`
         Email,
     }
     
    -impl LinkType {
    +impl<'a> LinkType<'a> {
         fn to_unknown(self) -> Self {
             match self {
    -            LinkType::Reference => LinkType::ReferenceUnknown,
    -            LinkType::Collapsed => LinkType::CollapsedUnknown,
    -            LinkType::Shortcut => LinkType::ShortcutUnknown,
    +            LinkType::Reference(id) => LinkType::ReferenceUnknown(id),
    +            LinkType::Collapsed(id) => LinkType::CollapsedUnknown(id),
    +            LinkType::Shortcut(id) => LinkType::ShortcutUnknown(id),
                 _ => unreachable!(),
             }
         }
    @@ -1849,7 +1849,7 @@ struct AlignmentIndex(usize);
     #[derive(Clone)]
     struct Allocations<'a> {
         refdefs: HashMap<LinkLabel<'a>, LinkDef<'a>>,
    -    links: Vec<(LinkType, CowStr<'a>, CowStr<'a>)>,
    +    links: Vec<(LinkType<'a>, CowStr<'a>, CowStr<'a>)>,
         cows: Vec<CowStr<'a>>,
         alignments: Vec<Vec<Alignment>>,
     }
    @@ -1870,7 +1870,7 @@ impl<'a> Allocations<'a> {
             CowIndex(ix)
         }
     
    -    fn allocate_link(&mut self, ty: LinkType, url: CowStr<'a>, title: CowStr<'a>) -> LinkIndex {
    +    fn allocate_link(&mut self, ty: LinkType<'a>, url: CowStr<'a>, title: CowStr<'a>) -> LinkIndex {
             let ix = self.links.len();
             self.links.push((ty, url, title));
             LinkIndex(ix)
    @@ -1892,7 +1892,7 @@ impl<'a> Index<CowIndex> for Allocations<'a> {
     }
     
     impl<'a> Index<LinkIndex> for Allocations<'a> {
    -    type Output = (LinkType, CowStr<'a>, CowStr<'a>);
    +    type Output = (LinkType<'a>, CowStr<'a>, CowStr<'a>);
     
         fn index(&self, ix: LinkIndex) -> &Self::Output {
             self.links.index(ix.0)
    @@ -2151,8 +2151,8 @@ impl<'a> Parser<'a> {
                                     RefScan::Failed => next,
                                 };
                                 let link_type = match &scan_result {
    -                                RefScan::LinkLabel(..) => LinkType::Reference,
    -                                RefScan::Collapsed(..) => LinkType::Collapsed,
    +                                RefScan::LinkLabel(id, _) => LinkType::Reference(id),
    +                                RefScan::Collapsed(..) => LinkType::Collapsed(????),
                                     RefScan::Failed => LinkType::Shortcut,
                                 };
     

    So far I'm unable to figure out how to get identifiers for shortcut links ([foo]) or collapsed links ([foo][]).

  3. Gozala commented on Mar 29, 2020

    @Gozala
    ContributorAuthor

    I'm starting to wonder if it would make more sense to hoist link type to the Tag instead of introducing lifetime to the LinkType, because markdown does have two kind Inline and Reference links and other than (Autolink and Email) all the LinkTypes are of Reference kind. With that definition of Tag would change to:

    /// Tags for elements that can contain other elements.
    #[derive(Clone, Debug, PartialEq)]
    pub enum Tag<'a> {
        /// A paragraph of text and other inline elements.
        Paragraph,
    
        /// A heading. The field indicates the level of the heading.
        Heading(u32),
    
        BlockQuote,
        /// A code block.
        CodeBlock(CodeBlockKind<'a>),
    
        /// A list. If the list is ordered the field indicates the number of the first item.
        /// Contains only list items.
        List(Option<u64>), // TODO: add delim and tight for ast (not needed for html)
        /// A list item.
        Item,
        /// A footnote definition. The value contained is the footnote's label by which it can
        /// be referred to.
        FootnoteDefinition(CowStr<'a>),
    
        /// A table. Contains a vector describing the text-alignment for each of its columns.
        Table(Vec<Alignment>),
        /// A table header. Contains only `TableRow`s. Note that the table body starts immediately
        /// after the closure of the `TableHead` tag. There is no `TableBody` tag.
        TableHead,
        /// A table row. Is used both for header rows as body rows. Contains only `TableCell`s.
        TableRow,
        TableCell,
    
        // span-level tags
        Emphasis,
        Strong,
        Strikethrough,
    
        // An inline link. The first field is the link type, the second the destination URL and the third is a title.
        InlineLink(InlineLinkType, CowStr<'a>, CowStr<'a>),
        // An reference link. The first field is the link type, the second the reference identifier, the third destination URL and the fourth is a title.
        ReferenceLink(ReferenceLinkType, CowStr<'a>, CowStr<'a>, CowStr<'a>),
      
    
        /// An image. The first field is the link type, the second the destination URL and the third is a title.
        InlineImage(InlineLinkType, CowStr<'a>, CowStr<'a>),
        /// An image. The first field is the link type, the second the reference identifier,the third the destination URL and the fourth is a title.
        ReferenceImage(ReferenceLinkType, CowStr<'a>, CowStr<'a>, CowStr<'a>),
    }
    
    #[derive(Clone, Debug, PartialEq, Clone)]
    pub enum InlineLinkType {
        /// Inline link like `[foo](bar)`
        Inline,
        /// Autolink like `<http://foo.bar/baz>`
        Autolink,
        /// Email address in autolink like `<[email protected]>`
        Email,
    }
    
    #[derive(Clone, Debug, PartialEq, Clone)]
    pub enum ReferenceLinkType {
        /// Reference link like `[foo][bar]`
        Reference,
        /// Reference without destination in the document, but resolved by the broken_link_callback
        ReferenceUnknown,
        /// Collapsed link like `[foo][]`
        Collapsed,
        /// Collapsed link without destination in the document, but resolved by the broken_link_callback
        CollapsedUnknown,
        /// Shortcut link like `[foo]`
        Shortcut,
        /// Shortcut without destination in the document, but resolved by the broken_link_callback
        ShortcutUnknown,
    }

    However this seems like a change that is likely to break more code downstream & the fact that it also breaks images into two categories also doesn't seem ideal.

  4. alerque commented on May 20, 2020

    @alerque
    Contributor

    I think it does make sense to move this info to the Link Tag and not saddle LinkType with it.

  5. ehuss commented on Aug 29, 2022

    @ehuss
    Contributor

    I want to add a use case mentioned in #217 where it would be nice to be able to detect unused references. It would need to have some way of connecting links to their references. I'm not sure if the changes proposed here would be sufficient for that, but it is something to consider.

  6. andrewbaxter commented on Dec 18, 2022

    @andrewbaxter

    This is also pretty important for markdown round tripping.

  7. moved this from Todo to In Progress in pulldown-cmark 0.10on May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions