2023-12-02 13:29:29 +03:00
|
|
|
use obsidian_export::postprocessors::{filter_by_tags, softbreaks_to_hardbreaks};
|
2021-09-12 13:50:11 +03:00
|
|
|
use obsidian_export::{Context, Exporter, MarkdownEvents, PostprocessorResult};
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use pulldown_cmark::{CowStr, Event};
|
|
|
|
use serde_yaml::Value;
|
2023-09-18 21:53:38 +03:00
|
|
|
use std::collections::HashSet;
|
2021-09-12 13:50:11 +03:00
|
|
|
use std::fs::{read_to_string, remove_file};
|
|
|
|
use std::path::PathBuf;
|
2023-09-18 21:53:38 +03:00
|
|
|
use std::sync::Mutex;
|
2021-09-12 13:50:11 +03:00
|
|
|
use tempfile::TempDir;
|
2023-12-02 13:29:29 +03:00
|
|
|
use walkdir::WalkDir;
|
2021-09-12 13:50:11 +03:00
|
|
|
|
|
|
|
/// This postprocessor replaces any instance of "foo" with "bar" in the note body.
|
2022-01-09 14:52:42 +03:00
|
|
|
fn foo_to_bar(_ctx: &mut Context, events: &mut MarkdownEvents) -> PostprocessorResult {
|
|
|
|
for event in events.iter_mut() {
|
|
|
|
if let Event::Text(text) = event {
|
2024-08-02 19:46:17 +03:00
|
|
|
*event = Event::Text(CowStr::from(text.replace("foo", "bar")));
|
2022-01-09 14:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PostprocessorResult::Continue
|
2021-09-12 13:50:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This postprocessor appends "bar: baz" to frontmatter.
|
2022-01-09 14:52:42 +03:00
|
|
|
fn append_frontmatter(ctx: &mut Context, _events: &mut MarkdownEvents) -> PostprocessorResult {
|
2021-09-12 13:50:11 +03:00
|
|
|
ctx.frontmatter.insert(
|
|
|
|
Value::String("bar".to_string()),
|
|
|
|
Value::String("baz".to_string()),
|
|
|
|
);
|
2022-01-09 14:52:42 +03:00
|
|
|
PostprocessorResult::Continue
|
2021-09-12 13:50:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The purpose of this test to verify the `append_frontmatter` postprocessor is called to extend
|
|
|
|
// the frontmatter, and the `foo_to_bar` postprocessor is called to replace instances of "foo" with
|
|
|
|
// "bar" (only in the note body).
|
|
|
|
#[test]
|
|
|
|
fn test_postprocessors() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
exporter.add_postprocessor(&foo_to_bar);
|
|
|
|
exporter.add_postprocessor(&append_frontmatter);
|
|
|
|
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
let expected = read_to_string("tests/testdata/expected/postprocessors/Note.md").unwrap();
|
2023-12-02 13:29:29 +03:00
|
|
|
let actual = read_to_string(tmp_dir.path().join(PathBuf::from("Note.md"))).unwrap();
|
2021-09-12 13:50:11 +03:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_postprocessor_stophere() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_postprocessor(&|_ctx, _mdevents| PostprocessorResult::StopHere);
|
|
|
|
exporter.add_embed_postprocessor(&|_ctx, _mdevents| PostprocessorResult::StopHere);
|
2021-09-12 13:50:11 +03:00
|
|
|
exporter.add_postprocessor(&|_, _| panic!("should not be called due to above processor"));
|
2021-09-12 15:53:27 +03:00
|
|
|
exporter.add_embed_postprocessor(&|_, _| panic!("should not be called due to above processor"));
|
2021-09-12 13:50:11 +03:00
|
|
|
exporter.run().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_postprocessor_stop_and_skip() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
2023-12-02 13:29:29 +03:00
|
|
|
let note_path = tmp_dir.path().join(PathBuf::from("Note.md"));
|
2021-09-12 13:50:11 +03:00
|
|
|
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
assert!(note_path.exists());
|
|
|
|
remove_file(¬e_path).unwrap();
|
|
|
|
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_postprocessor(&|_ctx, _mdevents| PostprocessorResult::StopAndSkipNote);
|
2021-09-12 13:50:11 +03:00
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
assert!(!note_path.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_postprocessor_change_destination() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
2023-12-02 13:29:29 +03:00
|
|
|
let original_note_path = tmp_dir.path().join(PathBuf::from("Note.md"));
|
2021-09-12 13:50:11 +03:00
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
assert!(original_note_path.exists());
|
|
|
|
remove_file(&original_note_path).unwrap();
|
|
|
|
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_postprocessor(&|ctx, _mdevents| {
|
2021-09-12 13:50:11 +03:00
|
|
|
ctx.destination.set_file_name("MovedNote.md");
|
2022-01-09 14:52:42 +03:00
|
|
|
PostprocessorResult::Continue
|
2021-09-12 13:50:11 +03:00
|
|
|
});
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
2023-12-02 13:29:29 +03:00
|
|
|
let new_note_path = tmp_dir.path().join(PathBuf::from("MovedNote.md"));
|
2021-09-12 13:50:11 +03:00
|
|
|
assert!(!original_note_path.exists());
|
|
|
|
assert!(new_note_path.exists());
|
|
|
|
}
|
2021-09-12 15:53:27 +03:00
|
|
|
|
2023-09-18 21:53:38 +03:00
|
|
|
// Ensure postprocessor type definition has proper lifetimes to allow state (here: `parents`)
|
|
|
|
// to be passed in. Otherwise, this fails with an error like:
|
|
|
|
// error[E0597]: `parents` does not live long enough
|
|
|
|
// cast requires that `parents` is borrowed for `'static`
|
|
|
|
#[test]
|
|
|
|
fn test_postprocessor_stateful_callback() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let parents: Mutex<HashSet<PathBuf>> = Default::default();
|
|
|
|
let callback = |ctx: &mut Context, _mdevents: &mut MarkdownEvents| -> PostprocessorResult {
|
|
|
|
parents
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.insert(ctx.destination.parent().unwrap().to_path_buf());
|
|
|
|
PostprocessorResult::Continue
|
|
|
|
};
|
|
|
|
exporter.add_postprocessor(&callback);
|
|
|
|
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
2023-12-02 13:29:29 +03:00
|
|
|
let expected = tmp_dir.path();
|
2023-09-18 21:53:38 +03:00
|
|
|
|
|
|
|
let parents = parents.lock().unwrap();
|
2024-08-02 19:46:17 +03:00
|
|
|
println!("{parents:?}");
|
2023-09-18 21:53:38 +03:00
|
|
|
assert_eq!(1, parents.len());
|
|
|
|
assert!(parents.contains(expected));
|
|
|
|
}
|
|
|
|
|
2021-09-12 15:53:27 +03:00
|
|
|
// The purpose of this test to verify the `append_frontmatter` postprocessor is called to extend
|
|
|
|
// the frontmatter, and the `foo_to_bar` postprocessor is called to replace instances of "foo" with
|
|
|
|
// "bar" (only in the note body).
|
|
|
|
#[test]
|
|
|
|
fn test_embed_postprocessors() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
exporter.add_embed_postprocessor(&foo_to_bar);
|
|
|
|
// Should have no effect with embeds:
|
|
|
|
exporter.add_embed_postprocessor(&append_frontmatter);
|
|
|
|
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
read_to_string("tests/testdata/expected/postprocessors/Note_embed_postprocess_only.md")
|
|
|
|
.unwrap();
|
2023-12-02 13:29:29 +03:00
|
|
|
let actual = read_to_string(tmp_dir.path().join(PathBuf::from("Note.md"))).unwrap();
|
2021-09-12 15:53:27 +03:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When StopAndSkipNote is used with an embed_preprocessor, it should skip the embedded note but
|
|
|
|
// continue with the rest of the note.
|
|
|
|
#[test]
|
|
|
|
fn test_embed_postprocessors_stop_and_skip() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_embed_postprocessor(&|_ctx, _mdevents| PostprocessorResult::StopAndSkipNote);
|
2021-09-12 15:53:27 +03:00
|
|
|
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
read_to_string("tests/testdata/expected/postprocessors/Note_embed_stop_and_skip.md")
|
|
|
|
.unwrap();
|
2023-12-02 13:29:29 +03:00
|
|
|
let actual = read_to_string(tmp_dir.path().join(PathBuf::from("Note.md"))).unwrap();
|
2021-09-12 15:53:27 +03:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test verifies that the context which is passed to an embed postprocessor is actually
|
|
|
|
// correct. Primarily, this means the frontmatter should reflect that of the note being embedded as
|
|
|
|
// opposed to the frontmatter of the root note.
|
|
|
|
#[test]
|
|
|
|
fn test_embed_postprocessors_context() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_postprocessor(&|ctx, _mdevents| {
|
2021-09-12 15:53:27 +03:00
|
|
|
if ctx.current_file() != &PathBuf::from("Note.md") {
|
2022-01-09 14:52:42 +03:00
|
|
|
return PostprocessorResult::Continue;
|
2021-09-12 15:53:27 +03:00
|
|
|
}
|
|
|
|
let is_root_note = ctx
|
|
|
|
.frontmatter
|
|
|
|
.get(&Value::String("is_root_note".to_string()))
|
|
|
|
.unwrap();
|
|
|
|
if is_root_note != &Value::Bool(true) {
|
|
|
|
// NOTE: Test failure may not give output consistently because the test binary affects
|
|
|
|
// how output is captured and printed in the thread running this postprocessor. Just
|
|
|
|
// run the test a couple times until the error shows up.
|
|
|
|
panic!(
|
|
|
|
"postprocessor: expected is_root_note in {} to be true, got false",
|
|
|
|
&ctx.current_file().display()
|
2024-08-02 19:46:17 +03:00
|
|
|
);
|
2021-09-12 15:53:27 +03:00
|
|
|
}
|
2022-01-09 14:52:42 +03:00
|
|
|
PostprocessorResult::Continue
|
2021-09-12 15:53:27 +03:00
|
|
|
});
|
2022-01-09 14:52:42 +03:00
|
|
|
exporter.add_embed_postprocessor(&|ctx, _mdevents| {
|
2021-09-12 15:53:27 +03:00
|
|
|
let is_root_note = ctx
|
|
|
|
.frontmatter
|
|
|
|
.get(&Value::String("is_root_note".to_string()))
|
|
|
|
.unwrap();
|
|
|
|
if is_root_note == &Value::Bool(true) {
|
|
|
|
// NOTE: Test failure may not give output consistently because the test binary affects
|
|
|
|
// how output is captured and printed in the thread running this postprocessor. Just
|
|
|
|
// run the test a couple times until the error shows up.
|
|
|
|
panic!(
|
|
|
|
"embed_postprocessor: expected is_root_note in {} to be false, got true",
|
|
|
|
&ctx.current_file().display()
|
2024-08-02 19:46:17 +03:00
|
|
|
);
|
2021-09-12 15:53:27 +03:00
|
|
|
}
|
2022-01-09 14:52:42 +03:00
|
|
|
PostprocessorResult::Continue
|
2021-09-12 15:53:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
exporter.run().unwrap();
|
|
|
|
}
|
2022-01-02 02:42:51 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_softbreaks_to_hardbreaks() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/postprocessors"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
exporter.add_postprocessor(&softbreaks_to_hardbreaks);
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
read_to_string("tests/testdata/expected/postprocessors/hard_linebreaks.md").unwrap();
|
2023-12-02 13:29:29 +03:00
|
|
|
let actual = read_to_string(tmp_dir.path().join(PathBuf::from("hard_linebreaks.md"))).unwrap();
|
2022-01-02 02:42:51 +03:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2023-12-02 13:29:29 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter_by_tags() {
|
|
|
|
let tmp_dir = TempDir::new().expect("failed to make tempdir");
|
|
|
|
let mut exporter = Exporter::new(
|
|
|
|
PathBuf::from("tests/testdata/input/filter-by-tags"),
|
|
|
|
tmp_dir.path().to_path_buf(),
|
|
|
|
);
|
|
|
|
let filter_by_tags = filter_by_tags(
|
|
|
|
vec!["private".to_string(), "no-export".to_string()],
|
|
|
|
vec!["export".to_string()],
|
|
|
|
);
|
|
|
|
exporter.add_postprocessor(&filter_by_tags);
|
|
|
|
exporter.run().unwrap();
|
|
|
|
|
|
|
|
let walker = WalkDir::new("tests/testdata/expected/filter-by-tags/")
|
|
|
|
// Without sorting here, different test runs may trigger the first assertion failure in
|
|
|
|
// unpredictable order.
|
|
|
|
.sort_by(|a, b| a.file_name().cmp(b.file_name()))
|
|
|
|
.into_iter();
|
|
|
|
for entry in walker {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.metadata().unwrap().is_dir() {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let filename = entry.file_name().to_string_lossy().into_owned();
|
|
|
|
let expected = read_to_string(entry.path()).unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
"failed to read {} from testdata/expected/filter-by-tags",
|
|
|
|
entry.path().display()
|
|
|
|
)
|
|
|
|
});
|
|
|
|
let actual = read_to_string(tmp_dir.path().join(PathBuf::from(&filename)))
|
|
|
|
.unwrap_or_else(|_| panic!("failed to read {} from temporary exportdir", filename));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
expected, actual,
|
|
|
|
"{} does not have expected content",
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|