Skip to content

Commit

Permalink
feat: ignore frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 11, 2024
1 parent cdab153 commit a3dba9a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/frontmatter.md
Original file line number Diff line number Diff line change
@@ -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
}
```

0 comments on commit a3dba9a

Please sign in to comment.