import DOMPurify from 'isomorphic-dompurify';
import showdown from 'showdown';
import type { character, groupChat } from './storage/database';
import { getFileSrc } from './storage/globalApi';
import { processScript } from './process/scripts';
const convertor = new showdown.Converter({
simpleLineBreaks: true,
strikethrough: true,
tables: true
})
const safeConvertor = new showdown.Converter({
simpleLineBreaks: true,
strikethrough: true,
tables: true,
backslashEscapesHTMLTags: true
})
DOMPurify.addHook("uponSanitizeElement", (node: HTMLElement, data) => {
if (data.tagName === "iframe") {
const src = node.getAttribute("src") || "";
if (!src.startsWith("https://www.youtube.com/embed/")) {
return node.parentNode.removeChild(node);
}
}
});
DOMPurify.addHook("uponSanitizeAttribute", (node, data) => {
if(data.attrName === 'style'){
data.attrValue = data.attrValue.replace(/(absolute)|(z-index)|(fixed)/g, '')
}
})
export async function ParseMarkdown(data:string, char:(character | groupChat) = null, mode:'normal'|'back' = 'normal') {
if(char && char.type !== 'group'){
if(char.customscript){
data = processScript(char, data, 'editdisplay')
}
if(char.additionalAssets){
for(const asset of char.additionalAssets){
const assetPath = await getFileSrc(asset[1])
data = data.replaceAll(`{{raw::${asset[0]}}}`, assetPath).
replaceAll(`{{img::${asset[0]}}}`,``)
.replaceAll(`{{video::${asset[0]}}}`,``)
.replaceAll(`{{audio::${asset[0]}}}`,``)
if(mode === 'back'){
data = data.replaceAll(`{{bg::${asset[0]}}}`, `