gregorias / anki-code-highlighter

Anki plugin for code syntax highlighting.
GNU Affero General Public License v3.0
54 stars 3 forks source link

Error on decks #60

Closed ExtremeCoolDude closed 10 months ago

ExtremeCoolDude commented 1 year ago

I'm using a premade deck and whenever I try to highlight, I get this error :

The selected code contains partial HTML tags. Clean up your code snippet in the HTML editor (https://docs.ankiweb.net/editing.html#editing-features) before highlighting.

image

It's not very practical to edit every html card in a premade deck with 1000 cards. I am wondering if there is a solution. Thanks!

ExtremeCoolDude commented 1 year ago

Sometimes also throws the same error, when there is nothing to clean:

image

gregorias commented 11 months ago

Hi @ExtremeCoolDude,

Regarding the first issue, a solution would be to implement an automatic way to clean up HTML from cards. You could also write a Python script yourself that cleans up your cards.
At this moment, I don't have the capacity to work on that but I welcome contributions.

Regarding the second issue, could you open a bug ticket and provide reproduction steps? I can't reproduce your issue, so I don't know how to fix it.

Thank you for reporting.

ExtremeCoolDude commented 11 months ago

Steps to reproduce:

  1. Create a test deck
  2. Create a basic card front and back like this :

image

on back : press enter twice to have two br tags, create a <div> a </div> in the html editor

  1. Highlight a block of test to add to code block by dragging the cursor over text
  2. Press ctrl + ' to invoke the add on:

This happens on my machine :

image

Please let me know if this is not reproducible. That would mean the issue is with my Anki version. (Version ⁨2.1.61 (0c1eaf4c)⁩ Python 3.9.15 Qt 6.4.2 PyQt 6.4.0)

gregorias commented 11 months ago

WAI. You are selecting a text block with HTML tags in them, which the current tool is not sophisticated enough to handle. You need to clean up HTML tags before applying a highlight.

ExtremeCoolDude commented 11 months ago

WAI ? I'm just a tech hobbyist, with no formal education in tech. Maybe make the message clear to something like " remove all html code from editor" instead of "remove partial tags"

gregorias commented 10 months ago

I've taken a deeper look into this and made the error message clearer.

I've been mistaken in my previous explanation. The problem is that you are selecting a partial non-Text node. The background is that I'm using Range.surroundContents to implement essential functionality and it doesn't work with partial nodes (https://developer.mozilla.org/en-US/docs/Web/API/Range/surroundContents). In other words, you just need to make sure you don't select half of div or span or something like that. It should be much better than cleaning entire HTML tags.

I've made the error message clearer in the latest CL to address the issue. I don't intend to make the code work with partial selection as I don't have the time to do that. :(

Thank you for reporting and your patience. Let me know though if you have further ideas on how to make this better.