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.12k stars 83 forks source link

Formatting {{authorString}} in template #189

Closed marc-vdm closed 2 years ago

marc-vdm commented 2 years ago

The {{authorString}} enables a template file to include a list of all authors, however, I'd love to connect individual references to their respective authors.

It would be amazing if there'd be another variable like {{formattedAuthorString}} that would automatically create a formatted list of the authors For example the output of{{authorString}} could be: Foo Bar, Aaa Bbb Then the output of {{formattedAuthorString}} would be: [[Foo Bar]], [[Aaa Bbb]].

This would enable automatic linking of publications to their authors, which could provide an easier workflow than manually having to edit links for every reference.

I'm unfortunately not familiar with javascript or how obsidian plugins work but in python I guess it'd look something like:

formattedAuthorString = ["[[{}]]".format(auth) for auth in authorString]

or in a more readable format:

formattedAuthorString = list()
for author in authorString:
    formatted_author = "[[" + author + "]]"
    formattedAuthorString.append(formatted_author)
JohnnyGoodNews commented 2 years ago

That's a pretty neat idea! Fortunately the handlebars formatting is quite flexible in that regard and the exposed {{entry}} variable provides us with an Author array. Those each hold a family and given(name) key. (See the docs: http://www.foldl.me/obsidian-citation-plugin/classes/entry.html)

My snippet for comma separated authors in the note template is as follows (minus the line breaks):

Authors:
{{#each entry.author}}
    [[{{this.given}} {{this.family}}]]
    {{#unless @last}}, {{/unless}}
{{/each}}
marc-vdm commented 2 years ago

I tried and this works, awesome!

Thanks a lot for taking the time to figure this out and share the solution

firubat commented 2 years ago

Came here looking for a way to have only the last names of the authors in my literature note title, a slight modification to @JohnnyGoodNews 's solution does the trick, thanks alot!

{{#each entry.author}}{{this.family}}{{#unless @last}}, {{/unless}} {{/each}}