From 0eb733837aa638a5b206099e821e17c15f261c2d Mon Sep 17 00:00:00 2001 From: khimaros Date: Thu, 12 Jan 2023 17:25:40 -0800 Subject: [PATCH] add flag to specify wikilink path prefix --- src/lib.rs | 14 +++++++++++++- src/main.rs | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index bb3f7e5..7f9df89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -230,6 +230,7 @@ pub struct Exporter<'a> { frontmatter_strategy: FrontmatterStrategy, vault_contents: Option>, walk_options: WalkOptions<'a>, + wikilink_prefix: String, process_embeds_recursively: bool, postprocessors: Vec<&'a Postprocessor<'a>>, embed_postprocessors: Vec<&'a Postprocessor<'a>>, @@ -242,6 +243,7 @@ impl<'a> fmt::Debug for Exporter<'a> { .field("destination", &self.destination) .field("frontmatter_strategy", &self.frontmatter_strategy) .field("vault_contents", &self.vault_contents) + .field("wikilink_prefix", &self.wikilink_prefix) .field("walk_options", &self.walk_options) .field( "process_embeds_recursively", @@ -274,6 +276,7 @@ impl<'a> Exporter<'a> { walk_options: WalkOptions::default(), process_embeds_recursively: true, vault_contents: None, + wikilink_prefix: "".to_string(), postprocessors: vec![], embed_postprocessors: vec![], } @@ -294,6 +297,12 @@ impl<'a> Exporter<'a> { self } + /// Set the wikilink_prefix to be used for this exporter. + pub fn wikilink_prefix(&mut self, prefix: String) -> &mut Exporter<'a> { + self.wikilink_prefix = prefix; + self + } + /// Set the [`FrontmatterStrategy`] to be used for this exporter. pub fn frontmatter_strategy(&mut self, strategy: FrontmatterStrategy) -> &mut Exporter<'a> { self.frontmatter_strategy = strategy; @@ -686,7 +695,10 @@ impl<'a> Exporter<'a> { .expect("should be able to build relative path when target file is found in vault"); let rel_link = rel_link.to_string_lossy(); - let mut link = utf8_percent_encode(&rel_link, PERCENTENCODE_CHARS).to_string(); + + let full_link = format!("{}{}", self.wikilink_prefix, rel_link); + + let mut link = utf8_percent_encode(&full_link, PERCENTENCODE_CHARS).to_string(); if let Some(section) = reference.section { link.push('#'); diff --git a/src/main.rs b/src/main.rs index 1798d1b..40f7c66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,13 @@ struct Opts { #[options(no_short, help = "Export only files with this tag")] only_tags: Vec, + #[options( + no_short, + help = "Prefix all wikilinks with this path.", + default = "" + )] + wikilink_prefix: String, + #[options(no_short, help = "Export hidden files", default = "false")] hidden: bool, @@ -95,6 +102,7 @@ fn main() { exporter.frontmatter_strategy(args.frontmatter_strategy); exporter.process_embeds_recursively(!args.no_recursive_embeds); exporter.walk_options(walk_options); + exporter.wikilink_prefix(args.wikilink_prefix); if args.hard_linebreaks { exporter.add_postprocessor(&softbreaks_to_hardbreaks);