updated tests and documentation
This commit is contained in:
parent
9eebe4751a
commit
ea853199fe
|
@ -58,7 +58,7 @@ struct Opts {
|
|||
#[options(
|
||||
no_short,
|
||||
long="front-matter-export-filtering",
|
||||
help="Enables selectively exporting files only if they have matching YAML key set to true in the frontmatter",
|
||||
help="Exclude all files from export that do not have a matching YAML key set to true in the frontmatter",
|
||||
default="false"
|
||||
)]
|
||||
front_matter_export_filtering: bool,
|
||||
|
|
|
@ -18,18 +18,13 @@ pub fn softbreaks_to_hardbreaks(
|
|||
PostprocessorResult::Continue
|
||||
}
|
||||
|
||||
// This function takes as input the YAML key to look for, then returns a new function (technically:
|
||||
// a closure) which matches the signature of a postprocessor.
|
||||
//
|
||||
// This use of dynamic function building allows the capturing of the configuration (in this case
|
||||
// the YAML key) without needing to store this data within the Exporter struct.
|
||||
//
|
||||
// (Ideally we could mark the return value as `-> impl Postprocessor` for readability, but we
|
||||
// cannot use a type alias here, which is what `Postprocessor` is)
|
||||
/// This postprocessor converts returns a new function (closure) that searches for the specified
|
||||
/// yaml_filter_key in a notes frontmatter. If it does not find a yaml_filter_key: true in the YAML,
|
||||
/// it tells the exporter to StopandSkipNote
|
||||
pub fn create_yaml_includer(
|
||||
yaml_inclusion_key: &str,
|
||||
yaml_filter_key: &str,
|
||||
) -> impl Fn(&mut Context, &mut MarkdownEvents) -> PostprocessorResult {
|
||||
let key = serde_yaml::Value::String(yaml_inclusion_key.to_string());
|
||||
let key = serde_yaml::Value::String(yaml_filter_key.to_string());
|
||||
|
||||
// This bit creates and returns the closure. The `move` statement is needed to make it take
|
||||
// ownership of `key` above.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use obsidian_export::postprocessors::softbreaks_to_hardbreaks;
|
||||
use obsidian_export::postprocessors::{softbreaks_to_hardbreaks, create_yaml_includer};
|
||||
use obsidian_export::{Context, Exporter, MarkdownEvents, PostprocessorResult};
|
||||
use pretty_assertions::assert_eq;
|
||||
use pulldown_cmark::{CowStr, Event};
|
||||
|
@ -219,3 +219,59 @@ fn test_softbreaks_to_hardbreaks() {
|
|||
.unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
// This test verifies that yaml inclusion works as desired
|
||||
// ONLY when a .md file has the specified key set to a YAML true should it work
|
||||
#[test]
|
||||
fn test_yaml_inclusion() {
|
||||
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
||||
let files = ["exlude_me.md", "include_me.md", "no_yaml.md"];
|
||||
let desired = [false, true, false];
|
||||
|
||||
let mut exporter = Exporter::new(
|
||||
PathBuf::from("tests/testdata/input/postprocessors/yaml-filtering"),
|
||||
tmp_dir.path().to_path_buf(),
|
||||
);
|
||||
let yaml_postprocessor = create_yaml_includer("export");
|
||||
exporter.add_postprocessor(&yaml_postprocessor);
|
||||
|
||||
// Run the exporter
|
||||
exporter.run().unwrap();
|
||||
|
||||
// Check that each file is included or excluded correctly
|
||||
files.iter().zip(desired.iter()).map(|(f, b)| {
|
||||
let note_path = tmp_dir.path().clone().join(PathBuf::from(f));
|
||||
assert!(note_path.exists() == *b);
|
||||
}).collect()
|
||||
|
||||
}
|
||||
|
||||
// This test verifies that yaml inclusion works as desired for the embedded post-processor
|
||||
// The behavior should be that the file is only embedded *if* it's frontmatter contains export: true
|
||||
#[test]
|
||||
fn test_yaml_inclusion_embedded() {
|
||||
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
||||
|
||||
let mut exporter = Exporter::new(
|
||||
PathBuf::from("tests/testdata/input/postprocessors/yaml-filtering"),
|
||||
tmp_dir.path().to_path_buf(),
|
||||
);
|
||||
|
||||
let yaml_postprocessor = create_yaml_includer("export");
|
||||
exporter.add_postprocessor(&yaml_postprocessor);
|
||||
exporter.add_embed_postprocessor(&yaml_postprocessor);
|
||||
|
||||
// Run the exporter
|
||||
exporter.run().unwrap();
|
||||
|
||||
let expected =
|
||||
read_to_string("tests/testdata/expected/postprocessors/yaml-filtering/included_embed.md").unwrap();
|
||||
let actual = read_to_string(
|
||||
tmp_dir
|
||||
.path()
|
||||
.clone()
|
||||
.join(PathBuf::from("include_me.md")),
|
||||
).unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
export: true
|
||||
layout: post
|
||||
title: test page
|
||||
---
|
||||
|
||||
# This is H1
|
||||
|
||||
here is some text
|
||||
|
||||
A working embed
|
||||
|
||||
* This note is embedded. It mentions the word foo.
|
||||
|
||||
Should be an empty embed
|
||||
|
||||
*
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
export: true
|
||||
is_root_note: false
|
||||
---
|
||||
|
||||
This note is embedded. It mentions the word foo.
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
export: false
|
||||
---
|
||||
|
||||
This note is embedded. It mentions the word foo.
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
export: false
|
||||
layout: post
|
||||
title: test page
|
||||
---
|
||||
|
||||
# This is H1
|
||||
|
||||
here is some text
|
||||
|
||||
A working embed
|
||||
|
||||
- ![[_yes_embed]]
|
||||
|
||||
Should be an empty embed
|
||||
|
||||
- ![[_no_embed]]
|
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
export: true
|
||||
layout: post
|
||||
title: test page
|
||||
---
|
||||
|
||||
# This is H1
|
||||
|
||||
here is some text
|
||||
|
||||
A working embed
|
||||
|
||||
- ![[_embed]]
|
||||
|
||||
Should be an empty embed
|
||||
|
||||
- ![[_no_embed]]
|
|
@ -0,0 +1,11 @@
|
|||
# This is H1
|
||||
|
||||
here is some text
|
||||
|
||||
A working embed
|
||||
|
||||
- ![[_yes_embed]]
|
||||
|
||||
Should be an empty embed
|
||||
|
||||
- ![[_no_embed]]
|
Loading…
Reference in New Issue