Perfect the logic of case 1.1.x

This commit is contained in:
Yu QX 2025-05-26 17:25:33 +08:00
parent 8ef7938c96
commit 4483fa95b8

View File

@ -15,8 +15,6 @@ export const specialCases = (src: string): string => {
const processedLines = lines.map((line) => { const processedLines = lines.map((line) => {
// 1. 中文 (Chinese, CN) // 1. 中文 (Chinese, CN)
if (/[\u4e00-\u9fa5]/.test(line)) { if (/[\u4e00-\u9fa5]/.test(line)) {
// Only execute if there are Chinese characters.
// 1.1. Problems caused by Chinese parentheses // 1.1. Problems caused by Chinese parentheses
/* Discription: /* Discription:
* When `*` has Chinese parentheses on the inside, markdown parser ignore bold or italic style. * When `*` has Chinese parentheses on the inside, markdown parser ignore bold or italic style.
@ -32,8 +30,6 @@ export const specialCases = (src: string): string => {
*/ */
if (line.includes('*')) { if (line.includes('*')) {
// Only execute if `*` is found in line.
// 1.1.1. Handle **bold** with Chinese parentheses // 1.1.1. Handle **bold** with Chinese parentheses
line = processCN_01(line, '**', '', ''); line = processCN_01(line, '**', '', '');
// 1.1.2. Handle *italic* with Chinese parentheses // 1.1.2. Handle *italic* with Chinese parentheses
@ -71,16 +67,16 @@ function processCN_01(
): string { ): string {
const escapedSymbol = escapeRegExp(symbol); const escapedSymbol = escapeRegExp(symbol);
const regex = new RegExp( const regex = new RegExp(
`(.*?)(?<!${escapedSymbol})(${escapedSymbol})([^${escapedSymbol}]+)(${escapedSymbol})(?!${escapedSymbol})(.*?)`, `(.?)(?<!${escapedSymbol})(${escapedSymbol})([^${escapedSymbol}]+)(${escapedSymbol})(?!${escapedSymbol})(.)`,
'g' 'g'
); );
return line.replace(regex, (match, l, left, content, right, r) => { return line.replace(regex, (match, l, left, content, right, r) => {
const result = const result =
(content.startsWith(leftSymbol) || content.endsWith(rightSymbol)) && (content.startsWith(leftSymbol) && l && l.length > 0 && isChineseChar(l[l.length - 1])) ||
(!l || (l && l.length > 0 && isChineseChar(l[l.length - 1]))) && (content.endsWith(rightSymbol) && r && r.length > 0 && isChineseChar(r[0]));
(!r || (r && r.length > 0 && isChineseChar(r[0])));
if (result) { if (result) {
return ` ${left}${content}${right} `; return `${l} ${left}${content}${right} ${r}`;
} else { } else {
return match; return match;
} }