Skip to main content

link_destination

Function link_destination 

Source
pub fn link_destination(url: &str, in_table_cell: bool) -> String
Expand description

A link or image destination, written so that CommonMark reads back exactly url (§6.3).

The bare form is used when it can carry the URL: no spaces, no ASCII control characters, no < or >, and parentheses that balance — an unbalanced ) would end the destination early, a ( would leave it open. Anything else is written in the pointy-bracket form <...>, which takes spaces and any parentheses. A line ending cannot stand in either form; it is percent-encoded (%0A, %0D), the one place the URL is changed rather than escaped. In both forms a backslash that would otherwise escape the character after it — an ASCII punctuation character, or the end of the destination — is doubled; other backslashes (C:\dir) are literal and stay as they are. An & that would start a character reference is escaped, so ?a=1&amp;b stays as written. Inside a table cell | is escaped, since a GFM table splits its cells before it reads links. An empty URL is <>.

A link that is not in a table cell passes in_table_cell: false.

use unparser_shared::markdown::link_destination;

assert_eq!(link_destination("https://example.com/a(b)", false), "https://example.com/a(b)");
assert_eq!(link_destination("my folder/file.png", false), "<my folder/file.png>");
assert_eq!(link_destination("notes).txt", false), "<notes).txt>");
assert_eq!(link_destination("a<b>c", false), r"<a\<b\>c>");