-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpostprocessors.rs
198 lines (181 loc) · 6.37 KB
/
postprocessors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! A collection of officially maintained [postprocessors][crate::Postprocessor].
use std::cell::LazyCell;
use pulldown_cmark::{CowStr, Event, Tag};
use regex::Regex;
use serde_yaml::Value;
use super::{Context, MarkdownEvents, PostprocessorResult};
/// This postprocessor converts all soft line breaks to hard line breaks. Enabling this mimics
/// Obsidian's _'Strict line breaks'_ setting.
pub fn softbreaks_to_hardbreaks(
_context: &mut Context,
events: &mut MarkdownEvents<'_>,
) -> PostprocessorResult {
for event in events.iter_mut() {
if event == &Event::SoftBreak {
*event = Event::HardBreak;
}
}
PostprocessorResult::Continue
}
pub fn filter_by_tags(
skip_tags: Vec<String>,
only_tags: Vec<String>,
) -> impl Fn(&mut Context, &mut MarkdownEvents<'_>) -> PostprocessorResult {
move |context: &mut Context, _events: &mut MarkdownEvents<'_>| -> PostprocessorResult {
match context.frontmatter.get("tags") {
None => filter_by_tags_(&[], &skip_tags, &only_tags),
Some(Value::Sequence(tags)) => filter_by_tags_(tags, &skip_tags, &only_tags),
_ => PostprocessorResult::Continue,
}
}
}
fn filter_by_tags_(
tags: &[Value],
skip_tags: &[String],
only_tags: &[String],
) -> PostprocessorResult {
let skip = skip_tags
.iter()
.any(|tag| tags.contains(&Value::String(tag.to_string())));
let include = only_tags.is_empty()
|| only_tags
.iter()
.any(|tag| tags.contains(&Value::String(tag.to_string())));
if skip || !include {
PostprocessorResult::StopAndSkipNote
} else {
PostprocessorResult::Continue
}
}
//Available strategies for what to do with comments
#[derive(Debug)]
#[non_exhaustive]
pub enum CommentStrategy {
// Leave comments alone and export them as normal
KeepUnchanged,
// Remove any comments from the output
Remove,
}
#[allow(clippy::arithmetic_side_effects)]
/// This postprocessor removes all Obsidian comments from a file excluding codeblocks. Enabling this
/// prohibits comments from being exported but leaves them untouched in the original files
pub fn remove_obsidian_comments(
_context: &mut Context,
events: &mut MarkdownEvents<'_>,
) -> PostprocessorResult {
let mut output = Vec::with_capacity(events.len());
let mut inside_comment = false;
let mut inside_codeblock = false;
let re = LazyCell::new(|| Regex::new(r"%%.*?%%").unwrap());
for event in &mut *events {
output.push(event.to_owned());
match event {
Event::Text(ref text) => {
if !text.contains("%%") {
if inside_comment {
output.pop(); //Inside block comment so remove
}
continue;
}
if inside_codeblock {
continue; //Skip anything inside codeblocks
}
output.pop();
if inside_comment {
inside_comment = false;
continue;
}
if !text.eq(&CowStr::from("%%")) {
let result = re.replace_all(text, "").to_string();
output.push(Event::Text(CowStr::from(result)));
continue;
}
inside_comment = true;
}
Event::Start(Tag::CodeBlock(_)) => {
if inside_comment {
output.pop();
} else {
inside_codeblock = true;
}
}
Event::End(Tag::CodeBlock(_)) => {
if inside_comment {
output.pop();
} else {
inside_codeblock = false;
}
}
Event::End(Tag::Paragraph) => {
if output.len() >= 2
&& output.get(output.len() - 2) == Option::from(&Event::Start(Tag::Paragraph))
{
// If the comment was the only item on the line remove the start and end
// paragraph events to remove the \n in the output file.
output.pop();
output.pop();
}
}
_ => {
if inside_comment {
output.pop();
}
}
}
}
*events = output;
PostprocessorResult::Continue
}
#[test]
fn test_filter_tags() {
let tags = vec![
Value::String("skip".into()),
Value::String("publish".into()),
];
let empty_tags = vec![];
assert_eq!(
filter_by_tags_(&empty_tags, &[], &[]),
PostprocessorResult::Continue,
"When no exclusion & inclusion are specified, files without tags are included"
);
assert_eq!(
filter_by_tags_(&tags, &[], &[]),
PostprocessorResult::Continue,
"When no exclusion & inclusion are specified, files with tags are included"
);
assert_eq!(
filter_by_tags_(&tags, &["exclude".into()], &[]),
PostprocessorResult::Continue,
"When exclusion tags don't match files with tags are included"
);
assert_eq!(
filter_by_tags_(&empty_tags, &["exclude".into()], &[]),
PostprocessorResult::Continue,
"When exclusion tags don't match files without tags are included"
);
assert_eq!(
filter_by_tags_(&tags, &[], &["publish".into()]),
PostprocessorResult::Continue,
"When exclusion tags don't match files with tags are included"
);
assert_eq!(
filter_by_tags_(&empty_tags, &[], &["include".into()]),
PostprocessorResult::StopAndSkipNote,
"When inclusion tags are specified files without tags are excluded"
);
assert_eq!(
filter_by_tags_(&tags, &[], &["include".into()]),
PostprocessorResult::StopAndSkipNote,
"When exclusion tags don't match files with tags are exluded"
);
assert_eq!(
filter_by_tags_(&tags, &["skip".into()], &["skip".into()]),
PostprocessorResult::StopAndSkipNote,
"When both inclusion and exclusion tags are the same exclusion wins"
);
assert_eq!(
filter_by_tags_(&tags, &["skip".into()], &["publish".into()]),
PostprocessorResult::StopAndSkipNote,
"When both inclusion and exclusion tags match exclusion wins"
);
}