Skip to content

Commit ca6298d

Browse files
authored
Fix tone colors for hyphenated pinyin (#91)
Previously, pinyin like yi1 mu2 - yi1 yang4 would break color coding. This should probably be a data fix instead, but this should work, too.
1 parent 23758f3 commit ca6298d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

public/js/modules/explore.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ let runTextToSpeech = function (text, anchors) {
105105
};
106106

107107
let renderPinyinForDefinition = function (pinyin, container) {
108-
const syllables = pinyin.split(' ');
108+
const syllables = getSyllables(pinyin);
109109
for (const syllable of syllables) {
110110
let syllableElement = document.createElement('span');
111111
// TODO: could just do this with CSS (.tonewhatever under .canto: no color coding)
@@ -122,7 +122,7 @@ function modifyHeaderTones(definitionList, word) {
122122
}
123123
// TODO just send the pinyin on the word set....
124124
const wordHolders = document.getElementsByClassName(`${word}-header`);
125-
const syllables = definitionList[0].pinyin.split(' ');
125+
const syllables = getSyllables(definitionList[0].pinyin);
126126
for (const wordHolder of wordHolders) {
127127
const elementsToModify = wordHolder.querySelectorAll('.word-header-character');
128128
if (syllables.length !== elementsToModify.length) {
@@ -587,11 +587,21 @@ let renderCharacterHeader = function (character, container) {
587587
});
588588
container.appendChild(characterHolder);
589589
}
590+
591+
function getSyllables(pinyin) {
592+
// some CEDICT entries include a " - ", so like:
593+
// yi1 mu2 - yi1 yang4 rather than yi1 mu2 yi1 yang4
594+
// arguably that should be a data fix, but I guess
595+
// it looks slightly better for display that way.
596+
// but for tone color coding, we just want to have each
597+
// syllable pronunciation by itself.
598+
return pinyin.replace(' - ', ' ').split(' ');
599+
}
590600
let renderWordHeaderByCharacter = function (word, container) {
591601
const definitionList = definitions[word];
592602
let syllables = null;
593603
if (definitionList && definitionList[0].pinyin) {
594-
syllables = definitionList[0].pinyin.split(' ');
604+
syllables = getSyllables(definitionList[0].pinyin);
595605
if (syllables.length !== word.length) {
596606
syllables = null;
597607
}

public/js/modules/search-suggestions-worker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ function buildTries(wordSet, definitions) {
173173
// yixia or yi1xia4 for 一下)
174174
// however, we should also add all permutations (e.g., yixia4 could also return 一下)
175175
for (const pinyin of uniquePinyin) {
176-
const syllables = pinyin.split(' ');
176+
// not great to duplicate this logic in so many places but I don't feel like making this
177+
// worker file a bundle
178+
const syllables = pinyin.replace(' - ', ' ').split(' ');
177179
let joinedPinyin = syllables.join('');
178180
// could do replaceAll with regex, but that wasn't supported until more recently on
179181
// android webview

0 commit comments

Comments
 (0)