Record Class MarkdownLink

java.lang.Object
java.lang.Record
com.google.common.labs.markdown.MarkdownLink

public record MarkdownLink(String label, String url) extends Record
Represents a markdown link in the format of [label](url).

This class offers a light-weight parser to quickly extract markdown links from a markdown text or file.

While roughly equivalent to


 import com.google.mu.util.StringFormat;

 new StringFormat("[{label}]({url})")
     .scan(markdown, MarkdownLink::new);
 
The parser properly handles escaping inside and outside of the link; nesting within the link label and link url; and won't mistakenly extract link-like syntax from backtick-quoted code or code blocks (recognizing code blocks quoted by single backtick, double, triple or any number of consecutive backticks).
Since:
10.3
  • Field Details

  • Constructor Details

    • MarkdownLink

      public MarkdownLink(String label, String url)
      Creates an instance of a MarkdownLink record class.
      Parameters:
      label - the value for the label record component
      url - the value for the url record component
  • Method Details

    • of

      public static MarkdownLink of(String link)
      Parses link into a MarkdownLink.
      Throws:
      NullPointerException - if link is null
      IllegalArgumentException - if parsing failed
    • scan

      public static Stream<MarkdownLink> scan(String markdown)
      Scans markdown and lazily extracts all markdown links.
      Throws:
      NullPointerException - if markdown is null
    • scan

      public static Stream<MarkdownLink> scan(Reader markdown)
      Scans markdown and lazily extracts all markdown links.
      Throws:
      NullPointerException - if markdown is null
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • label

      public String label()
      Returns the value of the label record component.
      Returns:
      the value of the label record component
    • url

      public String url()
      Returns the value of the url record component.
      Returns:
      the value of the url record component