From c8dda536ca8251cab7281de7ddcebab4c2fa63ab Mon Sep 17 00:00:00 2001 From: seaHi Date: Wed, 23 Sep 2026 10:17:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=B9=E8=BF=9B=20Markdown=20?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E6=A0=BC=E5=BC=8F=E5=8C=96=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=AE=BD=E5=AD=97=E7=AC=A6=E8=AE=A1=E7=AE=97=E5=B9=B6?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=B1=85=E4=B8=AD=E5=AF=B9=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/editor/tableFormatterPlugin.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/editor/tableFormatterPlugin.ts b/src/lib/editor/tableFormatterPlugin.ts index eeda099..190ec95 100644 --- a/src/lib/editor/tableFormatterPlugin.ts +++ b/src/lib/editor/tableFormatterPlugin.ts @@ -5,15 +5,33 @@ import { syntaxTree } from '@codemirror/language'; function getDisplayLength(str: string): number { let len = 0; for (let i = 0; i < str.length; i++) { - len += str.charCodeAt(i) > 255 ? 2 : 1; + const c = str.charCodeAt(i); + // Check for CJK and Fullwidth characters + if ( + (c >= 0x1100 && c <= 0x115f) || // Hangul Jamo + (c >= 0x2e80 && c <= 0xa4cf) || // CJK Radicals, Kangxi, Ideographs, etc. + (c >= 0xac00 && c <= 0xd7a3) || // Hangul Syllables + (c >= 0xf900 && c <= 0xfaff) || // CJK Compatibility Ideographs + (c >= 0xfe10 && c <= 0xfe19) || // Vertical forms + (c >= 0xfe30 && c <= 0xfe6f) || // CJK Compatibility Forms + (c >= 0xff00 && c <= 0xff60) || // Fullwidth Forms + (c >= 0xffe0 && c <= 0xffe6) + ) { + len += 2; + } else { + len += 1; + } } return len; } -function padEnd(str: string, targetLength: number): string { +function padCenter(str: string, targetLength: number): string { const curLen = getDisplayLength(str); if (curLen >= targetLength) return str; - return str + ' '.repeat(targetLength - curLen); + const padTotal = targetLength - curLen; + const padLeft = Math.floor(padTotal / 2); + const padRight = padTotal - padLeft; + return ' '.repeat(padLeft) + str + ' '.repeat(padRight); } export function formatMarkdownTable(tableStr: string): string { @@ -65,7 +83,7 @@ export function formatMarkdownTable(tableStr: string): string { } else { const parts = colWidths.map((w, i) => { const col = row[i] || ''; - return padEnd(col, w); + return padCenter(col, w); }); return '| ' + parts.join(' | ') + ' |'; }