From 1ff04811cbf58f2fba5a3fe87133e9c4402a2bac Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Sat, 12 Apr 2025 08:30:42 +0200 Subject: [PATCH] feat: Highlight term upon setting it SentenceListeningNote --- src/Entity/Note.php | 14 ++++++++++++++ src/Entity/SentenceListeningNote.php | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Entity/Note.php b/src/Entity/Note.php index d21e27a..db87505 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -94,4 +94,18 @@ class Note { return \DateTimeImmutable::createFromFormat('U', $this->mod); } + + + // -------------------------------------------------- Utility functions --- + + protected static function stringHighlight(string $haystack, string $needle) + { + $replace = sprintf( + '%s', + self::HIGHLIGHT_ATTR_KANJI, + $needle, + ); + + return str_replace($needle, $replace, strip_tags($haystack)); + } } diff --git a/src/Entity/SentenceListeningNote.php b/src/Entity/SentenceListeningNote.php index 3dd4f8c..ab2ce5d 100644 --- a/src/Entity/SentenceListeningNote.php +++ b/src/Entity/SentenceListeningNote.php @@ -20,6 +20,10 @@ class SentenceListeningNote extends Note $this->fields['VocabKanji'] = $term->getKanji(); $this->fields['VocabFurigana'] = $term->getReading(); $this->fields['VocabDef'] = $term->toAnkiVocabDef(); + $this->fields['SentKanji'] = $this->stringHighlight( + $this->fields['SentKanji'], + $term->getKanji(), + ); $this->term = $term; return $this; } @@ -55,8 +59,6 @@ class SentenceListeningNote extends Note // Related fields are updated using the setter $slNote->setTerm($term); - // Remove highlighting - $slNote->fields['SentKanji'] = strip_tags($slNote->fields['SentKanji']); // Reset relations and basic data $slNote->id = null; $slNote->model = self::MODEL_NAME;