fix(tui): strip markdown from inbox previews
This commit is contained in:
parent
0bd75af069
commit
46662b7eec
3 changed files with 45 additions and 4 deletions
|
|
@ -14,6 +14,7 @@ import { tint } from "../theme/color"
|
|||
import { getScrollAcceleration } from "../util/scroll"
|
||||
import { withTimestampedFallback } from "@opencode-ai/util/session-title-fallback"
|
||||
import { activityVerb } from "../util/activity-verb"
|
||||
import { markdownPreview } from "../util/markdown-preview"
|
||||
import { Spinner } from "./spinner"
|
||||
import type { SessionTabsStatus } from "./session-tabs"
|
||||
|
||||
|
|
@ -176,14 +177,12 @@ export function SessionInbox() {
|
|||
const preview = assistant?.content
|
||||
.filter((part) => part.type === "text")
|
||||
.map((part) => part.text)
|
||||
.join(" ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim()
|
||||
.join("\n")
|
||||
return {
|
||||
sessionID: tab.sessionID,
|
||||
title: session ? withTimestampedFallback(session) : (tab.title ?? "Loading session…"),
|
||||
updated: session?.time.updated ?? 0,
|
||||
preview: preview || "No assistant response yet",
|
||||
preview: markdownPreview(preview ?? "") || "No assistant response yet",
|
||||
status,
|
||||
group: sessionInboxGroup(session?.time.updated ?? 0, status.busy),
|
||||
}
|
||||
|
|
|
|||
17
packages/tui/src/util/markdown-preview.ts
Normal file
17
packages/tui/src/util/markdown-preview.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export function markdownPreview(input: string) {
|
||||
return input
|
||||
.replace(/```[^\n]*\n?([\s\S]*?)```/g, "$1")
|
||||
.replace(/`([^`\n]+)`/g, "$1")
|
||||
.replace(/!\[([^\]]*)\]\((?:\\.|[^)])*\)/g, "$1")
|
||||
.replace(/\[([^\]]+)\]\((?:\\.|[^)])*\)/g, "$1")
|
||||
.replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1")
|
||||
.replace(/^ {0,3}(?:#{1,6}\s+|>\s?|(?:[-+*]|\d+[.)])\s+)/gm, "")
|
||||
.replace(/(?<!\\)~~(?=\S)([\s\S]*?\S)(?<!\\)~~/g, "$1")
|
||||
.replace(/(?<!\\)\*\*(?=\S)([\s\S]*?\S)(?<!\\)\*\*/g, "$1")
|
||||
.replace(/(?<!\\)__(?=\S)([\s\S]*?\S)(?<!\\)__/g, "$1")
|
||||
.replace(/(?<![\\*])\*(?=\S)([^*\n]*?\S)(?<!\\)\*(?!\*)/g, "$1")
|
||||
.replace(/(?<![\\_])_(?=\S)([^_\n]*?\S)(?<!\\)_(?!_)/g, "$1")
|
||||
.replace(/\\([\\`*_[\]{}()#+\-.!>|~])/g, "$1")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim()
|
||||
}
|
||||
25
packages/tui/test/util/markdown-preview.test.ts
Normal file
25
packages/tui/test/util/markdown-preview.test.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { expect, test } from "bun:test"
|
||||
import { markdownPreview } from "../../src/util/markdown-preview"
|
||||
|
||||
test("strips common Markdown presentation syntax from previews", () => {
|
||||
expect(markdownPreview("The capture shows **Reset** and a _short-lived_ **override**.")).toBe(
|
||||
"The capture shows Reset and a short-lived override.",
|
||||
)
|
||||
expect(markdownPreview("Use [`sessionRename`](https://example.com) with ~~old~~ new behavior.")).toBe(
|
||||
"Use sessionRename with old new behavior.",
|
||||
)
|
||||
expect(markdownPreview(" Review [the result][result].")).toBe("diagram Review the result.")
|
||||
})
|
||||
|
||||
test("flattens block syntax and code without losing its content", () => {
|
||||
expect(markdownPreview("# Result\n\n- First item\n- Second `item`\n> Finished")).toBe(
|
||||
"Result First item Second item Finished",
|
||||
)
|
||||
expect(markdownPreview("```ts\nconst answer = 42\n```")).toBe("const answer = 42")
|
||||
})
|
||||
|
||||
test("preserves literal asterisks, globs, and escaped punctuation", () => {
|
||||
expect(markdownPreview("Search **/*.ts, calculate 2 * 3, and keep \\*literal\\*. ")).toBe(
|
||||
"Search **/*.ts, calculate 2 * 3, and keep *literal*.",
|
||||
)
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue