Get frontmatter directly from pulldown-cmark
pulldown-cmark 0.10.0 introduced metadata blocks in pulldown-cmark/pulldown-cmark#641. Using this we can drop the dependency on matter and grab the frontmatter as we're consuming markdown events from the note being parsed, instead of having to extract the frontmatter separately.
This commit is contained in:
parent
e2ef435f04
commit
3afab84d69
17
Cargo.lock
generated
17
Cargo.lock
generated
@ -331,12 +331,6 @@ version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.155"
|
||||
@ -355,16 +349,6 @@ version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
|
||||
[[package]]
|
||||
name = "matter"
|
||||
version = "0.1.0-alpha4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc16e839c57e0ad77957c42d39baab3692a1c6fa47692066470cddc24a5b0cd0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
@ -379,7 +363,6 @@ dependencies = [
|
||||
"filetime",
|
||||
"gumdrop",
|
||||
"ignore",
|
||||
"matter",
|
||||
"pathdiff",
|
||||
"percent-encoding",
|
||||
"pretty_assertions",
|
||||
|
@ -28,7 +28,6 @@ doc = false
|
||||
eyre = "0.6.12"
|
||||
gumdrop = "0.8.1"
|
||||
ignore = "0.4.22"
|
||||
matter = "0.1.0-alpha4"
|
||||
pathdiff = "0.2.1"
|
||||
percent-encoding = "2.3.1"
|
||||
pulldown-cmark = "0.11.0"
|
||||
|
33
src/lib.rs
33
src/lib.rs
@ -467,6 +467,7 @@ impl<'a> Exporter<'a> {
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
#[allow(clippy::panic_in_result_fn)]
|
||||
#[allow(clippy::shadow_unrelated)]
|
||||
fn parse_obsidian_note<'b>(
|
||||
&self,
|
||||
path: &Path,
|
||||
@ -478,23 +479,40 @@ impl<'a> Exporter<'a> {
|
||||
});
|
||||
}
|
||||
let content = fs::read_to_string(path).context(ReadSnafu { path })?;
|
||||
let (frontmatter, content) =
|
||||
matter::matter(&content).unwrap_or((String::new(), content.clone()));
|
||||
let frontmatter =
|
||||
frontmatter_from_str(&frontmatter).context(FrontMatterDecodeSnafu { path })?;
|
||||
let mut frontmatter = String::new();
|
||||
|
||||
let parser_options = Options::ENABLE_TABLES
|
||||
| Options::ENABLE_FOOTNOTES
|
||||
| Options::ENABLE_STRIKETHROUGH
|
||||
| Options::ENABLE_TASKLISTS
|
||||
| Options::ENABLE_MATH;
|
||||
| Options::ENABLE_MATH
|
||||
| Options::ENABLE_YAML_STYLE_METADATA_BLOCKS;
|
||||
|
||||
let mut ref_parser = RefParser::new();
|
||||
let mut events = vec![];
|
||||
// Most of the time, a reference triggers 5 events: [ or ![, [, <text>, ], ]
|
||||
let mut buffer = Vec::with_capacity(5);
|
||||
|
||||
for event in Parser::new_ext(&content, parser_options) {
|
||||
let mut parser = Parser::new_ext(&content, parser_options);
|
||||
'outer: while let Some(event) = parser.next() {
|
||||
// When encountering a metadata block (frontmatter), collect all events until getting
|
||||
// to the end of the block, at which point the nested loop will break out to the outer
|
||||
// loop again.
|
||||
if matches!(event, Event::Start(Tag::MetadataBlock(_kind))) {
|
||||
for event in parser.by_ref() {
|
||||
match event {
|
||||
Event::Text(cowstr) => frontmatter.push_str(&cowstr),
|
||||
Event::End(TagEnd::MetadataBlock(_kind)) => {
|
||||
continue 'outer;
|
||||
},
|
||||
_ => panic!(
|
||||
"Encountered an unexpected event while processing frontmatter in {}. Please report this as a bug with a copy of the note contents and this text: \n\nEvent: {:?}\n",
|
||||
path.display(),
|
||||
event
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
if ref_parser.state == RefParserState::Resetting {
|
||||
events.append(&mut buffer);
|
||||
buffer.clear();
|
||||
@ -583,8 +601,9 @@ impl<'a> Exporter<'a> {
|
||||
if !buffer.is_empty() {
|
||||
events.append(&mut buffer);
|
||||
}
|
||||
|
||||
Ok((
|
||||
frontmatter,
|
||||
frontmatter_from_str(&frontmatter).context(FrontMatterDecodeSnafu { path })?,
|
||||
events.into_iter().map(event_to_owned).collect(),
|
||||
))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user