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

Implement filename extension in link completion #114

Merged
merged 1 commit into from
May 28, 2024
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ new_file_folder_path = ""
# This is also imported from obsidian if not specified: specifically the option titled "New file location"
daily_notes_folder = ""


# Whether markdown links should include an extension or not
# for example [File](file.md) or [File](file)
include_md_extension_md_link = false

# Whether wikilinks should include an extension or not (needed for Markor compatibility)
# for example [[File]] or [[File.md]]
include_md_extension_wikilink = false
```


Expand Down
19 changes: 16 additions & 3 deletions src/completion/link_completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,15 @@ impl<'a> LinkCompleter<'a> for MarkdownLinkCompleter<'a> {

/// Will add <$1> to the refname if it contains spaces
fn completion_text_edit(&self, display: Option<&str>, refname: &str) -> CompletionTextEdit {
let ext = if self.settings().include_md_extension_md_link {
".md"
} else {
""
};

let link_ref_text = match refname.contains(' ') {
true => format!("<{}>", refname),
false => refname.to_owned(),
true => format!("<{}{}>", refname, ext),
false => format!("{}{}", refname, ext),
};

CompletionTextEdit::Edit(TextEdit {
Expand Down Expand Up @@ -347,6 +353,11 @@ impl<'a> LinkCompleter<'a> for WikiLinkCompleter<'a> {
}

fn completion_text_edit(&self, display: Option<&str>, refname: &str) -> CompletionTextEdit {
let ext = if self.settings().include_md_extension_wikilink {
".md"
} else {
""
};
CompletionTextEdit::Edit(TextEdit {
range: Range {
start: Position {
Expand All @@ -358,9 +369,11 @@ impl<'a> LinkCompleter<'a> for WikiLinkCompleter<'a> {
character: (self.chars_in_line).min(self.character + 2_u32),
},
},

new_text: format!(
"{}{}]]${{2:}}",
"{}{}{}]]${{2:}}",
refname,
ext,
display
.map(|display| format!("|{}", display))
.unwrap_or("".to_string())
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Settings {
pub semantic_tokens: bool,
pub tags_in_codeblocks: bool,
pub references_in_codeblocks: bool,
pub include_md_extension_md_link: bool,
pub include_md_extension_wikilink: bool,
}

impl Settings {
Expand Down Expand Up @@ -57,6 +59,8 @@ impl Settings {
.set_default("semantic_tokens", true)?
.set_default("tags_in_codeblocks", true)?
.set_default("references_in_codeblocks", true)?
.set_default("include_md_extension_md_link", false)?
.set_default("include_md_extension_wikilink", false)?
.set_override_option(
"semantic_tokens",
capabilities.text_document.as_ref().and_then(|it| {
Expand Down