Skip to main content

image

Function image 

Source
pub fn image(alt: &str, destination: &str, in_table_cell: bool) -> String
Expand description

A picture as an inline image: ![alt](destination).

The alt text is data from the document — whatever a person typed into a description box: several lines, blank lines, brackets. Written verbatim, a blank line ends the paragraph and the image reads back as literal text plus a stray paragraph, and an unbalanced ] ends the link text early. So the alt text is flattened to one line — every run of whitespace becomes one space, the ends trimmed — and the characters that are syntax inside link text are escaped: \, [, ], ` (a code span would swallow the closing bracket), < (raw HTML or an autolink), and an & that would start a character reference (&amp; would read back as &). Inside a table cell | is escaped too, since it would end the cell. * and _ are left alone: unmatched they are literal, matched they only style the text, and neither can end the image.

The destination is written by link_destination.

use unparser_shared::markdown::image;

assert_eq!(
    image("A view\n\nof the harbour", "img/1.png", false),
    "![A view of the harbour](img/1.png)"
);
assert_eq!(image("a [draft]", "my dir/a.png", false), r"![a \[draft\]](<my dir/a.png>)");
assert_eq!(image("x | y", "a.png", true), r"![x \| y](a.png)");