mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 09:41:23 +00:00
fix: add per-channel markdown table conversion (#1495) (thanks @odysseus0)
This commit is contained in:
34
src/markdown/tables.ts
Normal file
34
src/markdown/tables.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { MarkdownTableMode } from "../config/types.base.js";
|
||||
import { markdownToIRWithMeta } from "./ir.js";
|
||||
import { renderMarkdownWithMarkers } from "./render.js";
|
||||
|
||||
const MARKDOWN_STYLE_MARKERS = {
|
||||
bold: { open: "**", close: "**" },
|
||||
italic: { open: "_", close: "_" },
|
||||
strikethrough: { open: "~~", close: "~~" },
|
||||
code: { open: "`", close: "`" },
|
||||
code_block: { open: "```\n", close: "```" },
|
||||
} as const;
|
||||
|
||||
export function convertMarkdownTables(markdown: string, mode: MarkdownTableMode): string {
|
||||
if (!markdown || mode === "off") return markdown;
|
||||
const { ir, hasTables } = markdownToIRWithMeta(markdown, {
|
||||
linkify: false,
|
||||
autolink: false,
|
||||
headingStyle: "none",
|
||||
blockquotePrefix: "",
|
||||
tableMode: mode,
|
||||
});
|
||||
if (!hasTables) return markdown;
|
||||
return renderMarkdownWithMarkers(ir, {
|
||||
styleMarkers: MARKDOWN_STYLE_MARKERS,
|
||||
escapeText: (text) => text,
|
||||
buildLink: (link, text) => {
|
||||
const href = link.href.trim();
|
||||
if (!href) return null;
|
||||
const label = text.slice(link.start, link.end);
|
||||
if (!label) return null;
|
||||
return { start: link.start, end: link.end, open: "[", close: `](${href})` };
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user