wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-markdown/src/parser/inline/image.rs@main

raw
Date Commit Message Author Files + -
2026-02-23 21:59 beginning on port over some of the changes from markdown-ppp since the f... wayverd 11 527 15
...

1use nom::{
2    IResult, Parser,
3    bytes::complete::take_while,
4    character::complete::{char, multispace0},
5    combinator::opt,
6    sequence::{delimited, preceded},
7};
8
9use crate::{
10    ast::{Image, Inline},
11    parser::link_util::{link_destination, link_title},
12};
13
14// ![alt text](/url "title")
15pub(super) fn image<'a>() -> impl FnMut(&'a str) -> IResult<&'a str, Inline> {
16    move |input: &'a str| {
17        let (input, alt) = preceded(
18            char('!'),
19            delimited(char('['), take_while(|c| c != ']'), char(']')),
20        )
21        .parse(input)?;
22
23        let (input, (destination, title)) = delimited(
24            char('('),
25            (
26                preceded(multispace0, link_destination),
27                opt(preceded(multispace0, link_title)),
28            ),
29            preceded(multispace0, char(')')),
30        )
31        .parse(input)?;
32
33        Ok((
34            input,
35            Inline::Image(Image {
36                destination,
37                title,
38                alt: alt.to_owned(),
39            }),
40        ))
41    }
42}
43