I’m writing an addon that modifies the HTML content of {{sentence}} field by higlighting the {{vocab}} in it. I am trying to wrap vocab in a span tag but for some reason the HTML isn’t injected in the card.
init.py
from aqt import gui_hooks
from .highlighter import highlight_vocab
def on_add_note(note, _card):
if not note:
return
try:
vocab = note["vocab"]
sentence = note["sentence"]
highligted = highlight_vocab(vocab, sentence)
if highligted != sentence:
note["sentence"] = highligted
except Exception as e:
print(f"[Vocab highlighter] Error: {e}")
gui_hooks.add_cards_will_add_note.append(on_add_note)
highlighter.py
def highlight_vocab(vocab, sentence) :
vocab = vocab.strip()
sentence = sentence.strip()
if vocab and sentence :
if vocab in sentence:
return sentence.replace(vocab, f'<span class = "highlight">{vocab}</span>')
else:
return sentence
else:
return sentence