Skip to content

Instantly share code, notes, and snippets.

@QWxleA
Last active April 12, 2024 02:20
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save QWxleA/c7f08e8feded332736a6b376045d0fe4 to your computer and use it in GitHub Desktop.
Save QWxleA/c7f08e8feded332736a6b376045d0fe4 to your computer and use it in GitHub Desktop.
Obsidian zotero template for use with "obsidian-zotero-integration" plugin

QWxlea's over-engineered zotero template

Faq

What are these %% begin id-pn2xitti%% and %% end id-pn2xitti %% and ^pn2xitti? These are the persistance-markers of the indivual highlights, and the reference-id to that highlight.

The markers are meant for the importer. It will skip all text within those markers, so they stay when updating a zotero-import. Removing a marked block does not work well, it will not be reimported. Don't know how to fix that. If you want to reimport a highlight-block, best is to change the highlight in zotero (for example change the color). This will update the id, and on the next import the new highlight will be imported.

The reference-id is used like this: [[@name-of-the-note#^pn2xitti]]

---
title: "{{title | safe}}"
aliases:
{#- Backwards compatible with older version, which had @title in the published url -#}
{% set tempAlias = ["%40", citekey] | join | safe -%}
- "{{ tempAlias }}"
{% if shortTitle %}- {{shortTitle | safe}}
{% endif -%}
Year: {{date | format("YYYY")}}
tags: type/source
Authors: {{authors}}{{directors}}
permalink: "{{citekey | safe }}"
---
{#- These can be changed -#}
{#- This is the order in which the annotations are ordered -#}
{%-
set categoryHeading = {
"orange": "Main ideas and conclusions",
"yellow": "Ordinary notes",
"blue": "Quote / quotable",
"green": "Important To Me",
"red": "Disagree With Author",
"purple": "Interesting side-point",
"magenta": "Methodology",
"grey": "Definitions and concepts"
}
-%}
{%-
set categoryIcon = {
"orange": "💡",
"yellow": "📚",
"blue": "💬",
"green": "💚",
"red": "⛔",
"purple": "💭",
"magenta": "⚙️",
"grey": "🧩"
}
-%}
{#- ---------- Don't make any changes under here --------- #}
{%- macro minEditDate() -%}
{%- set tempDate = "" -%}
{%- for a in annotations -%}
{%- set testDate = a.date | format("YYYY-MM-DD#HH:mm:ss") -%}
{%- if testDate < tempDate or tempDate == ""-%}
{%- set tempDate = testDate -%}
{%- endif -%}
{%- endfor -%}
{%- for a in notes -%}
{%- set testDate = a.dateModified | format("YYYY-MM-DD#HH:mm:ss") -%}
{%- if testDate < tempDate or tempDate == ""-%}
{%- set tempDate = testDate -%}
{%- endif -%}
{%- endfor -%}
{{tempDate }}
{%- endmacro -%}
{# infer latest note date #}
{%- macro maxEditDate() -%}
{%- set tempDate = "" -%}
{%- for n in annotations -%}
{%- set testDate = n.date | format("YYYY-MM-DD#HH:mm:ss") -%}
{%- if testDate > tempDate or tempDate == ""-%}
{%- set tempDate = testDate -%}
{%- endif -%}
{%- endfor -%}
{%- for n in notes -%}
{%- set testDate = n.dateModified | format("YYYY-MM-DD#HH:mm:ss") -%}
{%- if testDate > tempDate or tempDate == ""-%}
{%- set tempDate = testDate -%}
{%- endif -%}
{%- endfor -%}
{{tempDate}}
{%- endmacro -%}
{#- handle | characters in zotero strings used in MD -#}
{% macro formatCell(cellText) -%}
{{ cellText | replace("|","❕")}}
{%- endmacro %}
{#- TAGS: handle space characters in zotero tags -#}
{%- set space = joiner(' ') -%}
{%- macro printTags(rawTags) -%}
{%- if rawTags.length > 0 -%}
{%- for tag in rawTags -%}
#{{ tag.tag | lower | replace(" ","_") }} {{ space() }}
{%- endfor -%}
{%- endif -%}
{%- endmacro %}
{%-
set zoteroColors = {
"#ff6666": "red",
"#f19837": "orange",
"#5fb236": "green",
"#ffd400": "yellow",
"#2ea8e5": "blue",
"#a28ae5": "purple",
"#e56eee": "magenta",
"#aaaaaa": "grey"
}
%}
# {{title}}
> [!info] Info - [**Zotero**]({{desktopURI}}) {% if DOI %}| [**DOI**](https://doi.org/{{DOI}}){% endif %} | Local {% for attachment in attachments | filterby("path", "endswith", ".pdf") %}[**PDF**](file:///{{attachment.path | replace(" ", "%20")}}){%- endfor %}
{% if ShortTitle %}> Short title: {{shortTitle | safe}}{% endif -%}
> Authors: {% for c in creators %}[[{{c.firstName}} {{c.lastName}}]] {% endfor %}
{% if publicationTitle %}> Publication: {{publicationTitle | safe}}{% endif -%}
> Type: {{itemType}}
> Year: {{date | format("YYYY")}}
> Zotero links: [Item]({{select}}) {% if pdfZoteroLink %}PDF: {{pdfZoteroLink}}{% endif %}
> Web links: {% if itemType == "webpage" %}[{{title}}]({{url}}){%else%}[Item]({{uri}}){%endif%} {% if pdfLink %}PDF: {{pdfLink}}{% endif %}
> {{printTags(note.tags)}}
>
> **History**
> Date item added to Zotero: [[{{dateAdded | format("YYYY-MM-DD")}}]]
{%- if annotations %}
> First date annotations or notes modified: [[{{minEditDate() | truncate(10,true,"")}}]]
> Last date annotations or notes modified: [[{{maxEditDate()| truncate(10,true,"")}}]]
{%- endif %}
> Export date: [[{{exportDate | format("YYYY-MM-DD")}}]]
{%- if relations.length > 0 %}
{{ "" }}
> [!abstract] Related Zotero items ({{ relations.length}}):
>
> | title | proxy note | desktopURI |
> | --- | --- | --- |
{%- for r in relations %}
> | {{formatCell(r.title)}} | [[@{{r.citekey}}]] | [Zotero Link]({{r.desktopURI}}) | {% if rel.DOI %}> DOI: {{rel.DOI}}{% endif %} |
{%- endfor -%}
{{ "" }}
{%- endif %}
{% if notes.length > 0 %}
> [!note] Notes ({{notes.length}})
{{ "" }}
{%- for note in notes -%}
{#- Clean up note, change heading level, just in case -#}
> {{ note.note | replace ("# ","### ") | replace(r/\n/g, '\n> ')}}
> {{printTags(note.tags)}}
> <small>📝️ (modified: {{ note.dateModified | format("YYYY-MM-DD") }}) [link](zotero://select/library/items/{{note.key}}) - [web]({{note.uri}})</small>
> {{ "" }}
> ---
{% endfor %}
{% endif -%}
> [!abstract]-
> {% if abstractNote %}
> {{abstractNote|replace("\n"," ")}}
> {% endif %}
---
## Persistant notes
{% persist "notes" %}
{% endpersist %}
---
## Annotations
{% for color, colorCategorie in zoteroColors %}
{%- for entry in annotations | filterby ("color", "startswith", color) -%}
{%- if entry.id %}
{% if entry and loop.first %}## {{categoryIcon[zoteroColors[entry.color]]}} {{categoryHeading[zoteroColors[color]]}}{% endif %}
{% set annoID = ["id-", entry.id] | join | lower %}
{%- persist annoID %}
{%- if entry.date > lastExportDate %}
{%- if entry.annotatedText %}{{categoryIcon[zoteroColors[entry.color]]}} {{ entry.annotatedText }}{% endif %}
{%- if entry.imageBaseName %}![[@{{citekey}}/{{entry.imageBaseName}}|300]]
{% endif %}
{{printTags(entry.tags)}}<small>([{{title | truncate(50)}} p.{{entry.pageLabel}}](zotero://open-pdf/library/items/{{entry.attachment.itemKey}}?page={{entry.page}}&annotation={{entry.id}})) edited:[[{{ entry.date | format("YYYY-MM-DD")}}]]</small>
{%- endif %}
{% endpersist %} {# do not add "-", entry-id needs newline #}
^{{ entry.id | lower }}
{%- if entry.comment %}
>[!note]
>{{entry.comment}}
{% endif -%}
{% endif -%} {# /if entry.id -#}
{% endfor -%}
{% endfor -%}
@lsolesen
Copy link

This is a very nice template. However for multiline notes it breaks, and I have not been able to figure out how to solve it.

Obsidian_QrUAskEvhk

Were you able to solve this somehow @QWxleA?

@timalexdb
Copy link

timalexdb commented Jul 28, 2023

@lsolesen replace newlines!

{{ entry.annotatedText | replace(r/\n/g, ' ') }}

if using above template as-is, edit on line 156

@QWxleA
Copy link
Author

QWxleA commented Jul 29, 2023

Updated gist with the help of @timalexdb 's snippet (dankjewel), try it out and see if this is to your liking .

Another edit, I broke page-links, repaired

@lsolesen
Copy link

lsolesen commented Jul 29, 2023 via email

@QWxleA
Copy link
Author

QWxleA commented Jul 29, 2023

Thanks. But the issue is not the annotated text but the note.note that gets rendered without the > when there is multilines. And there it does not make sense just to remove the newlines because then it gets unreadable. lør. 29. jul. 2023 12.08 skrev Alex @.***>:

Could you post a broken annotation, so I can see what it does? i updated the template with something that might work: This is a page with an attached note with multiple paragraphs in it, rendered correctly, is that what you meant (don't look at the broken annotation, the pdf is a broken scan or something)? https://qwxlea.org/Zotero/%40clark.chalmers_1998

@timalexdb
Copy link

@lsolesen ah, responded too fast. I'm not using the same template and now don't have the time to check thoroughly, but:

{{ entry.comment | replace(r/\n/g, '\n> ')
(line 165)

Basically callout syntax requires each line, including empty, to start with '> '. Did a quick try-out with my own template and this should work, as long as you don't have newline-separated enumerations/lists or are using HTML tags to style your comments further.

If I find time in the coming days I can look into this further. More a patch than a solution but might help you on your way.

@QWxleA
Copy link
Author

QWxleA commented Jul 29, 2023

That would be great!

@lsolesen
Copy link

@timalexdb Thanks. It works when adding your suggested regex.

@lsolesen
Copy link

https://gist.github.com/QWxleA/c7f08e8feded332736a6b376045d0fe4#file-zotero-template-L163 --> Maybe it is possible to use the settings for assets from the plugin here?

@lsolesen
Copy link

lsolesen commented Jul 30, 2023

For some reason I cannot get this line to render correctly - https://gist.github.com/QWxleA/c7f08e8feded332736a6b376045d0fe4#file-zotero-template-L166 - It does not render as a link.

obsidian-template

Skærmbillede 2023-07-30 083826

@lsolesen
Copy link

I can see that if I remove <small> it starts rendering as a link.

@lsolesen
Copy link

lsolesen commented Jul 31, 2023

Another thing is - that if I import the note twice everything under the annotations-headline gets deleted - while in the notes-section everything stays there. However, nothing is updated with new comments. I do not know whether this has something to do with the Zotero integration, Obsidian or the template.

@timalexdb
Copy link

I can see that if I remove <small> it starts rendering as a link.

Yeah you can't use HTML tags with Markdown linking. You could try HTML linking to combine

@lsolesen
Copy link

lsolesen commented Aug 1, 2023

@timalexdb It is just a little strange, as this renders correctly under the notes section:

<small>📝️ (modified: 2023-07-30) [link](zotero://select/library/items/59CJ73IB) - [web](http://zotero.org/users/4490325/items/59CJ73IB)</small>

But this under the Annotations-section does not?

<small>([Dit personlige ledelsesrum p.25](zotero://open-pdf/library/items/5KM2TWP6?page=25&annotation=N6W2LJWF)) edited:[[2023-07-30]]</small>

@timalexdb
Copy link

@lsolesen ah, might've been a bit assumptuous of me. I only got the two working when using the HTML ref style. You could try that as an alternative, or settle for regular-sized notes. The replace newline thing for me works when not using the tag (along with bulleted/numbered lists), but if you'd really like to keep it you could try the following:

{{ entry.comment | replace(r/\n/g, '</small>\n> <small>') }}

I assume this would still break numbered lists, though. And make sure the closing tag is present for your annotation already: this snippet only handles the tags surrounding a newline.

@lsolesen
Copy link

lsolesen commented Aug 1, 2023

@timalexdb Very strange. The link works when in the

>[!note]
> ...

But not outside that. I will just drop the that it shows with small text - it is not too important to me.

Another thing is - that if I import a reference twice everything under the annotations-headline gets deleted - while in the notes-section everything stays there.

However, nothing is updated with new comments made in Zotero. I do not know whether this has something to do with the Zotero integration, Obsidian or the template.

@timalexdb
Copy link

@lsolesen I think that's to do with how you persist annotations in your template. So it has to do with (your implementation of) the ZI plugin

@QWxleA
Copy link
Author

QWxleA commented Aug 5, 2023

Updated with some minor edits, nothing should break. If an annotation has been imported once, it will not be imported again. The easiest way to fix things is removing and reimporting from Zotero.

Links should work, if not let me know (they work here).

@pclavell
Copy link

Hello, your template is great, but I am facing an issue that I do not know how to solve. Every annotation (highlighted text) imported has this code surrounding it. %% begin id-XXXX%%
image
How can I get rid of all these marks before and after the annotation? Thanks a lot

@QWxleA
Copy link
Author

QWxleA commented Aug 24, 2023

@pclavell Added a Readme, let me know if it's unclear. You should not remove those markers, they are needed for persistence. They should be invisible in reading-mode

@Nikko-A
Copy link

Nikko-A commented Dec 10, 2023

Hello! I've been using the template for a while and It's been generally nice! However, I needed to tinker with some of the settings yet I still have some issues. I'd like to ask for some help.

  1. The new property front matter gets broken from this. I tried removing some of the hyphens which may have been causing the issue yet the problem persisted.
  2. The area highlights break unless specified in the annotation side. I think there should be a part in the readme that talks about how it could be mitigated by fixing the path in the annotations part.
  3. The multi-line notes break unless you add the suggested regex by @timalexdb
  4. Changing color hierarchy does not work unless you change them at the part where hexcodes are.

@timalexdb
Copy link

timalexdb commented Jan 12, 2024

Hello! I've been using the template for a while and It's been generally nice! However, I needed to tinker with some of the settings yet I still have some issues. I'd like to ask for some help.

1. The new property front matter gets broken from this. I tried removing some of the hyphens which may have been causing the issue yet the problem persisted.

2. The area highlights break unless specified in the annotation side. I think there should be a part in the readme that talks about how it could be mitigated by fixing the path in the annotations part.

3. The multi-line notes break unless you add the suggested regex by @timalexdb

4. Changing color hierarchy does not work unless you change them at the part where hexcodes are.

For 1, could you post an example of broken YAML and explain how it breaks (e.g. invalid YAML or not displaying in Obs properties format)?

@ceciliechrstnsn
Copy link

@lsolesen ah, responded too fast. I'm not using the same template and now don't have the time to check thoroughly, but:

{{ entry.comment | replace(r/\n/g, '\n> ') (line 165)

Basically callout syntax requires each line, including empty, to start with '> '. Did a quick try-out with my own template and this should work, as long as you don't have newline-separated enumerations/lists or are using HTML tags to style your comments further.

If I find time in the coming days I can look into this further. More a patch than a solution but might help you on your way.

I am doing something wrong. Where should this be added/what should this replace? Thanks, @timalexdb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment