Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flag to specify wikilink path prefix #239

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand All @@ -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",
Expand Down Expand Up @@ -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![],
}
Expand All @@ -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;
Expand Down Expand Up @@ -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('#');
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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);
Expand Down
Loading