Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/ui/src/lib/components/scenes/ai/markdown-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ function escapeHtml(text: string): string {
.replace(/'/g, ''')
}

function createTableCellAttrs(cell: Tokens.TableCell): string {
const attrs = []

if (cell.align) {
attrs.push(`style="text-align:${cell.align}"`)
}

if (cell.text.trim()) {
attrs.push(`title="${escapeHtml(cell.text)}"`)
}

return attrs.length > 0 ? ` ${attrs.join(' ')}` : ''
}

/**
* 创建自定义渲染器对象
*
Expand Down Expand Up @@ -115,16 +129,14 @@ function createRendererObject(options: Required<MarkdownParseOptions>): Renderer
table(token: Tokens.Table): string {
let headerHtml = ''
for (const cell of token.header) {
const align = cell.align ? ` style="text-align:${cell.align}"` : ''
headerHtml += `<th${align}>${this.parser.parseInline(cell.tokens)}</th>`
headerHtml += `<th${createTableCellAttrs(cell)}>${this.parser.parseInline(cell.tokens)}</th>`
}

let bodyHtml = ''
for (const row of token.rows) {
let rowHtml = ''
for (const cell of row) {
const align = cell.align ? ` style="text-align:${cell.align}"` : ''
rowHtml += `<td${align}>${this.parser.parseInline(cell.tokens)}</td>`
rowHtml += `<td${createTableCellAttrs(cell)}>${this.parser.parseInline(cell.tokens)}</td>`
}
bodyHtml += `<tr>${rowHtml}</tr>`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@
<thead>
<tr>
{#each tableColumns as column (column.key)}
<th>{column.label}</th>
<th title={column.label}>{column.label}</th>
{/each}
{#if editable}
<th class='hai-ai-table-action-head'>{uiM('data_table_actions')}</th>
Expand All @@ -1122,11 +1122,13 @@
ondrop={event => handleRowDrop(row.row_id, event)}
>
{#each tableColumns as column (column.key)}
{@const cellInputValue = getCellInputValue(row, column.key)}
<td>
<input
class={resolveCellInputClass(column.type)}
type={column.type === 'number' ? 'number' : 'text'}
value={getCellInputValue(row, column.key)}
value={cellInputValue}
title={cellInputValue}
disabled={!editable}
oninput={event => handleCellInput(row.row_id, column, event)}
/>
Expand Down
12 changes: 10 additions & 2 deletions packages/ui/tests/markdown-parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,16 @@ describe('parseMarkdown - 表格', () => {
const html = parseMarkdown(md)
expect(html).toContain('hai-md-table-wrap')
expect(html).toContain('<table>')
expect(html).toContain('<th>')
expect(html).toContain('<td>')
expect(html).toContain('<th title="A">')
expect(html).toContain('<td title="1">')
})

it('adds title attributes to table cells', () => {
const md = '| A | B |\n| --- | --- |\n| long cell | value "quoted" |'
const html = parseMarkdown(md)
expect(html).toContain('<th title="A">')
expect(html).toContain('<td title="long cell">')
expect(html).toContain('<td title="value &quot;quoted&quot;">')
})
})

Expand Down
Loading