Skip to content

Commit 5804104

Browse files
author
awegsche
authored
Fix link without content (#14)
* fix handling of filepath_only links * add test for filepath_only link
1 parent 055d113 commit 5804104

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ mod tests {
697697
"[description]{* hello}",
698698
"This is a <link>!",
699699
"<*linkable with markup*> here!",
700+
"{:another_file:}",
700701
]
701702
.into_iter()
702703
.map(|example| example.to_string() + "\n")

src/snapshots/rust_norg__tests__links.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,8 @@ expression: examples
130130
Text: here
131131
- Token:
132132
Special: "!"
133+
- - Paragraph:
134+
- Link:
135+
filepath: another_file
136+
targets: []
137+
description: ~

src/stage_3.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ fn paragraph_parser_opener_candidates_and_links() -> impl Parser<
188188
just(ParagraphSegmentToken::Special('}'))
189189
.not()
190190
.repeated()
191-
.at_least(1),
191+
.at_least(1)
192+
.or_not(),
192193
)
193194
.then_ignore(just(ParagraphSegmentToken::Special('}')))
194195
.then(anchor.clone().or_not())
@@ -197,26 +198,32 @@ fn paragraph_parser_opener_candidates_and_links() -> impl Parser<
197198
filepath: filepath
198199
.map(|content| content.into_iter().map_into::<String>().collect()),
199200
description: description.map(|content| parse_paragraph(content).unwrap()),
200-
targets: vec![if let Some(modifiers) = modifiers {
201-
match modifiers.as_str() {
202-
"$" => LinkTarget::Definition(parse_paragraph(content).unwrap()),
203-
"^" => LinkTarget::Footnote(parse_paragraph(content).unwrap()),
204-
"?" => LinkTarget::Wiki(parse_paragraph(content).unwrap()),
205-
"=" => LinkTarget::Extendable(parse_paragraph(content).unwrap()),
206-
"/" => LinkTarget::Path(content.into_iter().map_into::<String>().collect()),
207-
"@" => LinkTarget::Timestamp(
208-
content.into_iter().map_into::<String>().collect(),
209-
),
210-
211-
// Only other possibility is a heading.
212-
str => LinkTarget::Heading {
213-
level: str.len() as u16,
214-
title: parse_paragraph(content).unwrap(),
215-
},
216-
}
201+
targets: if let Some(content) = content {
202+
vec![if let Some(modifiers) = modifiers {
203+
match modifiers.as_str() {
204+
"$" => LinkTarget::Definition(parse_paragraph(content).unwrap()),
205+
"^" => LinkTarget::Footnote(parse_paragraph(content).unwrap()),
206+
"?" => LinkTarget::Wiki(parse_paragraph(content).unwrap()),
207+
"=" => LinkTarget::Extendable(parse_paragraph(content).unwrap()),
208+
"/" => {
209+
LinkTarget::Path(content.into_iter().map_into::<String>().collect())
210+
}
211+
"@" => LinkTarget::Timestamp(
212+
content.into_iter().map_into::<String>().collect(),
213+
),
214+
215+
// Only other possibility is a heading.
216+
str => LinkTarget::Heading {
217+
level: str.len() as u16,
218+
title: parse_paragraph(content).unwrap(),
219+
},
220+
}
221+
} else {
222+
LinkTarget::Url(content.into_iter().map_into::<String>().collect())
223+
}]
217224
} else {
218-
LinkTarget::Url(content.into_iter().map_into::<String>().collect())
219-
}],
225+
vec![]
226+
},
220227
},
221228
);
222229

0 commit comments

Comments
 (0)