Initial support for stripping comments from output

Not all cases supported by the reference editor are supported yet.
This commit is contained in:
Johan Förberg 2022-04-14 13:14:22 +02:00
parent ed4a568041
commit feb916c9de
2 changed files with 27 additions and 0 deletions

View File

@ -231,6 +231,7 @@ pub struct Exporter<'a> {
walk_options: WalkOptions<'a>,
process_embeds_recursively: bool,
add_titles: bool,
strip_comments: bool,
postprocessors: Vec<&'a Postprocessor>,
embed_postprocessors: Vec<&'a Postprocessor>,
}
@ -248,6 +249,7 @@ impl<'a> fmt::Debug for Exporter<'a> {
&self.process_embeds_recursively,
)
.field("add_titles", &self.add_titles)
.field("strip_comments", &self.strip_comments)
.field(
"postprocessors",
&format!("<{} postprocessors active>", self.postprocessors.len()),
@ -275,6 +277,7 @@ impl<'a> Exporter<'a> {
walk_options: WalkOptions::default(),
process_embeds_recursively: true,
add_titles: false,
strip_comments: false,
vault_contents: None,
postprocessors: vec![],
embed_postprocessors: vec![],
@ -332,6 +335,14 @@ impl<'a> Exporter<'a> {
self
}
/// Strip comment lines from output.
///
/// A comment line is one which begins with the characters "%%".
pub fn strip_comments(&mut self, strip_comments: bool) -> &mut Exporter<'a> {
self.strip_comments = strip_comments;
self
}
/// Append a function to the chain of [postprocessors][Postprocessor] to run on exported Obsidian Markdown notes.
pub fn add_postprocessor(&mut self, processor: &'a Postprocessor) -> &mut Exporter<'a> {
self.postprocessors.push(processor);
@ -503,6 +514,14 @@ impl<'a> Exporter<'a> {
ref_parser.ref_type = Some(RefType::Link);
ref_parser.transition(RefParserState::ExpectSecondOpenBracket);
}
Event::Text(ref s) => {
// TODO: This only handles %% at beginning of line, and doesn't handle
// other formatting embedded in comments
if self.strip_comments && !s.starts_with("%%") {
events.push(event);
}
buffer.clear();
}
_ => {
events.push(event);
buffer.clear();

View File

@ -61,6 +61,13 @@ struct Opts {
default = "false"
)]
add_titles: bool,
#[options(
no_short,
help = "Strip out comment lines beginning with '%%'",
default = "false"
)]
strip_comments: bool,
}
fn frontmatter_strategy_from_str(input: &str) -> Result<FrontmatterStrategy> {
@ -96,6 +103,7 @@ fn main() {
exporter.frontmatter_strategy(args.frontmatter_strategy);
exporter.process_embeds_recursively(!args.no_recursive_embeds);
exporter.add_titles(args.add_titles);
exporter.strip_comments(args.strip_comments);
exporter.walk_options(walk_options);
if args.hard_linebreaks {