promnesia icon indicating copy to clipboard operation
promnesia copied to clipboard

How does highlighting work in promnesia ?

Open kvgc opened this issue 2 years ago • 9 comments

Hi, This is also related to #244.

Here's a webpage: https://en.wikipedia.org/wiki/Wiki.

Let's say I want promnesia to highlight the following text:

Wikis are enabled by wiki software, otherwise known as wiki engines.

How should my notes be formatted such that promnesia is able to highlight that particular text/section?


Here are a couple of things I tried:

  • I tried saving it as a markdown file as follows but that doesn't highlight the text :

[test-wiki](https://en.wikipedia.org/wiki/Wiki) : Wikis are enabled by wiki software, otherwise known as wiki engines

  • But if I save it as a org file instead :
*** https://en.wikipedia.org/wiki/Wiki
:TYPE: article
: Wikis are enabled by wiki software, otherwise known as wiki engines.

I am able to get promnesia to highlight the entire paragraph :

Is there a reason why this does not work for markdown?

Thanks!

kvgc avatar Mar 23 '22 02:03 kvgc

I am using the "auto" indexer to index the files :

from promnesia.common import Source
from promnesia.sources import (auto)
SOURCES = [
    Source(
        auto.index,
        '/path/to/files',
    )
]

kvgc avatar Mar 23 '22 02:03 kvgc

Hi! So it doesn't really depend on the indexer, and kind of 'expected' behaviour at the moment. Basically the lowest 'granularity' highlighting works on is some sort of 'outer' HTML element. E.g. in this case you can see that the smallest thing that the whole highlight is enclosed in in the <p> element which contains the whole paragraph.

image

And also it does it on line by line basis, that's why your first example [test-wiki](https://en.wikipedia.org/wiki/Wiki) : Wikis are enabled by wiki software, otherwise known as wiki engines didn't work. If you change it to

[test-wiki](https://en.wikipedia.org/wiki/Wiki) 
Wikis are enabled by wiki software, otherwise known as wiki engines 

, I think I'd expect it to start working -- so it's not really about org mode or markdown :)

The reason it's implemented that way is to keep the highlight implementation simple, since I didn't want to reinvent the wheel (it's fairly short, see here https://github.com/karlicoss/promnesia/blob/4443d810dddc94f5c842f43f79ed5d1785aed837/extension/src/sidebar.js#L241-L259) .

Implementing highlights properly would be pretty hard -- basically highlighting the exact match within text would require detecting exact text boundaries properly, and then wrapping the text in some sort of <span>, which might break the layout. Whereas current implementation reuses existing HTML element, just adds an extra CSS class, so it can't impact the page layout.

Ideally it would be nice to reuse some existing library (like Hypothes.is annotator). Also see relevant issue https://github.com/karlicoss/promnesia/issues/30

karlicoss avatar Mar 23 '22 09:03 karlicoss

Ah, I see. Yeah, you are right. The second example does indeed highlight. Thanks! I am totally okay with it highlighting the entire outerHTML. Finding and highlighting the exact text does indeed seem challenging.

kvgc avatar Mar 23 '22 21:03 kvgc

Just wanted to add that perhaps mark.js might also be an alternate way to achieve this. Here's an example: https://github.com/kvgc/mark-js-examples/blob/main/annotation.html

This produces the following output: image

kvgc avatar Jun 14 '22 08:06 kvgc

@Stvad suggested this https://github.com/GoogleChromeLabs/text-fragments-polyfill (although not sure if it's suitable for matching longer bits of hihglight)

karlicoss avatar Dec 31 '22 02:12 karlicoss

longer bits of hihglight

Not entirely sure what do you mean by that

Stvad avatar Dec 31 '22 02:12 Stvad

As in, I only seen text fragments work for short snippets (e.g. when you jump from google search). Not sure how well it would work if you clipped several paragraphs of text (promnesia would handle it correctly and highlight multiple paragraphs). Basically need to read https://github.com/GoogleChromeLabs/text-fragments-polyfill/blob/main/src/text-fragment-utils.js -- will do a bit later

karlicoss avatar Dec 31 '22 02:12 karlicoss

hmm, my expectation is that it should work just fine. I liked this write-up on fragment links: https://web.dev/text-fragments/

also you can just try it with https://github.com/GoogleChromeLabs/link-to-text-fragment extension

Stvad avatar Dec 31 '22 03:12 Stvad

Related : https://developer.mozilla.org/en-US/docs/Web/Text_fragments#urls_with_multiple_text_fragments

kvgc avatar Mar 14 '23 09:03 kvgc