Skip to content

Commit

Permalink
Refactor headword conversion for multiple readings
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jan 21, 2024
1 parent fd37323 commit 7cbacff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/util/yomitan/convertHeadwordsToSC.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { convertReadingToRubySC } from './parseTextToSC.js';

/**
* Converts headword(s) to structured content.
* @param {TextReadingPair[]} headwords
* @param {Headword[]} headwords
*/
function convertHeadwordsToSC(headwords) {
const headwordsSCList = headwords.map(headwordToSC);
const headwordsSCList = headwordsToSC(headwords);
const separator = '・';
/**
* @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]}
Expand Down Expand Up @@ -36,11 +36,22 @@ function convertHeadwordsToSC(headwords) {

/**
* Converts a headword to structured content.
* @param {TextReadingPair} headword
* @returns {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent}
* @param {Headword[]} headwords
* @returns {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]}
*/
function headwordToSC(headword) {
return convertReadingToRubySC(headword);
function headwordsToSC(headwords) {
/**
* @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]}
*/
const headwordsSCList = [];
for (const headword of headwords) {
headwordsSCList.push(
...headword.readings.map((reading) =>
convertReadingToRubySC(headword.text, reading)
)
);
}
return headwordsSCList;
}

export { convertHeadwordsToSC };
20 changes: 15 additions & 5 deletions src/util/yomitan/parseTextToSC.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ function convertTextToSC(rawText, languageCode) {

try {
const readings = parseCantoneseReadings(phrase, reading);
return readings.map(convertReadingToRubySC);
return readings.map(({ text, reading }) =>
convertReadingToRubySC(text, reading)
);
} catch (error) {
return cleanedText;
}
Expand All @@ -44,17 +46,25 @@ function cleanRawText(rawText) {

/**
* Parses a text string into a structured content object with ruby text for readings
* @param {TextReadingPair} reading
* @param {string} text
* @param {string} reading
* @returns {import("yomichan-dict-builder/dist/types/yomitan/termbank").StructuredContent}
*/
function convertReadingToRubySC(reading) {
function convertReadingToRubySC(text, reading) {
// Check that both text and reading are type string, if not then cast to string
if (typeof text !== 'string') {
text = String(text);
}
if (typeof reading !== 'string') {
reading = String(reading);
}
return {
tag: 'ruby',
content: [
reading.text,
text,
{
tag: 'rt',
content: reading.reading,
content: reading,
},
],
};
Expand Down

0 comments on commit 7cbacff

Please sign in to comment.