Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request]: Highlight pre- and post-context #50

Open
2 tasks done
jez opened this issue Dec 29, 2024 · 6 comments · May be fixed by #51
Open
2 tasks done

[Request]: Highlight pre- and post-context #50

jez opened this issue Dec 29, 2024 · 6 comments · May be fixed by #51
Labels
enhancement New feature or request question Further information is requested
Milestone

Comments

@jez
Copy link

jez commented Dec 29, 2024

Feature Request Checklist

What feature would you like to request?

What I'd really like is some way to take input like this:

highlight = the thing I highlighted
contextualText = Something before the thing I highlighted, and then something after

and produce this:

🎯 Highlight:: Something before the thing I highlighted, and then something after.

One way I can think to achieve this would be if there were a Handlebars helper function to get the contextual text before and after the highlighted text (or, to have two additional template variables, contextualTextPre and contextualTextPost, which achieve the same thing).

If there's already a way to achieve this with functionality built into this plugin or Handlebars itself, I'd love to learn about it.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jez jez added enhancement New feature or request question Further information is requested labels Dec 29, 2024
@jez jez linked a pull request Dec 29, 2024 that will close this issue
8 tasks
@bandantonio
Copy link
Owner

Hi @jez
Thank you for the request and time to make a PR.

Did I understand you correctly that your idea is to basically highlight the highlighted portion within the contextualText?
I believe we can do this a bit more easier by introducing just one helper (instead of two) that would output the result with the expected formatting. Something like this:

{{highlightWithinContext highlight contextualText}}

The helper function would look like this:

Handlebars.registerHelper('highlightWithinContext', (highlight: string, contextualText: string) => {
  // there must always be a highlight within the contextual text
  // so it is possible to wrap that portion with <mark> tag and return it
  const highlightWithinContext = contextualText.replace(highlight, `<mark>${highlight}</mark>`);
  
  return highlightWithinContext;
});

I should also mention that the contextualText already includes "something" before and after. But since it is automatically scoped to the nearest sentence(s) only, in some cases, the highlight will match the contextualText, so there wouldn't be any text surrounding the highlight. This behavior is described in the documentation.

@jez
Copy link
Author

jez commented Jan 7, 2025

@bandantonio The difference between the solution you propose and the one I propose is that in mine, the choice of mark is configurable.

Continuing with your solution, it would be possible to accept markStart and markEnd arguments to highlightWithinContext, possibly defaulting to <mark> / </mark>. But for example, maybe you want to mark the text with <strong> / </strong> instead, or some other custom markup.

I should also mention that the contextualText already includes "something" before and after. But since it is automatically scoped to the nearest sentence(s) only, in some cases, the highlight will match the contextualText, so there wouldn't be any text surrounding the highlight. This behavior is described in the documentation.

Yep, understood, that's fine. I'm personally just trying to recreate this effect from the Books app:

image

So if the highlight and contextualText are the same, it's fine for the way it gets formatted to just have the whole thing wrapped in begin/end markers.

@jez
Copy link
Author

jez commented Jan 7, 2025

I have no strong opinion about either solution (one helper or two) in so far as it supports choosing what to use for the begin and end marker.

@bandantonio
Copy link
Owner

@jez Thanks for the explanation, now this makes more sense.
As for the markers, for the first iteration, I would introduce a list of allowed tags (for example, b/strong, i/em, mark, pre) as common ones for text formatting.

@jez
Copy link
Author

jez commented Jan 9, 2025

Thanks but I’m really hoping for arbitrary markers.

My intention is to use markup like

<span style="color: red;">red</span>

with custom inline CSS to recreate the highlights with the same colors as used by the Books app. There is even an example of doing so in the documentation, except it doesn’t support showing the contextual text before and after the highlight:

https://mister-gold.pro/obsidian-apple-books-highlights-plugin/customization/templates-and-variables.html#custom-templates

There’s also the question of how some people might want to use markdown markers (**) instead of HTML tags.

In my opinion, it’s not sufficient to artificially restrict the markers.

@vsheg
Copy link

vsheg commented Jan 16, 2025

This looks exactly like what I need. Thank you. Please add also an option to increase the context: +2 sentences at each side, +100 words at each side, the whole paragraph, etc.

Books.app also supports different colors and underlining. Hope this will be supported too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
Development

Successfully merging a pull request may close this issue.

3 participants