Skip to content

Instantly share code, notes, and snippets.

@johannrichard
Last active August 4, 2024 13:55

Revisions

  1. johannrichard revised this gist Aug 4, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Header Template.jinja
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ _by: [[{{ author | replace('\n', ']], [[')}}]]_
    > ![|300]( {{ cover_image_url }}) {% endif %}
    > Authors: [[{{ author | replace('\n', ']], [[')}}]]
    > Category: {{ category }}
    > Number of Highlights: =={{ num_highlights }}==
    > Number of Highlights: {{ num_highlights }}
    > Readwise URL: {{ highlights_url }}
    > Source URL: [{{ source_url }}]({{ source_url }})
    > Date: [[{{ updated }}]]
  2. johannrichard revised this gist Aug 4, 2024. 3 changed files with 12 additions and 11 deletions.
    6 changes: 3 additions & 3 deletions Frontmatter Template.jinja
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,14 @@
    ---
    id: {{ id }}
    created: {{ updated }}
    {#- For fields like title and aliases, remove linebreaks and either add spaces (title, alias) or create multiple list entries (author) -#}
    {# For fields like title and aliases, remove linebreaks and either add spaces (title, alias) or create multiple list entries (author) #}
    title: "{{ title | replace('\n', ' ')}}"
    aliases: ["{{ title | replace('\n', ' ') }}"{%- if sanitized_title %}, "{{sanitized_title}}"{%- endif %}]
    author: ["{{ author | replace('\n', '", "')}}"]
    category: "{{ category }}"
    highlights: {{ num_highlights }}
    last_highlight_at: {{ last_highlight_at }}
    source: {{ unique_url }}
    {#- Replace special characters in tags. Eventually, some of that might end up in the plugin as well -#}
    {# Replace special characters in tags. Eventually, some of that might end up in the plugin as well #}
    tags: [ {%- if tags_nohash %}{{ tags_nohash | replace('&', 'and') | replace('@', '') }},{%- endif %}{%- if hl_tags_nohash %}{{ hl_tags_nohash | replace('&', 'and') | replace('@', '')}}{%- endif %} ]
    ---
    ---
    12 changes: 7 additions & 5 deletions Header Template.jinja
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    {#- Header Template
    This is included once, and used to display variables of a Readwise Document (e.g. Author, Title, etc.) -#}
    {# Header Template
    This is included once, and used to display variables of a Readwise Document (e.g. Author, Title, etc.) #}
    %%
    ID: {{ id }}
    Updated: {{ updated }}
    %%
    # {{ title }}
    {#- Replace linebreaks and create multiple *Author* entries -#}
    {# Replace linebreaks and create multiple *Author* entries #}

    _by: [[{{ author | replace('\n', ']], [[')}}]]_

    > [!info]-
    @@ -36,11 +37,12 @@ _by: [[{{ author | replace('\n', ']], [[')}}]]_
    {%- endif %}

    ## Incoming Links
    {#- This requires that the dataview plugin is installed. It will show all incoming links to this file -#}
    {# This requires that the dataview plugin is installed. It will show all incoming links to this file #}

    ```dataview
    LIST FROM [[]] SORT file.frontmatter.created DESC
    ```

    ## Highlights
    {#- After this heading, the highlights will follow, using the Highlight Template -#}
    {# After this heading, the highlights will follow, using the Highlight Template #}

    5 changes: 2 additions & 3 deletions Highlight Template.jinja
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    {#- Highlight template that does a few things:
    {# Highlight template that does a few things:
    - Convert Readwise's "Highlight" syntax (__Highlight__) to Obsidian Highlight Syntax (==Highlight==)
    - Link to the Source and to the Highlight in Readwise
    - Add block-id's both for the highlight and the note (Please be aware that line breaks, lists etc. in a note might create multiple blocks and referencing the "block-id-note" block might only link to the last block of the note.
    -#}
    #}
    > [!quote]
    > {{ text | bq | replace('__', '==') }}
    {%- if category == 'books' %}
    @@ -13,7 +13,6 @@
    ^{{ id }}-note
    {% endif %}


    ({% if url %}[Source]({{ url }}), {% endif %}[Readwise](https://readwise.io/open/{{ id }}), [[{{ highlighted_at }}|{{ highlighted_at }}]])

    ----
  3. johannrichard revised this gist Aug 4, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Highlight Template.jinja
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    - Add block-id's both for the highlight and the note (Please be aware that line breaks, lists etc. in a note might create multiple blocks and referencing the "block-id-note" block might only link to the last block of the note.
    -#}
    > [!quote]
    > {{ text | bq | replaace('__', '==') }}
    > {{ text | bq | replace('__', '==') }}
    {%- if category == 'books' %}
    > (*Original*: [{{ location }}]({{ locationUrl }})){%endif %}
    ^{{ id }}
  4. johannrichard created this gist Aug 4, 2024.
    15 changes: 15 additions & 0 deletions Frontmatter Template.jinja
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    {#- Frontmatter Template - also called *Properties* in Obsidian -#}
    ---
    id: {{ id }}
    created: {{ updated }}
    {#- For fields like title and aliases, remove linebreaks and either add spaces (title, alias) or create multiple list entries (author) -#}
    title: "{{ title | replace('\n', ' ')}}"
    aliases: ["{{ title | replace('\n', ' ') }}"{%- if sanitized_title %}, "{{sanitized_title}}"{%- endif %}]
    author: ["{{ author | replace('\n', '", "')}}"]
    category: "{{ category }}"
    highlights: {{ num_highlights }}
    last_highlight_at: {{ last_highlight_at }}
    source: {{ unique_url }}
    {#- Replace special characters in tags. Eventually, some of that might end up in the plugin as well -#}
    tags: [ {%- if tags_nohash %}{{ tags_nohash | replace('&', 'and') | replace('@', '') }},{%- endif %}{%- if hl_tags_nohash %}{{ hl_tags_nohash | replace('&', 'and') | replace('@', '')}}{%- endif %} ]
    ---
    46 changes: 46 additions & 0 deletions Header Template.jinja
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    {#- Header Template
    This is included once, and used to display variables of a Readwise Document (e.g. Author, Title, etc.) -#}
    %%
    ID: {{ id }}
    Updated: {{ updated }}
    %%
    # {{ title }}
    {#- Replace linebreaks and create multiple *Author* entries -#}
    _by: [[{{ author | replace('\n', ']], [[')}}]]_

    > [!info]-
    {%- if category == 'books' or category == 'supplementals' %}
    > ![|300]( {{ cover_image_url }}) {% endif %}
    > Authors: [[{{ author | replace('\n', ']], [[')}}]]
    > Category: {{ category }}
    > Number of Highlights: =={{ num_highlights }}==
    > Readwise URL: {{ highlights_url }}
    > Source URL: [{{ source_url }}]({{ source_url }})
    > Date: [[{{ updated }}]]
    > Last Highlighted: [[{{ last_highlight_at }}]]

    {%- if summary %}
    ## Summary

    {{ summary }}
    ^summary
    {%- endif %}

    ---

    {%- if document_note %}
    ## Document Note

    {{ document_note }}
    ^document-note
    {%- endif %}

    ## Incoming Links
    {#- This requires that the dataview plugin is installed. It will show all incoming links to this file -#}

    ```dataview
    LIST FROM [[]] SORT file.frontmatter.created DESC
    ```

    ## Highlights
    {#- After this heading, the highlights will follow, using the Highlight Template -#}
    19 changes: 19 additions & 0 deletions Highlight Template.jinja
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    {#- Highlight template that does a few things:
    - Convert Readwise's "Highlight" syntax (__Highlight__) to Obsidian Highlight Syntax (==Highlight==)
    - Link to the Source and to the Highlight in Readwise
    - Add block-id's both for the highlight and the note (Please be aware that line breaks, lists etc. in a note might create multiple blocks and referencing the "block-id-note" block might only link to the last block of the note.
    -#}
    > [!quote]
    > {{ text | bq | replaace('__', '==') }}
    {%- if category == 'books' %}
    > (*Original*: [{{ location }}]({{ locationUrl }})){%endif %}
    ^{{ id }}
    {%if note %}
    {{ note }}
    ^{{ id }}-note
    {% endif %}


    ({% if url %}[Source]({{ url }}), {% endif %}[Readwise](https://readwise.io/open/{{ id }}), [[{{ highlighted_at }}|{{ highlighted_at }}]])

    ----
    16 changes: 16 additions & 0 deletions Readwise Mirror Templates.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # What?

    These three files are my (current) Templates for the [Readwise Mirror](https://github.com/jsonMartin/readwise-mirror) plugin for Obsidian. They include (some) documentation.

    # Why?

    They started to become complex enough to warrant some revision control, for which gist is perfect. They're probably useful for others too, hence the public gist.

    # How?

    * Install the plugin
    * Fill in the Header Template, the Frontmatter Template, and the Highlight Template
    * Use a Jinja Parser & Template engine (e.g. [this one](https://j2live.ttl255.com)[^1] to preview the files.


    [^1]: I'm not affiliated but found this one to be sufficiently simple to test *basic* templates. However, be aware that the plugin's own functions (e.g. `bq`) can not be parsed and rendered by third party parsers.