add flag to specify wikilink path prefix

This commit is contained in:
khimaros 2023-01-12 17:25:40 -08:00
parent a7517106e8
commit 0eb733837a
2 changed files with 21 additions and 1 deletions

View File

@ -230,6 +230,7 @@ pub struct Exporter<'a> {
frontmatter_strategy: FrontmatterStrategy,
vault_contents: Option<Vec<PathBuf>>,
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('#');

View File

@ -45,6 +45,13 @@ struct Opts {
#[options(no_short, help = "Export only files with this tag")]
only_tags: Vec<String>,
#[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);