Add syntax recheck guard

This commit is contained in:
Acid Chicken (硫酸鶏) 2019-01-25 22:18:01 +09:00
parent 8c7e56207b
commit e61c84056c
No known key found for this signature in database
GPG key ID: 5388F56C75B677A1

View file

@ -377,43 +377,56 @@ export const pack = async (
} }
//#endregion //#endregion
const nyamap: { [x: string]: string } = { if ((() => { // Recheck syntax
//#region nyaize: ja-JP const match = _note.text && _note.text.match(/<\/?!?nya>/ig) || [];
'な': 'にゃ', const stack: string[] = [];
'ナ': 'ニャ', for (const tag of [...match]
'ナ': 'ニャ' .map(x => x.toLocaleLowerCase()))
//#endregion if (tag.includes('/')) {
}; if (stack.pop() !== tag.replace('/', ''))
return false;
} else
stack.push(tag);
return !stack.length;
})()) {
const nyamap: { [x: string]: string } = {
//#region nyaize: ja-JP
'な': 'にゃ',
'ナ': 'ニャ',
'ナ': 'ニャ'
//#endregion
};
//#region nyaize: ko-KR //#region nyaize: ko-KR
const diffKoKr = '냐'.charCodeAt(0) - '나'.charCodeAt(0); const diffKoKr = '냐'.charCodeAt(0) - '나'.charCodeAt(0);
for (let i = '나'.charCodeAt(0); i < '내'.charCodeAt(0); i++) for (let i = '나'.charCodeAt(0); i < '내'.charCodeAt(0); i++)
nyamap[String.fromCharCode(i)] = String.fromCharCode(i + diffKoKr); nyamap[String.fromCharCode(i)] = String.fromCharCode(i + diffKoKr);
//#endregion //#endregion
const raw: string = _note.text; const raw: string = _note.text;
const stack = [!!_note.user.isCat]; const stack = [!!_note.user.isCat];
if (raw) { if (raw) {
_note.text = ''; _note.text = '';
for (let i = 0; i < raw.length; i++) { for (let i = 0; i < raw.length; i++) {
const head = raw[i]; const head = raw[i];
if (head === '<') { if (head === '<') {
const [, tag, state] = raw.slice(i).match(/^<((\/?!?)nya>)/i) || [, , , ]; const [, tag, state] = raw.slice(i).match(/^<((\/?!?)nya>)/i) || [, , , ];
if (typeof state === 'string') { if (typeof state === 'string') {
if (state[0] === '/') if (state[0] === '/')
stack.shift(); stack.shift();
else else
stack.unshift(!state); stack.unshift(!state);
i += tag.length; i += tag.length;
continue; continue;
}
} }
}
_note.text += stack[0] && nyamap[head] || head; _note.text += stack[0] && nyamap[head] || head;
}
} }
} }