feat(investor,mind,accountant): 채팅창 마크다운 표 렌더링 — mdTables 적용

회계사: 행마다 표가 따로 생기던 기존 표 코드 교체. 투자자: 대화 기록 복원용
renderMd(이스케이프만 하던 미완성)를 renderMdSafe로 위임.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-07 17:32:40 +09:00
1 parent c08af925ec
commit f89e87bea4
3 files changed
+70 -39

No files matched your search

+22 -12
View File
@@ -450,26 +450,36 @@ function renderMd(text){
.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>')
.replace(/\*(.+?)\*/g,'<em>$1</em>')
.replace(/^#{1,3}\s+(.+)$/gm,'<h3>$1</h3>')
.replace(/^\|(.+)\|$/gm,row=>{
const cells=row.slice(1,-1).split('|').map(c=>`<td>${c.trim()}</td>`).join('');
return `<tr>${cells}</tr>`;
})
.replace(/(<tr>.*?<\/tr>)/gs, tbl=>{
const rows=tbl.match(/<tr>.*?<\/tr>/gs)||[];
if(!rows.length)return tbl;
const header=rows[0].replace(/<td>/g,'<th>').replace(/<\/td>/g,'</th>');
const body=rows.slice(2).join('');
return `<table><thead>${header}</thead><tbody>${body}</tbody></table>`;
})
.split(/(<pre>[\s\S]*?<\/pre>)/).map((seg,i)=>i%2?seg:mdTables(seg)).join('')
.replace(/^-{3,}$/gm,'<hr>')
.replace(/\n\n/g,'</p><p>').replace(/^/,'<p>').replace(/$/,'</p>')
.replace(/<p><\/p>/g,'')
.replace(/<p>(<h3>)/g,'$1').replace(/(<\/h3>)<\/p>/g,'$1')
.replace(/<p>(<pre>)/g,'$1').replace(/(<\/pre>)<\/p>/g,'$1')
.replace(/<p>(<table>)/g,'$1').replace(/(<\/table>)<\/p>/g,'$1')
.replace(/<p>(<div style="overflow-x:auto">)/g,'$1').replace(/(<\/table><\/div>)<\/p>/g,'$1')
.replace(/<p>(<hr>)/g,'$1').replace(/(<hr>)<\/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(){
const input=document.getElementById('chat-input');
const msg=input.value.trim();
+25 -26
View File
@@ -705,31 +705,8 @@ function addMsg(role,html){
return b;
}
function renderMd(text){
// First extract chart blocks to replace with links
let out=text;
// Replace kstock blocks with a clickable chip
out=out.replace(/```kstock\s*\n([^`]+)\n```/g,(m,body)=>{
const parts=body.trim().split('|');const code=parts[0].trim().replace(/\D/g,'');const period=(parts[1]||'day').trim();
if(!code)return m;
return `<span class="iv-chart-link" onclick="loadKStock('${code}','${code}','${period}')">📈 ${code} ${period} 차트 보기</span>`;
});
// Replace tradingview blocks with a clickable chip
out=out.replace(/```tradingview\s*\n([^`]+)\n```/g,(m,body)=>{
const parts=body.trim().split('|');const sym=parts[0].trim().toUpperCase();const iv=(parts[1]||'D').trim();
if(!sym)return m;
return `<span class="iv-chart-link" onclick="loadTV('${sym}','${iv}','${sym}')">📈 ${sym} 차트 보기</span>`;
});
return out
.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
// restore already-HTML chart links before further escaping
.replace(/&lt;span class="iv-chart-link"([^&]*)&gt;(.*?)&lt;\/span&gt;/g,(m)=>{
// this won't work — need to do escaping AFTER substitution
return m;
})
// Actually process differently below
;
}
// 대화 기록 복원용 — 실시간 응답과 같은 렌더러를 쓴다(예전 구현은 이스케이프만 하고 끝나 마크다운이 안 먹었다).
function renderMd(text){ return renderMdSafe(text); }
function renderMdSafe(text){
// Do chart block substitution BEFORE HTML escaping
@@ -753,6 +730,7 @@ function renderMdSafe(text){
out=out
.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
.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>')
@@ -762,12 +740,33 @@ function renderMdSafe(text){
.replace(/\n\n/g,'</p><p>').replace(/^/,'<p>').replace(/$/,'</p>')
.replace(/<p><\/p>/g,'').replace(/<p>(<h3>)/g,'$1').replace(/(<\/h3>)<\/p>/g,'$1')
.replace(/<p>(<pre>)/g,'$1').replace(/(<\/pre>)<\/p>/g,'$1')
.replace(/<p>(<ul>)/g,'$1').replace(/(<\/ul>)<\/p>/g,'$1');
.replace(/<p>(<ul>)/g,'$1').replace(/(<\/ul>)<\/p>/g,'$1')
.replace(/<p>(<div style="overflow-x:auto">)/g,'$1').replace(/(<\/table><\/div>)<\/p>/g,'$1');
// Restore chart placeholders
charts.forEach((html,i)=>{out=out.replace(new RegExp(`\x00CHART${i}\x00`,'g'),html);});
return out;
}
// 마크다운 표: '|'로 시작하는 연속 줄 + 두 번째 줄이 구분선(|---|---|)인 블록만 <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(){
const input=document.getElementById('chat-input');
const msg=input.value.trim();
+23 -1
View File
@@ -1231,6 +1231,7 @@ function renderMd(text){
return text
.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
.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>')
@@ -1239,7 +1240,28 @@ function renderMd(text){
.replace(/\n\n/g,'</p><p>').replace(/^/,'<p>').replace(/$/,'</p>')
.replace(/<p><\/p>/g,'').replace(/<p>(<h3>)/g,'$1').replace(/(<\/h3>)<\/p>/g,'$1')
.replace(/<p>(<pre>)/g,'$1').replace(/(<\/pre>)<\/p>/g,'$1')
.replace(/<p>(<blockquote)/g,'$1').replace(/(<\/blockquote>)<\/p>/g,'$1');
.replace(/<p>(<blockquote)/g,'$1').replace(/(<\/blockquote>)<\/p>/g,'$1')
.replace(/<p>(<div style="overflow-x:auto">)/g,'$1').replace(/(<\/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');
}
function sendToChat(message,statusMsg){