Add regex risu flags

This commit is contained in:
kwaroran
2024-09-05 21:45:51 +09:00
parent 7d14cf614c
commit d511089090
8 changed files with 120 additions and 37 deletions

View File

@@ -4,7 +4,7 @@ import { DataBase, setDatabase, type Database, type Message, type character, typ
import { getFileSrc } from './storage/globalApi';
import { processScriptFull } from './process/scripts';
import { get } from 'svelte/store';
import css from '@adobe/css-tools'
import css, { type CssAtRuleAST } from '@adobe/css-tools'
import { CurrentCharacter, CurrentChat, SizeStore, selectedCharID } from './stores';
import { calcString } from './process/infunctions';
import { findCharacterbyId, getPersonaPrompt, getUserIcon, getUserName, parseKeyValue, sfc32, sleep, uuidtoNumber } from './util';
@@ -439,6 +439,32 @@ function encodeStyle(txt:string){
}
const styleDecodeRegex = /\<risu-style\>(.+?)\<\/risu-style\>/gms
function decodeStyleRule(rule:CssAtRuleAST){
if(rule.type === 'rule'){
if(rule.selectors){
for(let i=0;i<rule.selectors.length;i++){
let slt:string = rule.selectors[i]
if(slt){
let selectors = (slt.split(' ') ?? []).map((v) => {
if(v.startsWith('.')){
return ".x-risu-" + v.substring(1)
}
return v
}).join(' ')
rule.selectors[i] = ".chattext " + selectors
}
}
}
}
if(rule.type === 'media' || rule.type === 'supports' || rule.type === 'document' || rule.type === 'host' || rule.type === 'container' ){
for(let i=0;i<rule.rules.length;i++){
rule.rules[i] = decodeStyleRule(rule.rules[i])
}
}
return rule
}
function decodeStyle(text:string){
return text.replaceAll(styleDecodeRegex, (full, txt:string) => {
@@ -446,26 +472,10 @@ function decodeStyle(text:string){
const ast = css.parse(Buffer.from(txt, 'hex').toString('utf-8'))
const rules = ast?.stylesheet?.rules
if(rules){
for(const rule of rules){
if(rule.type === 'rule'){
if(rule.selectors){
for(let i=0;i<rule.selectors.length;i++){
let slt:string = rule.selectors[i]
if(slt){
let selectors = (slt.split(' ') ?? []).map((v) => {
if(v.startsWith('.')){
return ".x-risu-" + v.substring(1)
}
return v
}).join(' ')
rule.selectors[i] = ".chattext " + selectors
}
}
}
}
for(let i=0;i<rules.length;i++){
rules[i] = decodeStyleRule(rules[i])
}
ast.stylesheet.rules = rules
}
return `<style>${css.stringify(ast)}</style>`