mirror of
https://github.com/zoni/obsidian-export.git
synced 2024-06-27 03:35:24 +02:00
updated tests and documentation
This commit is contained in:
parent
f467902cfa
commit
7123975297
|
@ -58,7 +58,7 @@ struct Opts {
|
||||||
#[options(
|
#[options(
|
||||||
no_short,
|
no_short,
|
||||||
long="front-matter-export-filtering",
|
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"
|
default="false"
|
||||||
)]
|
)]
|
||||||
front_matter_export_filtering: bool,
|
front_matter_export_filtering: bool,
|
||||||
|
|
|
@ -18,18 +18,13 @@ pub fn softbreaks_to_hardbreaks(
|
||||||
PostprocessorResult::Continue
|
PostprocessorResult::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function takes as input the YAML key to look for, then returns a new function (technically:
|
/// This postprocessor converts returns a new function (closure) that searches for the specified
|
||||||
// a closure) which matches the signature of a postprocessor.
|
/// 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
|
||||||
// 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)
|
|
||||||
pub fn create_yaml_includer(
|
pub fn create_yaml_includer(
|
||||||
yaml_inclusion_key: &str,
|
yaml_filter_key: &str,
|
||||||
) -> impl Fn(&mut Context, &mut MarkdownEvents) -> PostprocessorResult {
|
) -> 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
|
// This bit creates and returns the closure. The `move` statement is needed to make it take
|
||||||
// ownership of `key` above.
|
// 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 obsidian_export::{Context, Exporter, MarkdownEvents, PostprocessorResult};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use pulldown_cmark::{CowStr, Event};
|
use pulldown_cmark::{CowStr, Event};
|
||||||
|
@ -219,3 +219,59 @@ fn test_softbreaks_to_hardbreaks() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(expected, actual);
|
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);
|
||||||
|
}
|
17
tests/testdata/expected/postprocessors/yaml-filtering/included_embed.md
vendored
Normal file
17
tests/testdata/expected/postprocessors/yaml-filtering/included_embed.md
vendored
Normal file
|
@ -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
|
||||||
|
|
||||||
|
*
|
6
tests/testdata/input/postprocessors/yaml-filtering/_embed.md
vendored
Normal file
6
tests/testdata/input/postprocessors/yaml-filtering/_embed.md
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
export: true
|
||||||
|
is_root_note: false
|
||||||
|
---
|
||||||
|
|
||||||
|
This note is embedded. It mentions the word foo.
|
5
tests/testdata/input/postprocessors/yaml-filtering/_no_embed.md
vendored
Normal file
5
tests/testdata/input/postprocessors/yaml-filtering/_no_embed.md
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
export: false
|
||||||
|
---
|
||||||
|
|
||||||
|
This note is embedded. It mentions the word foo.
|
17
tests/testdata/input/postprocessors/yaml-filtering/exclude_me.md
vendored
Normal file
17
tests/testdata/input/postprocessors/yaml-filtering/exclude_me.md
vendored
Normal file
|
@ -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]]
|
17
tests/testdata/input/postprocessors/yaml-filtering/include_me.md
vendored
Normal file
17
tests/testdata/input/postprocessors/yaml-filtering/include_me.md
vendored
Normal file
|
@ -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]]
|
11
tests/testdata/input/postprocessors/yaml-filtering/no_yaml.md
vendored
Normal file
11
tests/testdata/input/postprocessors/yaml-filtering/no_yaml.md
vendored
Normal file
|
@ -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
Block a user