wayver's git archive


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

sable-markdown/src/parser/blocks/tests/link_definition.rs@337ba67f65eaa17b44e371af7c0f0c761d6aa914

raw
Date Commit Message Author Files + -
2026-02-23 01:55 initial mvp wayverd 139 17808 0
...

1use crate::{ast::*, parser::parse_markdown};
2
3#[test]
4fn link_definition1() {
5    let doc = parse_markdown("[foo]: /url \"title\"").unwrap();
6    assert_eq!(
7        doc,
8        Document {
9            blocks: vec![Block::Definition(LinkDefinition {
10                label: vec![Inline::Text("foo".to_owned())],
11                destination: "/url".to_owned(),
12                title: Some("title".to_owned())
13            })]
14        }
15    );
16}
17
18#[test]
19fn link_definition2() {
20    let doc = parse_markdown(
21        "   [foo]: 
22      /url  
23           'the title'
24",
25    )
26    .unwrap();
27    assert_eq!(
28        doc,
29        Document {
30            blocks: vec![Block::Definition(LinkDefinition {
31                label: vec![Inline::Text("foo".to_owned())],
32                destination: "/url".to_owned(),
33                title: Some("the title".to_owned())
34            })]
35        }
36    );
37}
38
39#[test]
40fn link_definition3() {
41    let doc = parse_markdown("[Foo*bar\\]]:my_(url) 'title (with parens)'").unwrap();
42    assert_eq!(
43        doc,
44        Document {
45            blocks: vec![Block::Definition(LinkDefinition {
46                label: vec![Inline::Text("Foo*bar]".to_owned())],
47                destination: "my_(url)".to_owned(),
48                title: Some("title (with parens)".to_owned())
49            })]
50        }
51    );
52}
53
54#[test]
55fn link_definition4() {
56    let doc = parse_markdown(
57        "[Foo bar]:
58<my url>
59'title'",
60    )
61    .unwrap();
62    assert_eq!(
63        doc,
64        Document {
65            blocks: vec![Block::Definition(LinkDefinition {
66                label: vec![Inline::Text("Foo bar".to_owned())],
67                destination: "my url".to_owned(),
68                title: Some("title".to_owned())
69            })]
70        }
71    );
72}
73
74#[test]
75fn link_definition5() {
76    let doc = parse_markdown(
77        "[foo]: /url '
78title
79line1
80line2
81'",
82    )
83    .unwrap();
84    assert_eq!(
85        doc,
86        Document {
87            blocks: vec![Block::Definition(LinkDefinition {
88                label: vec![Inline::Text("foo".to_owned())],
89                destination: "/url".to_owned(),
90                title: Some("\ntitle\nline1\nline2\n".to_owned())
91            })]
92        }
93    );
94}
95
96#[test]
97fn link_definition6() {
98    let doc = parse_markdown(
99        "[foo]:
100/url",
101    )
102    .unwrap();
103    assert_eq!(
104        doc,
105        Document {
106            blocks: vec![Block::Definition(LinkDefinition {
107                label: vec![Inline::Text("foo".to_owned())],
108                destination: "/url".to_owned(),
109                title: None
110            })]
111        }
112    );
113}
114
115#[test]
116fn link_definition7() {
117    let doc = parse_markdown("[foo]: <>").unwrap();
118    assert_eq!(
119        doc,
120        Document {
121            blocks: vec![Block::Definition(LinkDefinition {
122                label: vec![Inline::Text("foo".to_owned())],
123                destination: "".to_owned(),
124                title: None
125            })]
126        }
127    );
128}
129
130// #[test]
131// fn link_definition_mapped1() {
132//     let config = MarkdownParserConfig::default().with_block_link_definition_behavior(
133//         ElementBehavior::Map(Rc::new(RefCell::new(Box::new(|block| {
134//             if let Block::Definition(v) = block {
135//                 let mut label = vec![Inline::Text("mapped ".to_owned())];
136//                 label.extend(v.label);
137//                 Block::Definition(LinkDefinition {
138//                     label,
139//                     destination: format!("mapped {}", v.destination),
140//                     title: v.title.map(|t| format!("mapped {t}")),
141//                 })
142//             } else {
143//                 block
144//             }
145//         })))),
146//     );
147//     let doc = parse_markdown(
148//         MarkdownParserState::with_config(config),
149//         "[foo]: /url \"title\"",
150//     )
151//     .unwrap();
152//     assert_eq!(
153//         doc,
154//         Document {
155//             blocks: vec![Block::Definition(LinkDefinition {
156//                 label: vec![
157//                     Inline::Text("mapped ".to_owned()),
158//                     Inline::Text("foo".to_owned())
159//                 ],
160//                 destination: "mapped /url".to_owned(),
161//                 title: Some("mapped title".to_owned())
162//             })]
163//         }
164//     );
165// }
166
167// #[test]
168// fn link_definition_mapped2() {
169//     let config = MarkdownParserConfig::default().with_block_link_definition_behavior(
170//         ElementBehavior::FlatMap(Rc::new(RefCell::new(Box::new(|block| {
171//             if let Block::Definition(v) = block {
172//                 let mut label = vec![Inline::Text("mapped ".to_owned())];
173//                 label.extend(v.label);
174//                 let link1 = Block::Definition(LinkDefinition {
175//                     label: label.clone(),
176//                     destination: format!("mapped {}", v.destination),
177//                     title: v.title.as_ref().map(|t| format!("mapped1 {t}")),
178//                 });
179//                 let link2 = Block::Definition(LinkDefinition {
180//                     label,
181//                     destination: format!("mapped {}", v.destination),
182//                     title: v.title.map(|t| format!("mapped2 {t}")),
183//                 });
184//                 vec![link1, link2]
185//             } else {
186//                 vec![block]
187//             }
188//         })))),
189//     );
190//     let doc = parse_markdown(
191//         MarkdownParserState::with_config(config),
192//         "[foo]: /url \"title\"",
193//     )
194//     .unwrap();
195//     assert_eq!(
196//         doc,
197//         Document {
198//             blocks: vec![
199//                 Block::Definition(LinkDefinition {
200//                     label: vec![
201//                         Inline::Text("mapped ".to_owned()),
202//                         Inline::Text("foo".to_owned())
203//                     ],
204//                     destination: "mapped /url".to_owned(),
205//                     title: Some("mapped1 title".to_owned())
206//                 }),
207//                 Block::Definition(LinkDefinition {
208//                     label: vec![
209//                         Inline::Text("mapped ".to_owned()),
210//                         Inline::Text("foo".to_owned())
211//                     ],
212//                     destination: "mapped /url".to_owned(),
213//                     title: Some("mapped2 title".to_owned())
214//                 }),
215//             ]
216//         }
217//     );
218// }
219