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;