Merge commit '8c700b1e080a29a02375664e856bc6e5b757c7c3' into feature/negodata
This commit is contained in:
commit
9ac8fe6363
26 files changed
+530
-62
No files matched your search
@@ -0,0 +1,79 @@
|
||||
import { Fragment, type ReactNode } from 'react';
|
||||
|
||||
/**
|
||||
* 봇 스크립트 경량 마크업(마크다운 서브셋) 렌더러 — 공급사 프론트(frontend emphasis.tsx)와 동일 규칙.
|
||||
*
|
||||
* agent 스크립트는 의미(텍스트)만 소유하고 표현(굵기·밑줄·색)은 각 프론트가 결정한다.
|
||||
* 지원 문법(중첩 가능): **굵게** __밑줄__ {{강조|빨강}} {{안내|파랑}} (검정은 기본, 마커 없음).
|
||||
* 색 토큰명은 시맨틱이며 실제 색값은 이 프론트의 디자인 토큰(tokens.css)이 결정 — 다크모드 안전.
|
||||
* innerHTML 없이 React 노드로 분해(XSS 안전). 짝이 안 맞는 마커는 평문으로 출력.
|
||||
*/
|
||||
|
||||
// 시맨틱 색 토큰 → 이 프론트의 Tailwind 색 클래스(tokens.css --color-destructive/--color-info).
|
||||
const COLOR_CLASS: Record<string, string> = {
|
||||
강조: 'text-destructive',
|
||||
안내: 'text-info',
|
||||
};
|
||||
|
||||
export function renderEmphasis(text: string): ReactNode {
|
||||
if (!text || !/\*\*|__|\{\{/.test(text)) return text;
|
||||
return <Fragment>{parseInline(text, 'e')}</Fragment>;
|
||||
}
|
||||
|
||||
function parseInline(text: string, keyPrefix: string): ReactNode[] {
|
||||
const nodes: ReactNode[] = [];
|
||||
let pos = 0;
|
||||
let seq = 0;
|
||||
while (pos < text.length) {
|
||||
const hit = nextMarker(text, pos);
|
||||
if (!hit) {
|
||||
nodes.push(text.slice(pos));
|
||||
break;
|
||||
}
|
||||
if (hit.start > pos) nodes.push(text.slice(pos, hit.start));
|
||||
const key = `${keyPrefix}-${seq++}`;
|
||||
const inner = parseInline(hit.inner, key);
|
||||
nodes.push(wrap(hit.kind, hit.token, inner, key));
|
||||
pos = hit.end;
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
type Hit = { kind: 'bold' | 'underline' | 'color'; start: number; end: number; inner: string; token?: string };
|
||||
|
||||
function nextMarker(text: string, pos: number): Hit | null {
|
||||
let best: Hit | null = null;
|
||||
const consider = (h: Hit | null) => {
|
||||
if (h && (!best || h.start < best.start)) best = h;
|
||||
};
|
||||
consider(matchDelim(text, pos, '**', 'bold'));
|
||||
consider(matchDelim(text, pos, '__', 'underline'));
|
||||
consider(matchColor(text, pos));
|
||||
return best;
|
||||
}
|
||||
|
||||
function matchDelim(text: string, pos: number, delim: '**' | '__', kind: 'bold' | 'underline'): Hit | null {
|
||||
const open = text.indexOf(delim, pos);
|
||||
if (open < 0) return null;
|
||||
const close = text.indexOf(delim, open + delim.length);
|
||||
if (close < 0) return null;
|
||||
return { kind, start: open, end: close + delim.length, inner: text.slice(open + delim.length, close) };
|
||||
}
|
||||
|
||||
function matchColor(text: string, pos: number): Hit | null {
|
||||
const open = text.indexOf('{{', pos);
|
||||
if (open < 0) return null;
|
||||
const bar = text.indexOf('|', open + 2);
|
||||
if (bar < 0) return null;
|
||||
const close = text.indexOf('}}', bar + 1);
|
||||
if (close < 0) return null;
|
||||
const token = text.slice(open + 2, bar).trim();
|
||||
if (!(token in COLOR_CLASS)) return null;
|
||||
return { kind: 'color', start: open, end: close + 2, inner: text.slice(bar + 1, close), token };
|
||||
}
|
||||
|
||||
function wrap(kind: Hit['kind'], token: string | undefined, inner: ReactNode[], key: string): ReactNode {
|
||||
if (kind === 'bold') return <strong key={key} className="font-semibold">{inner}</strong>;
|
||||
if (kind === 'underline') return <u key={key}>{inner}</u>;
|
||||
return <span key={key} className={token ? COLOR_CLASS[token] : undefined}>{inner}</span>;
|
||||
}
|
||||
Reference in new issue
Block a user