diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2026-03-31 09:00:55 -0300 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2026-03-31 09:00:55 -0300 |
| commit | ef6899009050d9036651162c17a8e7572552c558 (patch) | |
| tree | 030842051ccb7fd0209d0157929bc8bafecd392b /src/common-util | |
| parent | a0924d447a53511c96945873a3ec996b5800d9a9 (diff) | |
sugar, wiki-data: simplify re tool bigtime
Diffstat (limited to 'src/common-util')
| -rw-r--r-- | src/common-util/sugar.js | 47 | ||||
| -rw-r--r-- | src/common-util/wiki-data.js | 106 |
2 files changed, 68 insertions, 85 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index 4dd34785..c988156c 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -418,39 +418,28 @@ export function escapeRegex(string) { } // Adapted from here: https://emnudge.dev/notes/multiline-regex/ +// ...with a lot of changes export function re(...args) { - let flags = ''; - - const fn = (strings, ...substitutions) => { - strings = strings - .map(str => str.replace(/(?:\/\/.+)/gm, '')) - .map(str => str.replace(/\s+/g, '')); - - substitutions = substitutions - .map(sub => [sub].flat(Infinity)) - .map(sub => sub - .map(item => - (item instanceof RegExp - ? item.source - : item.toString()))) - .map(sub => sub.join('')); - - const source = - String.raw({raw: strings}, ...substitutions); - - return new RegExp(source, flags); - }; - - if ( - args.length === 1 && - typeof args[0] === 'string' && - args[0].match(/^[a-z]+$/) - ) { + let flags, parts; + if (args.length === 2) { flags = args[0]; - return fn; + parts = args[1]; + } else if (args.length === 1) { + flags = ''; + parts = args[0]; } else { - return fn(...args); + throw new Error(`Expected 1 or 2 arguments`); } + + const source = parts + .flat(Infinity) + .map(item => + (item instanceof RegExp + ? item.source + : item.toString())) + .join(''); + + return new RegExp(source, flags); } export function splitKeys(key) { diff --git a/src/common-util/wiki-data.js b/src/common-util/wiki-data.js index 0e447030..ff325b7a 100644 --- a/src/common-util/wiki-data.js +++ b/src/common-util/wiki-data.js @@ -76,55 +76,48 @@ export function compareKebabCase(name1, name2) { // by slashes or dashes (only valid orders are MM/DD/YYYY and YYYY/MM/DD) // const dateRegex = groupName => - re` - (?<${groupName}> - ${[ - /[a-zA-Z]+ [0-9]{1,2}, [0-9]{4,4}/, '|', - /[0-9]{1,2} [^,]*[0-9]{4,4}/, '|', - /[0-9]{1,4}[-/][0-9]{1,4}[-/][0-9]{1,4}/, - ]} - ) - `; + re([ + `(?<${groupName}>`, + /[a-zA-Z]+ [0-9]{1,2}, [0-9]{4,4}/, '|', + /[0-9]{1,2} [^,]*[0-9]{4,4}/, '|', + /[0-9]{1,4}[-/][0-9]{1,4}[-/][0-9]{1,4}/, + `)`, + ]); const contentEntryHeadingRegex = - re('gm')` - ^(?: - (?:${[ + re('gm', [ + '^(?:', + '(?:', /<i>(?<artists>.+?):<\/i>/, /(?: \((?<annotation1>.*)\))?/, - ]}) - | - (?:${[ + ')', + '|', + '(?:', /@@ (?<annotation2>.*)/, - ]}) - )$ - `; + ')', + ')$', + ]); const contentEntryAnnotationTailRegex = - re` - ${/(?:, |^)/} - - ${/(?:(?<dateKind>sometime|throughout|around) )?/} - - ${dateRegex('date')} - - ${[ - '(?:', - ' ?', - '-', - ' ?', - dateRegex('secondDate'), - ')?', - ]} - - ${[ - '(?: ?(?<= )', - /(?<accessKind>captured|accessed)/, - ' ', - dateRegex('accessDate'), - ')?', - ]} - `; + re([ + /(?:, |^)/, + + /(?:(?<dateKind>sometime|throughout|around) )?/, + dateRegex('date'), + + '(?:', + ' ?', + '-', + ' ?', + dateRegex('secondDate'), + ')?', + + '(?: ?(?<= )', + /(?<accessKind>captured|accessed)/, + ' ', + dateRegex('accessDate'), + ')?', + ]); export function* matchContentEntries(sourceText) { let workingEntry = null; @@ -606,20 +599,21 @@ export function* matchMarkdownLinks(markdownSource, {marked}) { } export function* matchInlineLinks(source) { - const plausibleLinkRegexp = re('gmi')` - ${/\b[a-z]*:\/\//} - ${/.*?/} - - (?=${[ - // Ordinary in-sentence punctuation doesn't terminate the - // un-greedy URL match above, but it shouldn't be counted - // as part of the link either, if it's at the end. - /(?:[,.!?]*)/, - - // Actual terminators. - /(?:\s|$|<br>)/, - ]}) - `; + const plausibleLinkRegexp = + re('gmi', [ + /\b[a-z]*:\/\//, + /.*?/, + + '(?=', + // Ordinary in-sentence punctuation doesn't terminate the + // un-greedy URL match above, but it shouldn't be counted + // as part of the link either, if it's at the end. + /(?:[,.!?]*)/, + + // Actual terminators. + /(?:\s|$|<br>)/, + ')', + ]); let plausibleMatch = null; while (plausibleMatch = plausibleLinkRegexp.exec(source)) { |