Skip to content

Commit 8a3de86

Browse files
committed
Make structured content extraction for glossary-plain recursive to more easily ensure correct ordering
<rikaitan.link>ZjI3MWZjMGRhM2U1NWE5OGZhOTFjOTgzNGQ3NWZjY2M5NmRlYWUyNwo=</rikaitan.link>
1 parent 793ebd3 commit 8a3de86

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

ext/js/templates/anki-template-renderer.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -781,28 +781,40 @@ export class AnkiTemplateRenderer {
781781
}
782782

783783
/**
784-
* @param {import('dictionary-data').TermGlossaryStructuredContent} content
785-
* @param {StructuredContentGenerator} structuredContentGenerator
786-
* @returns {string[]}
784+
* @param {import('structured-content.js').Content[]} content
785+
* @returns {import('structured-content.js').Content[]}
787786
*/
788-
_extractGlossaryData(content, structuredContentGenerator) {
787+
_extractGlossaryStructuredContentRecursive(content) {
789788
/** @type {import('structured-content.js').Content[]} */
790-
const glossaryContentQueue = [];
791-
const structuredContentQueue = [content.content];
792-
while (structuredContentQueue.length > 0) {
793-
const structuredContent = structuredContentQueue.shift();
789+
const extractedContent = [];
790+
while (content.length > 0) {
791+
const structuredContent = content.shift();
794792
if (Array.isArray(structuredContent)) {
795-
structuredContentQueue.push(...structuredContent);
796-
} else if (typeof structuredContent === 'object' && structuredContent.content) {
793+
extractedContent.push(...this._extractGlossaryStructuredContentRecursive(structuredContent));
794+
} else if (typeof structuredContent === 'object' && structuredContent) {
797795
// @ts-expect-error - Checking if `data` exists
798796
if (structuredContent.data?.content === 'glossary') {
799-
glossaryContentQueue.push(structuredContent);
797+
extractedContent.push(structuredContent);
800798
continue;
801799
}
802-
structuredContentQueue.push(structuredContent.content);
800+
if (structuredContent.content) {
801+
extractedContent.push(...this._extractGlossaryStructuredContentRecursive([structuredContent.content]));
802+
}
803803
}
804804
}
805805

806+
return extractedContent;
807+
}
808+
809+
/**
810+
* @param {import('dictionary-data').TermGlossaryStructuredContent} content
811+
* @param {StructuredContentGenerator} structuredContentGenerator
812+
* @returns {string[]}
813+
*/
814+
_extractGlossaryData(content, structuredContentGenerator) {
815+
/** @type {import('structured-content.js').Content[]} */
816+
const glossaryContentQueue = this._extractGlossaryStructuredContentRecursive([content.content]);
817+
806818
/** @type {string[]} */
807819
const rawGlossaryContent = [];
808820
while (glossaryContentQueue.length > 0) {

0 commit comments

Comments
 (0)