feat(dingtalk): make wide-table grouping switchable for wide-screen PC cards

DINGTALK_CARD_WIDE_TABLE_GROUP=off keeps over-wide answer tables as tables on the final frame (still replaces <br> inside cells) so the PC wide-screen card can lay them out itself.
This commit is contained in:
hermes
2026-09-18 11:45:55 +08:00
parent 882ac62433
commit 92324b4242
6 changed files with 138 additions and 8 deletions
+23 -4
View File
@@ -10,6 +10,19 @@ import (
// (one character per line) in the streaming card renderer.
const maxTableCellVisualWidth = 32
// tableNormalizeOptions controls how a final answer is adapted to the card
// renderer. The zero value keeps the historical behaviour.
type tableNormalizeOptions struct {
// groupWideTables rewrites tables containing over-wide cells into grouped
// lists (bold row heading + "- header:value" lines). Disable it when the
// client lays out wide tables itself, so the answer keeps its real table.
groupWideTables bool
}
func defaultTableNormalizeOptions() tableNormalizeOptions {
return tableNormalizeOptions{groupWideTables: true}
}
// normalizeWideMarkdownTables makes final answers render reliably on DingTalk
// cards without changing the knowledge itself. It (1) replaces <br> inside
// table cells with a full-width separator, and (2) rewrites any table that
@@ -17,6 +30,10 @@ const maxTableCellVisualWidth = 32
// followed by one "- header:value" line per column. Compact tables pass
// through unchanged. Tables inside code fences are never touched.
func normalizeWideMarkdownTables(content string) string {
return normalizeMarkdownTables(content, defaultTableNormalizeOptions())
}
func normalizeMarkdownTables(content string, options tableNormalizeOptions) string {
if !strings.Contains(content, "|") {
return content
}
@@ -36,7 +53,7 @@ func normalizeWideMarkdownTables(content string) string {
strings.HasPrefix(strings.TrimSpace(lines[end+1].text), "|") {
end++
}
out = append(out, normalizeTableBlock(lines[i:end+1])...)
out = append(out, normalizeTableBlock(lines[i:end+1], options)...)
i = end + 1
continue
}
@@ -46,7 +63,7 @@ func normalizeWideMarkdownTables(content string) string {
return strings.Join(out, "\n")
}
func normalizeTableBlock(rows []markdownStreamLine) []string {
func normalizeTableBlock(rows []markdownStreamLine, options tableNormalizeOptions) []string {
cells := make([][]string, 0, len(rows))
for idx, row := range rows {
if idx == 1 { // delimiter row
@@ -63,9 +80,11 @@ func normalizeTableBlock(rows []markdownStreamLine) []string {
}
}
header := cells[0]
if maxWidth <= maxTableCellVisualWidth {
if maxWidth <= maxTableCellVisualWidth || !options.groupWideTables {
// Compact tables keep their exact original bytes unless a cell carries
// <br>, which the card renderer mishandles inside cells.
// <br>, which the card renderer mishandles inside cells. The same path
// serves over-wide tables when grouping is disabled: the table stays a
// table and only <br> is replaced.
if !blockHasBreak(rows) {
rebuilt := make([]string, len(rows))
for i, row := range rows {