change markdown engine to markdown-lt

This commit is contained in:
kwaroran
2024-07-02 05:04:37 +09:00
parent 45b16da8a5
commit f38b88e208
5 changed files with 105 additions and 60 deletions

View File

@@ -1,6 +1,5 @@
import DOMPurify from 'isomorphic-dompurify';
import { Marked } from 'marked';
import markdownit from 'markdown-it'
import { DataBase, setDatabase, type Database, type Message, type character, type customscript, type groupChat, type triggerscript } from './storage/database';
import { getFileSrc } from './storage/globalApi';
import { processScriptFull } from './process/scripts';
@@ -18,24 +17,12 @@ import { requestChatData } from './process/request';
import type { OpenAIChat } from './process';
import { alertInput, alertNormal } from './alert';
const mconverted = new Marked({
gfm: true,
const mconverted = markdownit({
html: true,
breaks: true,
silent: true,
tokenizer: {
del(src) {
const cap = /^~~~(?=\S)([\s\S]*?\S)~~~/.exec(src);
if (cap) {
return {
type: 'del',
raw: cap[0],
text: cap[1],
tokens: this.lexer.inlineTokens(cap[1])
};
}
}
}
linkify: false
})
mconverted.disable(['code'])
@@ -194,7 +181,7 @@ export async function ParseMarkdown(data:string, charArg:(character|simpleCharac
data = encodeStyle(data)
if(mode === 'normal'){
data = risuFormater(data)
data = mconverted.parse(data)
data = mconverted.render(data)
}
return decodeStyle(DOMPurify.sanitize(data, {
ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
@@ -212,12 +199,12 @@ export function postTranslationParse(data:string){
}
}
data = mconverted.parse(lines.join('\n'))
data = mconverted.render(lines.join('\n'))
return data
}
export function parseMarkdownSafe(data:string) {
return DOMPurify.sanitize(mconverted.parse(data), {
return DOMPurify.sanitize(mconverted.render(data), {
FORBID_TAGS: ["a", "style"],
FORBID_ATTR: ["style", "href", "class"]
})
@@ -2291,4 +2278,32 @@ export async function promptTypeParser(prompt:string):Promise<string | PromptPar
}
return prompt
}
export function applyMarkdownToNode(node: Node) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent;
if (text) {
let markdown = mconverted.render(text);
if (markdown !== text) {
const span = document.createElement('span');
span.innerHTML = markdown;
// inherit inline style from the parent node
const parentStyle = (node.parentNode as HTMLElement)?.style;
if(parentStyle){
for(let i=0;i<parentStyle.length;i++){
span.style.setProperty(parentStyle[i], parentStyle.getPropertyValue(parentStyle[i]))
}
}
(node as Element)?.replaceWith(span);
return
}
}
} else {
for (const child of node.childNodes) {
applyMarkdownToNode(child);
}
}
}

View File

@@ -5,10 +5,10 @@ import { globalFetch, isTauri } from "../storage/globalApi"
import { alertError } from "../alert"
import { requestChatData } from "../process/request"
import { doingChat } from "../process"
import type { simpleCharacterArgument } from "../parser"
import { applyMarkdownToNode, type simpleCharacterArgument } from "../parser"
import { selectedCharID } from "../stores"
import { getModuleRegexScripts } from "../process/modules"
import { getNodetextToSentence, sleep, applyMarkdownToNode } from "../util"
import { getNodetextToSentence, sleep } from "../util"
import { processScriptFull } from "../process/scripts"
import { Capacitor } from "@capacitor/core"

View File

@@ -8,19 +8,9 @@ import { basename } from "@tauri-apps/api/path"
import { createBlankChar, getCharImage } from "./characters"
import { appWindow } from '@tauri-apps/api/window';
import { isTauri } from "./storage/globalApi"
import { Marked } from "marked"
export const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
const mconverted = new Marked({
gfm: true,
breaks: true,
silent: true,
tokenizer: {
}
})
export interface Messagec extends Message{
index: number
}
@@ -585,33 +575,6 @@ export function getNodetextToSentence(node: Node): string {
return result;
}
export function applyMarkdownToNode(node: Node) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent;
if (text) {
let markdown = mconverted.parseInline(text);
if (markdown !== text) {
const span = document.createElement('span');
span.innerHTML = markdown;
// inherit inline style from the parent node
const parentStyle = (node.parentNode as HTMLElement)?.style;
if(parentStyle){
for(let i=0;i<parentStyle.length;i++){
span.style.setProperty(parentStyle[i], parentStyle.getPropertyValue(parentStyle[i]))
}
}
(node as Element)?.replaceWith(span);
return
}
}
} else {
for (const child of node.childNodes) {
applyMarkdownToNode(child);
}
}
}
export const TagList = [
{