Skip to content

Commit

Permalink
Fix finding media to inject in anki note generator (#1848)
Browse files Browse the repository at this point in the history
* Fix _findAllPaths function in anki-deck-generator

* Add descriptive comment to _findAllPaths
  • Loading branch information
Kuuuube authored Mar 9, 2025
1 parent 078dec4 commit a2658dd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ext/js/pages/settings/anki-deck-generator-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,16 @@ export class AnkiDeckGeneratorController {
}

/**
* Extracts all values of json keys named `path` which contain a string value.
* Example json snippet containing a path:
* ...","path":"example-dictionary/svg/example-media.svg","...
* The path can be found in many different positions in the structure of the definition json.
* It is most reliable to flatten it to a string and use regex.
* @param {object} obj
* @returns {Array<string>}
*/
_findAllPaths(obj) {
// @ts-expect-error - Recursive function to find object keys deeply nested in objects and arrays. Essentially impossible to type correctly.
// eslint-disable-next-line unicorn/no-array-reduce, @typescript-eslint/no-unsafe-argument
return Object.entries(obj).reduce((acc, [key, value]) => (key === 'path' ? [...acc, value] : (typeof value === 'object' ? [...acc, ...this._findAllPaths(value)] : acc)), []);
return JSON.stringify(obj).match(/(?<="path":").*?(?=")/g) ?? [];
}

/**
Expand Down

0 comments on commit a2658dd

Please sign in to comment.