hans / obsidian-citation-plugin

Obsidian plugin which integrates your academic reference manager with the Obsidian editor. Search your references from within Obsidian and automatically create and reference literature notes for papers and books.
MIT License
1.04k stars 77 forks source link

Custom alias format when inserting litterature note link #188

Open Klemet opened 1 year ago

Klemet commented 1 year ago

Is your feature request related to a problem? Please describe. Be ready for some nitpicking ! I apologize in advance πŸ™. I just...really like to have everything neat.

Literature notes are great, and using @citekey as their title is great so that there is no title conflict for literature notes about articles with similar authors/dates.

However, citekeys are not always beautiful as the link title to the literature note when insert in text. For example:

...as is said in @DijkstraAlgorithmGraph1965, it is possible that....

Is not very pretty. If one would be nit-picky (βœ‹), one could prefer the following:

...as is said in Dijkstra et al., 1965, it is possible that...

The problem is that formatting the title of the link in that way is not practical, since the Insert literature note link function will invariably get the title of the literature note, and because this title cannot be composed of certain characters, and should be as unique as the citekey to avoid conflicts between literature notes.

It is possible to use the Markdown citation template to create a link on the form of [Pretty link](@Citekey); but then, one would miss the point of the Insert literature note link function, which is to automatically create the literature note if it does not exist already (one would have to check every time to see if the note indeed exists).

Short version : one might want the literature note to have the citekey as its title; but one could want for the link to said note to have another text than the citekey, which the Insert litterature note link function does not propose.

Describe the solution you'd like Add a new parameter to the plugin, the literature note title alias for link insertion:

image

Then, add this alias to the links that are created by the function. In the case of a markdown link, the alias is simply used in between the [ ] symbols. For an Wikilink, the alias is simply added after the | symbol of the link, as follows:

  async insertLiteratureNoteLink(citekey: string): Promise<void> {
    this.getOrCreateLiteratureNoteFile(citekey)
      .then((file: TFile) => {
        const useMarkdown: boolean = (<VaultExt>this.app.vault).getConfig(
          'useMarkdownLinks',
        );
        const title = this.getTitleForCitekey(citekey);
        const titleAlias = this.getTitleAliasForCitekey(citekey);

        let linkText: string;
        if (useMarkdown) {
          const uri = encodeURI(
            this.app.metadataCache.fileToLinktext(file, '', false),
          );
          linkText = `[${titleAlias}](${uri})`;
        } else {
          linkText = `[[${title}|$(titleAlias)]]`;
        }

        this.editor.replaceRange(linkText, this.editor.getCursor());
      })
      .catch(console.error);
  }

Used in conjunction with the handlebar syntax used in #133 , it is thus possible to obtain a link looking like Dijkstra et al., 1965 while pointing toward a literature note whose name is @DijkstraAlgorithmGraph1965. This allow for an avoidance of file name conflicts while keeping things pretty !

(Also, changing this.editor.replaceRange(linkText, this.editor.getCursor()); to this.editor.replaceSelection(linkText, this.editor.getCursor()); should resolve #138 .)

Describe alternatives you've considered Not worrying about everything being absolutely perfect for my workflow; I failed. Forgive me !