Skip to content

Commit

Permalink
Merge pull request #508 from kivikakk/push-muqwvlutrkol
Browse files Browse the repository at this point in the history
Detect ending front matter delimiter at EOF
  • Loading branch information
charlottia authored Dec 14, 2024
2 parents f403247 + 4b90053 commit 8d1e90c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,16 @@ pub fn split_off_front_matter<'s>(mut s: &'s str, delimiter: &str) -> Option<(&'
start += match s[start..]
.find(&("\n".to_string() + delimiter + "\r\n"))
.or_else(|| s[start..].find(&("\n".to_string() + delimiter + "\n")))
.or_else(|| s[start..].find(&("\n".to_string() + delimiter))) // delimiter followed by EOF
{
Some(n) => n + 1 + delimiter.len(),
None => return None,
};

if start == s.len() {
return Some((s, ""));
}

start += if s[start..].starts_with('\n') {
1
} else if s[start..].starts_with("\r\n") {
Expand Down
34 changes: 34 additions & 0 deletions src/tests/front_matter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,37 @@ fn second_line() {

assert!(found.is_none(), "no FrontMatter expected");
}

#[test]
fn fm_only_with_trailing_newline() {
let input = "---\nfoo: bar\n---\n";

let mut options = Options::default();
options.extension.front_matter_delimiter = Some("---".to_owned());
let arena = Arena::new();
let root = parse_document(&arena, input, &options);

let found = root
.descendants()
.filter(|n| matches!(n.data.borrow().value, NodeValue::FrontMatter(..)))
.next();

assert!(found.is_some(), "front matter expected");
}

#[test]
fn fm_only_without_trailing_newline() {
let input = "---\nfoo: bar\n---";

let mut options = Options::default();
options.extension.front_matter_delimiter = Some("---".to_owned());
let arena = Arena::new();
let root = parse_document(&arena, input, &options);

let found = root
.descendants()
.filter(|n| matches!(n.data.borrow().value, NodeValue::FrontMatter(..)))
.next();

assert!(found.is_some(), "front matter expected");
}

0 comments on commit 8d1e90c

Please sign in to comment.