Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect ending front matter delimiter at EOF #508

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
Loading