weichenw/obsidian-hypothesis-plugin

Better support for multiple-line highlights

Closed this issue ยท 10 comments

When highlighting the README page of Hypothesis like the following, the highlighted part cannot be styled well.

Not sure if the indentation of list items can be kept the same way as the source, but it would be great if the quotation (in the following example) can be kept for the highlighted text.

The source:
Screenshot 2022-02-07 at 16 33 17

The synced annotations:
Screenshot 2022-02-07 at 16 37 45

The improved/desired annotations:
Screenshot 2022-02-07 at 16 38 01

The template:

{% if is_new_article %}# {{title}}

{% if is_new_article %}## Metadata{% endif %}

{% if is_new_article %}{% if author %}- Author:: [{{author}}]({{authorUrl}}){% endif %}
- Title:: {{title}}
- Category Hypothesis:: Article{% endif %}
{% if url %}- Source URL:: {{url}}{% endif %}{% endif %}

{% if is_new_article %}## Highlights{% endif %}

{% for highlight in highlights %}> {{highlight.text}} (View Highlight [{{highlight.location}}]({{highlight.incontext}})) ^hs{{highlight.id}}{% if highlight.tags | length %}
- Tag: {% for tag in highlight.tags %}#{{tag| replace(" ", "-")}} {% endfor %}{% endif %}{% if highlight.annotation %}
- [ ] {{highlight.annotation}}{% endif %}{% if highlight.created %}
- Highlighted on {{highlight.created}}{% endif %}{% if highlight.updated %}
- Updated on {{highlight.updated}}{% endif %}

{% endfor %}
phgn0 commented

Edit: nevermind my previous comment, I misread the question.

Text selectors in hypothesis annotations just include the text, not any styling like bullet points. So without re-parsing the original HTML (very hard) we cannot reconstruct that.

What do you mean precisely by "if the quotation (in the following example) can be kept for the highlighted text."? That it adds the ">" before every inserted line? If so, I think that should be implemented in the template, since other people might want to style it differently...

@phgn0 I should have explained this more clearly. ๐Ÿ˜… The template I pasted there did not give me the desired result. The desired result was a manually modified version of the synced one with the current version of the plugin. Therefore, it would be great if the issue I mentioned can be fixed.

That it adds the ">" before every inserted line? If so, I think that should be implemented in the template, since other people might want to style it differently...

Yes, your interpretation is correct, and I agree that this can be implemented in the template. I wasn't sure how to do this with the template.

phgn0 commented

I think replacing > {{highlight.text}} with {% set lines = highlight.text.split("\n") %}{% for line in lines %}> {{ line + "\n" }}{% endfor %} might work. (plus the same for the annotation text)

It looks fairly nice in the new editor:
Screenshot 2022-02-22 at 10 13 54 PM

@wenlzhang Does this work for you?
@weichenw What do you think about adding this multi-line formatting to the default template?

@phgn0 Thanks, it works for me!

For annotation text, would it be possible to make the first line have a different style than other lines? This would help quickly take structured notes. The reason for having the first line as a task is that one can then use Dataview to query all annotation text.

For instance, the first line can be a task, and the second and third lines would be indented list items. This means that the annotation text

Line 1
Line 2
Line 3

would be synced as

- [ ] Line 1
    - Line 2
    - Line 3
phgn0 commented

Yes, adding something like {% if loop.first %}- [ ] {% else %} - {% endif %} before {{ line + "\n" }} should work I believe. See the templating docs here: https://mozilla.github.io/nunjucks/templating.html

To clarify, the reason you want to indent the second line is so that the checkmark applies to the entire annotation note?

I'm fairly happy with the minimalistic template I landed on, which puts everything on the top level:

Screenshot 2022-02-23 at 11 22 26 AM

Thanks for the help!

To clarify, the reason you want to indent the second line is so that the checkmark applies to the entire annotation note?

Yes, something like that. The first line can be synced as a task, and the second line can be an indented list item to that task. For annotation notes within one document, this way of handling annotation notes may not be very beneficial, and treating multiple lines of the annotation note as the same top level would work well as in your example.

The main reason for treating the second line as a sub-item to the task (first line) is that I can use Dataview to query/search for desired annotation notes in all Hypothesis documents synced to Obsidian.
https://github.com/blacksmithgu/obsidian-dataview

In my workflow, I handle the summary of the annotation (literature note) and thoughts on the annotation (potential permanent note) differently for easier distinguishing them later on:

  • If the annotation note is a summary, then it would be synced as - [ ] Summary.
  • If the annotation note is a thought, then it would be synced as - [ ] ==Thought==. When taking notes about thoughts, I would use ==== to highlight them.
  • Since I treat the first line of the annotation note as a task, then Dataview can be used to search for a keyword/tag, for example, within all documents in the Hypothesis folder. In the keyword, I can also include == to search for my thoughts, rather than the summary. This way, I can better arrange when to summarize related summary and thought.

In short, currently, Dataview has rather good query support for tasks but not for ordinary list items. Also, when searching for tasks, one can choose to show their sub-tasks or sub-items. This way, everything in the annotation note can show up in the query results. That's why I would like the first line to be a task and the second line to be an indented list item.

With the help from @phgn0 the following template fixes the multiple-line highlight and annotation for me.

For the annotation text part, I changed the way of taking notes a bit to make the multiple-line annotation also looks indented in Hypothesis's editor box. So, the following note:

Line 1
- Line 2
- Line 3

would be synced as

- [ ] Line 1
    - Line 2
    - Line 3

The template that works for me:

{% if is_new_article %}# {{title}}

{% if is_new_article %}## Metadata{% endif %}

{% if is_new_article %}{% if author %}- Author:: [{{author}}]({{authorUrl}}){% endif %}
- Title:: {{title}}
- Category Hypothesis:: Article{% endif %}
{% if url %}- Source URL:: {{url}}{% endif %}{% endif %}

{% if is_new_article %}## Highlights{% endif %}

{% for highlight in highlights %}{% set lines = highlight.text.split("\n") %}{% for line in lines %}> {{ line + "\n" }}{% endfor %} (View Highlight [{{highlight.location}}]({{highlight.incontext}})) ^hs{{highlight.id | replace("_", "-")}}{% if highlight.tags | length %}
- Tag: {% for tag in highlight.tags %}#{{tag| replace(" ", "-")}} {% endfor %}{% endif %}{% if highlight.annotation %}
{% set lines = highlight.annotation.split("\n") %}{% for line in lines %}{% if loop.first %}- [ ] {{ line + "\n" }}{% else %}{% if loop.last %}    {{ line }}{% else %}    {{ line + "\n" }}{% endif %}{% endif %}{% endfor %}{% endif %}{% if highlight.created %}
- Highlighted on {{highlight.created}}{% endif %}{% if highlight.updated %}
- Updated on {{highlight.updated}}{% endif %}

{% endfor %}

Hi @wenlzhang, I was wondering if you're still using your template or did you have any update for it? Thanks!

Hi @wenlzhang, I was wondering if you're still using your template or did you have any update for it? Thanks!

@nhan000 Yes, I am still using the template above to sync highlights and annotations into Obsidian, as I now have a task-centred workflow. Do you have anything specific to ask?

@wenlzhang Thanks for your reply. For my workflow, I want to sync all highlights with the order following the order of the highlights in the article. If I make more highlights, I want the markdown file to be overwritten instead of appending new highlights to the bottom.

I've settled down on the template that I want. However, it seems that my highlights synced into Obsidian only in the temporal order that I make them, and not the order that they appear in the article. I don't know how I can fix that.

Edit: It's a known issue and the author won't implement this feature #30 (comment)