Support multiple named assets

This commit is contained in:
kwaroran
2025-04-14 17:14:33 +09:00
parent cf039b86af
commit 21561fe5ff

View File

@@ -311,14 +311,18 @@ async function replaceAsync(string, regexp, replacerFunction) {
return string.replace(regexp, () => replacements[i++])
}
async function getAssetSrc(assetArr: string[][], name: string, assetPaths: {[key: string]:{path: string, ext?: string}}) {
async function getAssetSrc(assetArr: string[][], name: string, assetPaths: {[key: string]:{path: string[], ext?: string}}) {
for (const asset of assetArr) {
if (trimmer(asset[0].toLocaleLowerCase()) !== trimmer(name)) continue
const assetPath = await getFileSrc(asset[1])
assetPaths[asset[0].toLocaleLowerCase()] = {
path: assetPath,
const key = asset[0].toLocaleLowerCase()
assetPaths[key] = {
path: [],
ext: asset[2]
}
if(assetPaths[key].ext === asset[2]){
assetPaths[key].path.push(assetPath)
}
return
}
}
@@ -332,11 +336,11 @@ async function getEmoSrc(emoArr: string[][], emoPaths: {[key: string]:{path: str
}
}
async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back', mode2:'unset'|'pre'|'post' = 'unset'){
async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back', arg:{ch:number}){
const assetWidthString = (DBState.db.assetWidth && DBState.db.assetWidth !== -1 || DBState.db.assetWidth === 0) ? `max-width:${DBState.db.assetWidth}rem;` : ''
let assetPaths:{[key:string]:{
path:string
path:string[]
ext?:string
}} = {}
let emoPaths:{[key:string]:{
@@ -391,33 +395,39 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c
return ''
}
}
let p = path.path[0]
if(path.path.length > 1){
p = path.path[Math.floor(arg.ch % p.length)]
}
switch(type){
case 'raw':
case 'path':
return path.path
return p
case 'img':
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>`
return `<img src="${p}" alt="${p}" style="${assetWidthString} "/>`
case 'image':
return `<div class="risu-inlay-image"><img src="${path.path}" alt="${path.path}" style="${assetWidthString}"/></div>\n`
return `<div class="risu-inlay-image"><img src="${p}" alt="${p}" style="${assetWidthString}"/></div>\n`
case 'video':
return `<video controls autoplay loop><source src="${path.path}" type="video/mp4"></video>\n`
return `<video controls autoplay loop><source src="${p}" type="video/mp4"></video>\n`
case 'video-img':
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>\n`
return `<video autoplay muted loop><source src="${p}" type="video/mp4"></video>\n`
case 'audio':
return `<audio controls autoplay loop><source src="${path.path}" type="audio/mpeg"></audio>\n`
return `<audio controls autoplay loop><source src="${p}" type="audio/mpeg"></audio>\n`
case 'bg':
if(mode === 'back'){
return `<div style="width:100%;height:100%;background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)),url(${path.path}); background-size: cover;"></div>`
return `<div style="width:100%;height:100%;background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)),url(${p}); background-size: cover;"></div>`
}
break
case 'asset':{
if(path.ext && videoExtention.includes(path.ext)){
return `<video autoplay muted loop><source src="${path.path}" type="video/mp4"></video>\n`
return `<video autoplay muted loop><source src="${p}" type="video/mp4"></video>\n`
}
return `<img src="${path.path}" alt="${path.path}" style="${assetWidthString} "/>\n`
return `<img src="${p}" alt="${p}" style="${assetWidthString} "/>\n`
}
case 'bgm':
return `<div risu-ctrl="bgm___auto___${path.path}" style="display:none;"></div>\n`
return `<div risu-ctrl="bgm___auto___${p}" style="display:none;"></div>\n`
}
return ''
})
@@ -437,7 +447,7 @@ async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|c
return data
}
async function getClosestMatch(char: simpleCharacterArgument|character, name:string, assetPaths:{[key:string]:{path:string, ext?:string}}){
async function getClosestMatch(char: simpleCharacterArgument|character, name:string, assetPaths:{[key:string]:{path:string[], ext?:string}}){
if(!char.additionalAssets) return null
let closest = ''
@@ -463,7 +473,7 @@ async function getClosestMatch(char: simpleCharacterArgument|character, name:str
const assetPath = await getFileSrc(targetPath)
assetPaths[closest] = {
path: assetPath,
path: [assetPath],
ext: targetExt
}
@@ -557,7 +567,9 @@ export async function ParseMarkdown(
let char = (typeof(charArg) === 'string') ? (findCharacterbyId(charArg)) : (charArg)
if(char && char.type !== 'group'){
data = await parseAdditionalAssets(data, char, additionalAssetMode, 'pre')
data = await parseAdditionalAssets(data, char, additionalAssetMode, {
ch: chatID
})
firstParsed = data
}
@@ -566,7 +578,9 @@ export async function ParseMarkdown(
}
if(firstParsed !== data && char && char.type !== 'group'){
data = await parseAdditionalAssets(data, char, additionalAssetMode, 'post')
data = await parseAdditionalAssets(data, char, additionalAssetMode, {
ch: chatID
})
}
data = await parseInlayAssets(data ?? '')