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:
Nick Groenen 2024-08-04 23:58:15 +02:00
parent e2ef435f04
commit 3afab84d69
No known key found for this signature in database
GPG Key ID: 4F0AD019928AE098
3 changed files with 26 additions and 25 deletions

17
Cargo.lock generated
View File

@ -331,12 +331,6 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.155" version = "0.2.155"
@ -355,16 +349,6 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" 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]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.4" version = "2.7.4"
@ -379,7 +363,6 @@ dependencies = [
"filetime", "filetime",
"gumdrop", "gumdrop",
"ignore", "ignore",
"matter",
"pathdiff", "pathdiff",
"percent-encoding", "percent-encoding",
"pretty_assertions", "pretty_assertions",

View File

@ -28,7 +28,6 @@ doc = false
eyre = "0.6.12" eyre = "0.6.12"
gumdrop = "0.8.1" gumdrop = "0.8.1"
ignore = "0.4.22" ignore = "0.4.22"
matter = "0.1.0-alpha4"
pathdiff = "0.2.1" pathdiff = "0.2.1"
percent-encoding = "2.3.1" percent-encoding = "2.3.1"
pulldown-cmark = "0.11.0" pulldown-cmark = "0.11.0"

View File

@ -467,6 +467,7 @@ impl<'a> Exporter<'a> {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
#[allow(clippy::panic_in_result_fn)] #[allow(clippy::panic_in_result_fn)]
#[allow(clippy::shadow_unrelated)]
fn parse_obsidian_note<'b>( fn parse_obsidian_note<'b>(
&self, &self,
path: &Path, path: &Path,
@ -478,23 +479,40 @@ impl<'a> Exporter<'a> {
}); });
} }
let content = fs::read_to_string(path).context(ReadSnafu { path })?; let content = fs::read_to_string(path).context(ReadSnafu { path })?;
let (frontmatter, content) = let mut frontmatter = String::new();
matter::matter(&content).unwrap_or((String::new(), content.clone()));
let frontmatter =
frontmatter_from_str(&frontmatter).context(FrontMatterDecodeSnafu { path })?;
let parser_options = Options::ENABLE_TABLES let parser_options = Options::ENABLE_TABLES
| Options::ENABLE_FOOTNOTES | Options::ENABLE_FOOTNOTES
| Options::ENABLE_STRIKETHROUGH | Options::ENABLE_STRIKETHROUGH
| Options::ENABLE_TASKLISTS | Options::ENABLE_TASKLISTS
| Options::ENABLE_MATH; | Options::ENABLE_MATH
| Options::ENABLE_YAML_STYLE_METADATA_BLOCKS;
let mut ref_parser = RefParser::new(); let mut ref_parser = RefParser::new();
let mut events = vec![]; let mut events = vec![];
// Most of the time, a reference triggers 5 events: [ or ![, [, <text>, ], ] // Most of the time, a reference triggers 5 events: [ or ![, [, <text>, ], ]
let mut buffer = Vec::with_capacity(5); 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 { if ref_parser.state == RefParserState::Resetting {
events.append(&mut buffer); events.append(&mut buffer);
buffer.clear(); buffer.clear();
@ -583,8 +601,9 @@ impl<'a> Exporter<'a> {
if !buffer.is_empty() { if !buffer.is_empty() {
events.append(&mut buffer); events.append(&mut buffer);
} }
Ok(( Ok((
frontmatter, frontmatter_from_str(&frontmatter).context(FrontMatterDecodeSnafu { path })?,
events.into_iter().map(event_to_owned).collect(), events.into_iter().map(event_to_owned).collect(),
)) ))
} }