From 25b507a01a36f779d4f63e884382d47c5f5d29d2 Mon Sep 17 00:00:00 2001 From: Dong Hyun Kim <146043537+ThePotioner@users.noreply.github.com> Date: Thu, 6 Feb 2025 18:33:43 +0900 Subject: [PATCH] refactor(parser): import only the required asset when in a node environment - refactor parseAdditionalAssets() - Modified the getClostMatch() to work well with Object to which it is dynamically added. --- src/ts/parser.svelte.ts | 142 +++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 54 deletions(-) diff --git a/src/ts/parser.svelte.ts b/src/ts/parser.svelte.ts index c6912d5e..4a235208 100644 --- a/src/ts/parser.svelte.ts +++ b/src/ts/parser.svelte.ts @@ -295,6 +295,14 @@ async function renderHighlightableMarkdown(data:string) { export const assetRegex = /{{(raw|path|img|image|video|audio|bg|emotion|asset|video-img|source)::(.+?)}}/gms +async function replaceAsync(string, regexp, replacerFunction) { + const replacements = await Promise.all( + Array.from(string.matchAll(regexp), + match => replacerFunction(...match as any))); + let i = 0; + return string.replace(regexp, () => replacements[i++]); +} + async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back', mode2:'unset'|'pre'|'post' = 'unset'){ const assetWidthString = (DBState.db.assetWidth && DBState.db.assetWidth !== -1 || DBState.db.assetWidth === 0) ? `max-width:${DBState.db.assetWidth}rem;` : '' @@ -306,15 +314,6 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c path:string }} = {} - if(char.additionalAssets){ - for(const asset of char.additionalAssets){ - const assetPath = await getFileSrc(asset[1]) - assetPaths[asset[0].toLocaleLowerCase()] = { - path: assetPath, - ext: asset[2] - } - } - } if(char.emotionImages){ for(const emo of char.emotionImages){ const emoPath = await getFileSrc(emo[1]) @@ -335,8 +334,23 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c } const videoExtention = ['mp4', 'webm', 'avi', 'm4p', 'm4v'] let needsSourceAccess = false - data = data.replaceAll(assetRegex, (full:string, type:string, name:string) => { + data = await replaceAsync(data, assetRegex, async (full:string, type:string, name:string) => { name = name.toLocaleLowerCase() + if(char.additionalAssets){ + for(const asset of char.additionalAssets){ + if (trimmer(asset[0].toLocaleLowerCase()) === trimmer(name)){ + console.log("In parseAdditionalAssets() > Catch Asset : " + asset[0] + "\nIn parseAdditionalAssets() > Asset Path : " + asset[1]) + const assetPath = await getFileSrc(asset[1]) + assetPaths[name] = { + path: assetPath, + ext: asset[2] + } + } + } + } + console.log("In parseAdditionalAssets() > Name: " + name) + console.log("In parseAdditionalAssets() > Char Obj: ", char) + console.log("In parseAdditionalAssets() > Char Type: " + char.type) if(type === 'emotion'){ const path = emoPaths[name]?.path if(!path){ @@ -361,7 +375,7 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c return '' } - path = getClosestMatch(name, assetPaths) + path = await getClosestMatch(char, name, assetPaths) if(!path){ return '' @@ -411,63 +425,84 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c return data } -function getClosestMatch(name:string, assetPaths:{[key:string]:{path:string, ext?:string}}){ - - if(Object.keys(assetPaths).length === 0){ - return null - } - - //Levenshtein distance, new with 1d array - const dest = (a:string, b:string) => { - const h = a.length + 1 - const w = b.length + 1 - let d = new Int16Array(h * w) - for(let i=0;i DBState.db.assetMaxDifference){ return null } + + const assetPath = await getFileSrc(targetPath) + assetPaths[closest] = { + path: assetPath, + ext: targetExt + } + + console.log("In getClosestMatch() > assetPaths[closest]: " + assetPaths[closest]) + console.log("In getClosestMatch() > closet, closetDist: " + closest, closestDist) + return assetPaths[closest] } +//Levenshtein distance, new with 1d array +function getDistance(a:string, b:string) { + const h = a.length + 1 + const w = b.length + 1 + let d = new Int16Array(h * w) + for(let i=0;i { try { let text = Buffer.from(txt, 'hex').toString('utf-8')