Skip to content

Commit a88139d

Browse files
Merge branch 'personal' into remove-comments
2 parents e3aa83d + 3be906b commit a88139d

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ struct Opts {
8484
default = "keep-unchanged"
8585
)]
8686
comment_strategy: CommentStrategy,
87+
help = "Remove automatic-table-of-contents objects",
88+
default = "false"
89+
)]
90+
remove_table_of_contents: bool,
91+
8792
}
8893

8994
fn frontmatter_strategy_from_str(input: &str) -> Result<FrontmatterStrategy> {
@@ -133,6 +138,11 @@ fn main() {
133138
exporter.add_postprocessor(&softbreaks_to_hardbreaks);
134139
}
135140

141+
142+
if args.remove_table_of_contents {
143+
exporter.add_postprocessor(&remove_toc);
144+
}
145+
136146
if matches!(args.comment_strategy, CommentStrategy::Remove) {
137147
exporter.add_postprocessor(&remove_obsidian_comments);
138148
}

src/postprocessors.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! A collection of officially maintained [postprocessors][crate::Postprocessor].
22
33
use std::cell::LazyCell;
4-
5-
use pulldown_cmark::{CowStr, Event, Tag};
4+
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Tag};
65
use regex::Regex;
76
use serde_yaml::Value;
7+
use std::string::String;
88

99
use super::{Context, MarkdownEvents, PostprocessorResult};
1010

@@ -55,6 +55,36 @@ fn filter_by_tags_(
5555
}
5656
}
5757

58+
pub fn remove_toc(_context: &mut Context, events: &mut MarkdownEvents) -> PostprocessorResult {
59+
let mut output = Vec::with_capacity(events.len());
60+
61+
for event in &mut *events {
62+
output.push(event.to_owned());
63+
match event {
64+
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(ref language_tag))) => {
65+
if language_tag == &CowStr::from("toc")
66+
|| language_tag == &CowStr::from("table-of-contents")
67+
{
68+
output.pop(); // Remove codeblock start tag that was pushed onto output
69+
}
70+
}
71+
Event::End(Tag::CodeBlock(CodeBlockKind::Fenced(ref language_tag))) => {
72+
if language_tag == &CowStr::from("toc")
73+
|| language_tag == &CowStr::from("table-of-contents")
74+
{
75+
// The corresponding codeblock start tag for this is replaced with regular
76+
// text (containing the Hugo shortcode), so we must also pop this end tag.
77+
output.pop();
78+
}
79+
}
80+
_ => {}
81+
}
82+
}
83+
eprintln!("OUTPUT ---- {:?}", output);
84+
*events = output;
85+
PostprocessorResult::Continue
86+
}
87+
5888
//Available strategies for what to do with comments
5989
#[derive(Debug)]
6090
#[non_exhaustive]

tests/postprocessors_test.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
use obsidian_export::postprocessors::{
3+
filter_by_tags, remove_obsidian_comments, softbreaks_to_hardbreaks,
4+
};
5+
use pretty_assertions::assert_eq;
6+
use pulldown_cmark::{CowStr, Event};
7+
use serde_yaml::Value;
18
use std::collections::HashSet;
29
use std::fs::{read_to_string, remove_file};
310
use std::path::PathBuf;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Not a comment
2+
3+
4+
````md
5+
%% Comment in code block should be kept %%
6+
````
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Not a comment
2+
%% Comment should be removed %%
3+
```md
4+
%% Comment in code block should be kept %%
5+
```
6+
7+
%%
8+
This is
9+
a block comment
10+
that should be removed
11+
%%

0 commit comments

Comments
 (0)