Merge remote-tracking branch 'origin/main' into feature/backend-frontend
This commit is contained in:
commit
7bfd77f6c3
24 files changed
+509
-45
No files matched your search
@@ -4,6 +4,7 @@ import { useChatStore } from '@/features/chat/stores/useChatStore'
|
||||
import { useChatInitStore } from '@/features/chat/stores/useChatInitStore'
|
||||
import { SessionStatus } from '@/apis/negotiation/negotiation.type'
|
||||
import type { ChatMessage as ChatMessageType } from '@/features/chat/types'
|
||||
import { renderEmphasis } from '@/features/chat/lib/emphasis'
|
||||
import { Indicator } from '@/features/chat/components/templates/Indicator'
|
||||
import { Summary } from '@/features/chat/components/templates/Summary'
|
||||
import { BidSummary } from '@/features/chat/components/templates/BidSummary'
|
||||
@@ -99,7 +100,8 @@ const BotMessage = memo(function BotMessage({ message, isFirst }: { message: Cha
|
||||
return (
|
||||
<div className="mb-[56px]">
|
||||
<div className={cn('flex flex-col', !isFirst && 'pt-[36px]')}>
|
||||
<div className="body-1-read-r">{message.script || ''}</div>
|
||||
{/* 봇 스크립트의 `**굵게**` 경량 마크업 해석 (표현 규칙은 프론트 소유 — emphasis.tsx) */}
|
||||
<div className="body-1-read-r">{renderEmphasis(message.script || '')}</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-4 w-full mt-[32px]">
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import { Fragment, type ReactNode } from 'react'
|
||||
|
||||
/**
|
||||
* 봇 스크립트 경량 마크업(마크다운 서브셋) 렌더러.
|
||||
*
|
||||
* 원칙: agent 스크립트는 의미(텍스트)만 소유하고 표현(굵기·밑줄·색)은 프론트가 결정한다.
|
||||
* 지원 문법(중첩 가능):
|
||||
* **굵게** → <strong>
|
||||
* __밑줄__ → <u>
|
||||
* {{강조|빨강}} → 시맨틱 색 토큰(강조=negative, 안내=info). 검정은 기본이라 마커 없음.
|
||||
*
|
||||
* 색 토큰명은 시맨틱(강조/안내)이며 실제 색값은 이 프론트의 디자인 토큰(index.css)이 결정한다
|
||||
* — hex 를 스크립트에 싣지 않으므로 다크모드·리브랜딩에 안전(선행 Chat_server 의 리치텍스트
|
||||
* JSON 이중 관리를 채택하지 않는다). innerHTML 없이 React 노드로 분해해 XSS 안전.
|
||||
* 짝이 맞지 않는 마커는 해석하지 않고 평문으로 출력한다. 봇 메시지에만 적용.
|
||||
*/
|
||||
|
||||
// 시맨틱 색 토큰 → 이 프론트의 Tailwind 색 클래스(index.css --color-negative/--color-info).
|
||||
const COLOR_CLASS: Record<string, string> = {
|
||||
강조: 'text-negative',
|
||||
안내: '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 }
|
||||
|
||||
// pos 이후 가장 이른 마커 1건. 닫힘이 없으면 무시(평문 처리).
|
||||
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