feat(doctor,lawyer): 채팅창 마크다운 표 렌더링 — 탐정앱과 동일한 mdTables 적용
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
931c3e4647
commit
c08af925ec
2 files changed
+44
-2
No files matched your search
@@ -1389,6 +1389,7 @@ function renderMd(text){
|
||||
return text
|
||||
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
|
||||
.replace(/```[\w]*\n?([\s\S]*?)```/g,'<pre><code>$1</code></pre>')
|
||||
.split(/(<pre>[\s\S]*?<\/pre>)/).map((seg,i)=>i%2?seg:mdTables(seg)).join('')
|
||||
.replace(/`([^`]+)`/g,'<code>$1</code>')
|
||||
.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>')
|
||||
.replace(/\*(.+?)\*/g,'<em>$1</em>')
|
||||
@@ -1397,7 +1398,27 @@ function renderMd(text){
|
||||
.replace(/^[-*]\s+(.+)$/gm,'<li>$1</li>')
|
||||
.replace(/(<li>[\s\S]+?<\/li>)/g,m=>`<ul style="padding-left:16px;margin:3px 0">${m}</ul>`)
|
||||
.replace(/\n\n/g,'</p><p>').replace(/^/,'<p>').replace(/$/,'</p>')
|
||||
.replace(/<p><\/p>/g,'').replace(/<p>(<[hup])/g,'$1').replace(/(<\/[hup][^>]*>)<\/p>/g,'$1');
|
||||
.replace(/<p><\/p>/g,'').replace(/<p>(<[hup]|<div style="overflow-x:auto">)/g,'$1').replace(/(<\/[hup][^>]*>|<\/table><\/div>)<\/p>/g,'$1');
|
||||
}
|
||||
|
||||
// 마크다운 표: '|'로 시작하는 연속 줄 + 두 번째 줄이 구분선(|---|---|)인 블록만 <table>로 바꾼다.
|
||||
// 입력은 이미 HTML 이스케이프된 상태. 셀 안의 `코드`·**굵게**는 이후 인라인 변환이 처리한다.
|
||||
function mdTables(text){
|
||||
const cells=l=>l.trim().replace(/^\|/,'').replace(/\|$/,'').split('|').map(c=>c.trim());
|
||||
const isSep=l=>/^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/.test(l)&&l.includes('-')&&/\|/.test(l);
|
||||
const lines=text.split('\n'),out=[];
|
||||
for(let i=0;i<lines.length;){
|
||||
if(/^\s*\|/.test(lines[i])&&i+1<lines.length&&isSep(lines[i+1])){
|
||||
const head=cells(lines[i]),al=cells(lines[i+1]).map(c=>/^:-+:$/.test(c)?'center':/-:$/.test(c)?'right':'');
|
||||
let j=i+2;const rows=[];
|
||||
while(j<lines.length&&/^\s*\|/.test(lines[j])){rows.push(cells(lines[j]));j++;}
|
||||
const td=(t,c,k)=>`<${t}${al[k]?` style="text-align:${al[k]}"`:''}>${c}</${t}>`;
|
||||
out.push('<div style="overflow-x:auto"><table><thead><tr>'+head.map((c,k)=>td('th',c,k)).join('')+'</tr></thead><tbody>'+
|
||||
rows.map(r=>'<tr>'+head.map((_,k)=>td('td',r[k]??'',k)).join('')+'</tr>').join('')+'</tbody></table></div>');
|
||||
i=j;
|
||||
}else out.push(lines[i++]);
|
||||
}
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
// 사용자 메시지 HTML — 카메라 프레임 마크다운()은 썸네일로, 나머지는 이스케이프 텍스트로.
|
||||
|
||||
@@ -1732,6 +1732,7 @@ function renderMd(text){
|
||||
return text
|
||||
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
|
||||
.replace(/```[\w]*\n?([\s\S]*?)```/g,'<pre><code>$1</code></pre>')
|
||||
.split(/(<pre>[\s\S]*?<\/pre>)/).map((seg,i)=>i%2?seg:mdTables(seg)).join('')
|
||||
.replace(/`([^`]+)`/g,'<code>$1</code>')
|
||||
.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>')
|
||||
.replace(/\*(.+?)\*/g,'<em>$1</em>')
|
||||
@@ -1740,7 +1741,27 @@ function renderMd(text){
|
||||
.replace(/^[-*]\s+(.+)$/gm,'<li>$1</li>')
|
||||
.replace(/(<li>[\s\S]+?<\/li>)/g,m=>`<ul style="padding-left:16px;margin:3px 0">${m}</ul>`)
|
||||
.replace(/\n\n/g,'</p><p>').replace(/^/,'<p>').replace(/$/,'</p>')
|
||||
.replace(/<p><\/p>/g,'').replace(/<p>(<[hup])/g,'$1').replace(/(<\/[hup][^>]*>)<\/p>/g,'$1');
|
||||
.replace(/<p><\/p>/g,'').replace(/<p>(<[hup]|<div style="overflow-x:auto">)/g,'$1').replace(/(<\/[hup][^>]*>|<\/table><\/div>)<\/p>/g,'$1');
|
||||
}
|
||||
|
||||
// 마크다운 표: '|'로 시작하는 연속 줄 + 두 번째 줄이 구분선(|---|---|)인 블록만 <table>로 바꾼다.
|
||||
// 입력은 이미 HTML 이스케이프된 상태. 셀 안의 `코드`·**굵게**는 이후 인라인 변환이 처리한다.
|
||||
function mdTables(text){
|
||||
const cells=l=>l.trim().replace(/^\|/,'').replace(/\|$/,'').split('|').map(c=>c.trim());
|
||||
const isSep=l=>/^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/.test(l)&&l.includes('-')&&/\|/.test(l);
|
||||
const lines=text.split('\n'),out=[];
|
||||
for(let i=0;i<lines.length;){
|
||||
if(/^\s*\|/.test(lines[i])&&i+1<lines.length&&isSep(lines[i+1])){
|
||||
const head=cells(lines[i]),al=cells(lines[i+1]).map(c=>/^:-+:$/.test(c)?'center':/-:$/.test(c)?'right':'');
|
||||
let j=i+2;const rows=[];
|
||||
while(j<lines.length&&/^\s*\|/.test(lines[j])){rows.push(cells(lines[j]));j++;}
|
||||
const td=(t,c,k)=>`<${t}${al[k]?` style="text-align:${al[k]}"`:''}>${c}</${t}>`;
|
||||
out.push('<div style="overflow-x:auto"><table><thead><tr>'+head.map((c,k)=>td('th',c,k)).join('')+'</tr></thead><tbody>'+
|
||||
rows.map(r=>'<tr>'+head.map((_,k)=>td('td',r[k]??'',k)).join('')+'</tr>').join('')+'</tbody></table></div>');
|
||||
i=j;
|
||||
}else out.push(lines[i++]);
|
||||
}
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
async function sendMessage(){
|
||||
|
||||
Reference in new issue
Block a user