-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Account for front matter when calculating sourcepos
#494
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
use crate::{format_commonmark, parse_document, Arena, Options}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn round_trip_one_field() { | ||
let mut options = Options::default(); | ||
options.extension.front_matter_delimiter = Some("---".to_owned()); | ||
let arena = Arena::new(); | ||
let input = "---\nlayout: post\n---\nText\n"; | ||
let root = parse_document(&arena, input, &options); | ||
let mut buf = Vec::new(); | ||
format_commonmark(&root, &options, &mut buf).unwrap(); | ||
assert_eq!(&String::from_utf8(buf).unwrap(), input); | ||
} | ||
|
||
#[test] | ||
fn round_trip_wide_delimiter() { | ||
let mut options = Options::default(); | ||
options.extension.front_matter_delimiter = Some("\u{04fc}".to_owned()); | ||
let arena = Arena::new(); | ||
let input = "\u{04fc}\nlayout: post\n\u{04fc}\nText\n"; | ||
let root = parse_document(&arena, input, &options); | ||
let mut buf = Vec::new(); | ||
format_commonmark(&root, &options, &mut buf).unwrap(); | ||
assert_eq!(&String::from_utf8(buf).unwrap(), input); | ||
} | ||
|
||
#[test] | ||
fn ast_wide_delimiter() { | ||
let input = "\u{04fc}\nlayout: post\n\u{04fc}\nText\n"; | ||
|
||
assert_ast_match_i( | ||
input, | ||
ast!((document (1:1-4:4) [ | ||
(frontmatter (1:1-3:1) []) | ||
(paragraph (4:1-4:4) [ | ||
(text (4:1-4:4) []) | ||
]) | ||
])), | ||
|opts| opts.extension.front_matter_delimiter = Some("\u{04fc}".to_owned()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn ast() { | ||
let input = "q\nlayout: post\nq\nText\n"; | ||
|
||
assert_ast_match_i( | ||
input, | ||
ast!((document (1:1-4:4) [ | ||
(frontmatter (1:1-3:1) []) | ||
(paragraph (4:1-4:4) [ | ||
(text (4:1-4:4) []) | ||
]) | ||
])), | ||
|opts| opts.extension.front_matter_delimiter = Some("q".to_owned()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn ast_blank_line() { | ||
let input = r#"--- | ||
a: b | ||
--- | ||
|
||
hello world | ||
"#; | ||
|
||
assert_ast_match_i( | ||
input, | ||
ast!((document (1:1-5:11) [ | ||
(frontmatter (1:1-3:3) []) | ||
(paragraph (5:1-5:11) [ | ||
(text (5:1-5:11) []) | ||
]) | ||
])), | ||
|opts| opts.extension.front_matter_delimiter = Some("---".to_owned()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn ast_carriage_return() { | ||
let input = "q\r\nlayout: post\r\nq\r\nText\r\n"; | ||
|
||
assert_ast_match_i( | ||
input, | ||
ast!((document (1:1-4:4) [ | ||
(frontmatter (1:1-3:1) []) | ||
(paragraph (4:1-4:4) [ | ||
(text (4:1-4:4) []) | ||
]) | ||
])), | ||
|opts| opts.extension.front_matter_delimiter = Some("q".to_owned()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn trailing_space_open() { | ||
let input = "--- \nlayout: post\n---\nText\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_none(), "no FrontMatter expected"); | ||
} | ||
|
||
#[test] | ||
fn leading_space_open() { | ||
let input = " ---\nlayout: post\n---\nText\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_none(), "no FrontMatter expected"); | ||
} | ||
|
||
#[test] | ||
fn leading_space_close() { | ||
let input = "---\nlayout: post\n ---\nText\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_none(), "no FrontMatter expected"); | ||
} | ||
|
||
#[test] | ||
fn trailing_space_close() { | ||
let input = "---\nlayout: post\n--- \nText\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_none(), "no FrontMatter expected"); | ||
} | ||
|
||
#[test] | ||
fn second_line() { | ||
let input = "\n---\nlayout: post\n ---\nText\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_none(), "no FrontMatter expected"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line I'm particularly unsure about. Are columns supposed to be bytes, characters, or actual columns (i.e. accounting for ZWJ, combining diacritics, etc.)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This question gave me pause too. Instead of me trying to infer it from some of my own code, let's try to get it from the source (pun intended (video unrelated)). If we ask cmark for sourcepos of some text with minimally non-byte-oriented text …
… we see it doesn't make any attempt to interpret UTF-8 whatsoever. This is a bit ¯\_(ツ)_/¯ But it accords with our current behaviour, which makes sense given we modelled on it totally:
So, for consistency (and ease of your implementation), I'd say let's start with bytes, and I'll open an issue to think about improving this (#495). There's a few places in
parser/mod.rs
where we actually useself.column
and treat it as a byte count, and while I'm not sure it'd come into play here exactly (they're mostly around leading indent), it's another vote for bytes.