[feat] experimental automark
This commit is contained in:
@@ -78,6 +78,11 @@
|
|||||||
<Help key="experimental"/><Help key="inlayImages"/>
|
<Help key="experimental"/><Help key="inlayImages"/>
|
||||||
</Check>
|
</Check>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="flex items-center mt-4">
|
||||||
|
<Check bind:check={$DataBase.automark} name="Experimental Native Automark">
|
||||||
|
<Help key="experimental"/>
|
||||||
|
</Check>
|
||||||
|
</div>
|
||||||
<div class="flex items-center mt-4">
|
<div class="flex items-center mt-4">
|
||||||
<Check check={$DataBase.tpo} name="2.0 Alpha Web-DevMode" onChange={() => {
|
<Check check={$DataBase.tpo} name="2.0 Alpha Web-DevMode" onChange={() => {
|
||||||
// access code is "tendo"
|
// access code is "tendo"
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ html, body{
|
|||||||
color: var(--FontColorItalicBold);
|
color: var(--FontColorItalicBold);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chattext x-em{
|
||||||
|
color: var(--FontColorItalicBold);
|
||||||
|
font-style: italic;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
::-webkit-scrollbar {
|
::-webkit-scrollbar {
|
||||||
width: 5px;
|
width: 5px;
|
||||||
height: 5px;
|
height: 5px;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { calcString } from './process/infunctions';
|
|||||||
import { findCharacterbyId } from './util';
|
import { findCharacterbyId } from './util';
|
||||||
import { getInlayImage } from './image';
|
import { getInlayImage } from './image';
|
||||||
import { cloneDeep } from 'lodash';
|
import { cloneDeep } from 'lodash';
|
||||||
|
import { autoMarkNew } from './plugins/automark';
|
||||||
|
|
||||||
const convertora = new showdown.Converter({
|
const convertora = new showdown.Converter({
|
||||||
simpleLineBreaks: true,
|
simpleLineBreaks: true,
|
||||||
@@ -134,7 +135,7 @@ export interface simpleCharacterArgument{
|
|||||||
export async function ParseMarkdown(data:string, charArg:(simpleCharacterArgument | groupChat | string) = null, mode:'normal'|'back' = 'normal', chatID=-1) {
|
export async function ParseMarkdown(data:string, charArg:(simpleCharacterArgument | groupChat | string) = null, mode:'normal'|'back' = 'normal', chatID=-1) {
|
||||||
let firstParsed = ''
|
let firstParsed = ''
|
||||||
const orgDat = data
|
const orgDat = data
|
||||||
|
const db = get(DataBase)
|
||||||
let char = (typeof(charArg) === 'string') ? (findCharacterbyId(charArg)) : (charArg)
|
let char = (typeof(charArg) === 'string') ? (findCharacterbyId(charArg)) : (charArg)
|
||||||
if(char && char.type !== 'group'){
|
if(char && char.type !== 'group'){
|
||||||
data = await parseAdditionalAssets(data, char, mode, 'pre')
|
data = await parseAdditionalAssets(data, char, mode, 'pre')
|
||||||
@@ -147,12 +148,32 @@ export async function ParseMarkdown(data:string, charArg:(simpleCharacterArgumen
|
|||||||
data = await parseAdditionalAssets(data, char, mode, 'post')
|
data = await parseAdditionalAssets(data, char, mode, 'post')
|
||||||
}
|
}
|
||||||
data = await parseInlayImages(data)
|
data = await parseInlayImages(data)
|
||||||
return decodeStyle(DOMPurify.sanitize(mconverted.parse(encodeStyle(data)), {
|
if(db.automark){
|
||||||
ADD_TAGS: ["iframe", "style", "risu-style"],
|
// data = autoMarkNew(DOMPurify.sanitize(data, {
|
||||||
|
// ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
|
||||||
|
// ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
|
||||||
|
// FORBID_ATTR: ["href"]
|
||||||
|
// }))
|
||||||
|
// return data
|
||||||
|
// data = autoMarkNew(data)
|
||||||
|
// data = encodeStyle(data)
|
||||||
|
// data = mconverted.parse(data)
|
||||||
|
return (DOMPurify.sanitize(autoMarkNew(data), {
|
||||||
|
ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
|
||||||
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
|
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
|
||||||
FORBID_ATTR: ["href"]
|
FORBID_ATTR: ["href"]
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
data = encodeStyle(data)
|
||||||
|
data = mconverted.parse(data)
|
||||||
|
return decodeStyle(DOMPurify.sanitize(data, {
|
||||||
|
ADD_TAGS: ["iframe", "style", "risu-style", "x-em"],
|
||||||
|
ADD_ATTR: ["allow", "allowfullscreen", "frameborder", "scrolling", "risu-btn"],
|
||||||
|
FORBID_ATTR: ["href"]
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function parseMarkdownSafe(data:string) {
|
export function parseMarkdownSafe(data:string) {
|
||||||
return DOMPurify.sanitize(safeConvertor.makeHtml(data), {
|
return DOMPurify.sanitize(safeConvertor.makeHtml(data), {
|
||||||
|
|||||||
@@ -27,3 +27,133 @@ export function autoMarkPlugin(data:string){
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function autoMarkNew(dat:string){
|
||||||
|
const excludeTexts = ["#","1.","2.","3.","4.","5.","6.","7.","8.","9.","0."]
|
||||||
|
const mark = (data:string) => {
|
||||||
|
for(const text of excludeTexts){
|
||||||
|
if(data.startsWith(text)){
|
||||||
|
return document.createTextNode(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = 0
|
||||||
|
let stacks:Node[] = [document.createElement('p')]
|
||||||
|
let stackText = [""]
|
||||||
|
let stackType:number[] = [0]
|
||||||
|
function isAlpha(str:string) {
|
||||||
|
//check if string is alphabet, including extended latin by charcode. string.length === 1
|
||||||
|
const code = str.charCodeAt(0)
|
||||||
|
return (code > 64 && code < 91) || (code > 96 && code < 123) || (code > 127 && code < 256)
|
||||||
|
}
|
||||||
|
let stackIndex = 0
|
||||||
|
while(index < data.length){
|
||||||
|
switch(data[index]){
|
||||||
|
case '"':
|
||||||
|
case '“':
|
||||||
|
case '”':{
|
||||||
|
if(stackType[stackIndex] === 1){
|
||||||
|
const stack = stacks.pop()
|
||||||
|
stackText[stackIndex] += data[index]
|
||||||
|
stack.appendChild(document.createTextNode(stackText.pop()))
|
||||||
|
stackType.pop()
|
||||||
|
stackIndex--
|
||||||
|
stacks[stackIndex].appendChild(stack)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
stacks[stackIndex].appendChild(document.createTextNode(stackText[stackIndex]))
|
||||||
|
stackText[stackIndex] = ""
|
||||||
|
stacks.push(document.createElement('x-placeholder'))
|
||||||
|
stackText.push(data[index])
|
||||||
|
stackType.push(1)
|
||||||
|
stackIndex++
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case "'":
|
||||||
|
case "‘":
|
||||||
|
case "’":{
|
||||||
|
if(stackType[stackIndex] === 2){
|
||||||
|
if(data[index+1] === undefined || !isAlpha(data[index+1])){
|
||||||
|
const stack = stacks.pop()
|
||||||
|
stackText[stackIndex] += data[index]
|
||||||
|
stack.appendChild(document.createTextNode(stackText.pop()))
|
||||||
|
stackType.pop()
|
||||||
|
stackIndex--
|
||||||
|
stacks[stackIndex].appendChild(stack)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
stackText[stackIndex] += data[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(data[index-1] === ' ' || data[index-1] === '\n' || data[index-1] === undefined){
|
||||||
|
stacks[stackIndex].appendChild(document.createTextNode(stackText[stackIndex]))
|
||||||
|
stackText[stackIndex] = ""
|
||||||
|
stacks.push(document.createElement('x-em'))
|
||||||
|
stackText.push(data[index])
|
||||||
|
stackType.push(2)
|
||||||
|
stackIndex++
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
stackText[stackIndex] += data[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case '\n':{
|
||||||
|
stacks[stackIndex].appendChild(document.createTextNode(stackText[stackIndex]))
|
||||||
|
stackText[stackIndex] = ""
|
||||||
|
stacks[stackIndex].appendChild(document.createElement('br'))
|
||||||
|
}
|
||||||
|
default:{
|
||||||
|
stackText[stackIndex] += data[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
for(let i=stackIndex;i>0;i--){
|
||||||
|
stacks[i-1].appendChild(document.createTextNode(stackText[i]))
|
||||||
|
stacks[i-1].appendChild(stacks[i])
|
||||||
|
}
|
||||||
|
stacks[0].appendChild(document.createTextNode(stackText[0]))
|
||||||
|
const childs = stacks[0].childNodes
|
||||||
|
for(let i=0;i<childs.length;i++){
|
||||||
|
if(childs[i].nodeType === 3){
|
||||||
|
const marked = document.createElement('em')
|
||||||
|
marked.appendChild(document.createTextNode(childs[i].textContent))
|
||||||
|
stacks[0].replaceChild(marked, childs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stacks[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const domparser = new DOMParser()
|
||||||
|
const doc = domparser.parseFromString(`<body>${dat}</body>`, 'text/html')
|
||||||
|
const body = doc.body
|
||||||
|
console.log(body.innerHTML)
|
||||||
|
let newChilds:Node[] = []
|
||||||
|
for(let i=0;i<body.childNodes.length;i++){
|
||||||
|
if(body.childNodes[i].nodeType === 3){
|
||||||
|
const lines = body.childNodes[i].textContent.split('\n')
|
||||||
|
for(let j=0;j<lines.length;j++){
|
||||||
|
newChilds.push(mark(lines[j]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
newChilds.push(body.childNodes[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const newBody = document.createElement('body')
|
||||||
|
for(let i=0;i<newChilds.length;i++){
|
||||||
|
if(newChilds[i] === null){
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newBody.appendChild(newChilds[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return newBody.innerHTML
|
||||||
|
}
|
||||||
@@ -506,6 +506,7 @@ export interface Database{
|
|||||||
reverseProxyOobaMode:boolean
|
reverseProxyOobaMode:boolean
|
||||||
reverseProxyOobaArgs: OobaChatCompletionRequestParams
|
reverseProxyOobaArgs: OobaChatCompletionRequestParams
|
||||||
tpo?:boolean
|
tpo?:boolean
|
||||||
|
automark?:boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface customscript{
|
export interface customscript{
|
||||||
|
|||||||
Reference in New Issue
Block a user