From a3dba9a8bfb2135c511c1b2510e95908891657b0 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Mon, 11 Mar 2024 14:28:35 +0100 Subject: [PATCH] feat: ignore frontmatter --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 13 ++++++++++++- tests/frontmatter.md | 19 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/frontmatter.md diff --git a/Cargo.lock b/Cargo.lock index 9a703fc9..69966c31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,6 +215,12 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -233,12 +239,23 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[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 = "mdsf" version = "0.0.0" dependencies = [ "clap", "ignore", + "matter", "pulldown-cmark", "pulldown-cmark-to-cmark", "schemars", @@ -291,6 +308,18 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + [[package]] name = "regex-automata" version = "0.4.6" diff --git a/Cargo.toml b/Cargo.toml index f55c508c..a9bf2662 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ categories = ["development-tools"] [dependencies] clap = { version = "4.5.2", features = ["derive"] } ignore = "0.4.22" +matter = "0.1.0-alpha4" pulldown-cmark = { version = "0.9.6", default-features = false } pulldown-cmark-to-cmark = "11.2.0" schemars = "0.8.16" diff --git a/src/lib.rs b/src/lib.rs index e4f9603c..e06438e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ use config::MdsfConfig; use error::MdsfError; use formatters::format_snippet; use languages::Language; +use matter::matter; use pulldown_cmark::CowStr; use pulldown_cmark_to_cmark::cmark_resume_with_options; @@ -48,7 +49,11 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md return Ok(()); } - let parser = pulldown_cmark::Parser::new_ext(&input, pulldown_cmark::Options::all()); + // NOTE: should be removed once `pulldown-cmark-to-cmark` is upgraded to `v0.10.0` of `pulldown-cmark` + let (frontmatter, markdown_input) = + matter(&input).unwrap_or_else(|| (String::new(), input.to_owned())); + + let parser = pulldown_cmark::Parser::new_ext(&markdown_input, pulldown_cmark::Options::all()); let mut output = String::with_capacity(input.len() + 128); @@ -105,6 +110,12 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md s.finalize(&mut output).map_err(MdsfError::from)?; } + output = output.trim().to_owned(); + + if !frontmatter.is_empty() { + output = format!("---\n{frontmatter}\n---\n\n{output}"); + } + if config.markdown.enabled { if !output.is_empty() { output = format_snippet(config, &Language::Markdown, &output); diff --git a/tests/frontmatter.md b/tests/frontmatter.md new file mode 100644 index 00000000..99fb24e7 --- /dev/null +++ b/tests/frontmatter.md @@ -0,0 +1,19 @@ +--- +tile1: asd asd +tile2: asd asd +tile3: asd asd + +tile4: asd asd + +tile5: asd asd +--- + +this is the content + +```go +package main + +func add(a int, b int) int { + return a + b +} +```