fix: add per-channel markdown table conversion (#1495) (thanks @odysseus0)
This commit is contained in:
60
src/config/markdown-tables.ts
Normal file
60
src/config/markdown-tables.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { normalizeChannelId } from "../channels/plugins/index.js";
|
||||
import { normalizeAccountId } from "../routing/session-key.js";
|
||||
import type { ClawdbotConfig } from "./config.js";
|
||||
import type { MarkdownTableMode } from "./types.base.js";
|
||||
|
||||
type MarkdownConfigEntry = {
|
||||
markdown?: {
|
||||
tables?: MarkdownTableMode;
|
||||
};
|
||||
};
|
||||
|
||||
type MarkdownConfigSection = MarkdownConfigEntry & {
|
||||
accounts?: Record<string, MarkdownConfigEntry>;
|
||||
};
|
||||
|
||||
const DEFAULT_TABLE_MODES = new Map<string, MarkdownTableMode>([
|
||||
["signal", "bullets"],
|
||||
["whatsapp", "bullets"],
|
||||
]);
|
||||
|
||||
const isMarkdownTableMode = (value: unknown): value is MarkdownTableMode =>
|
||||
value === "off" || value === "bullets" || value === "code";
|
||||
|
||||
function resolveMarkdownModeFromSection(
|
||||
section: MarkdownConfigSection | undefined,
|
||||
accountId?: string | null,
|
||||
): MarkdownTableMode | undefined {
|
||||
if (!section) return undefined;
|
||||
const normalizedAccountId = normalizeAccountId(accountId);
|
||||
const accounts = section.accounts;
|
||||
if (accounts && typeof accounts === "object") {
|
||||
const direct = accounts[normalizedAccountId];
|
||||
const directMode = direct?.markdown?.tables;
|
||||
if (isMarkdownTableMode(directMode)) return directMode;
|
||||
const matchKey = Object.keys(accounts).find(
|
||||
(key) => key.toLowerCase() === normalizedAccountId.toLowerCase(),
|
||||
);
|
||||
const match = matchKey ? accounts[matchKey] : undefined;
|
||||
const matchMode = match?.markdown?.tables;
|
||||
if (isMarkdownTableMode(matchMode)) return matchMode;
|
||||
}
|
||||
const sectionMode = section.markdown?.tables;
|
||||
return isMarkdownTableMode(sectionMode) ? sectionMode : undefined;
|
||||
}
|
||||
|
||||
export function resolveMarkdownTableMode(params: {
|
||||
cfg?: Partial<ClawdbotConfig>;
|
||||
channel?: string | null;
|
||||
accountId?: string | null;
|
||||
}): MarkdownTableMode {
|
||||
const channel = normalizeChannelId(params.channel);
|
||||
const defaultMode = channel ? (DEFAULT_TABLE_MODES.get(channel) ?? "code") : "code";
|
||||
if (!channel || !params.cfg) return defaultMode;
|
||||
const channelsConfig = params.cfg.channels as Record<string, unknown> | undefined;
|
||||
const section = (channelsConfig?.[channel] ??
|
||||
(params.cfg as Record<string, unknown> | undefined)?.[channel]) as
|
||||
| MarkdownConfigSection
|
||||
| undefined;
|
||||
return resolveMarkdownModeFromSection(section, params.accountId) ?? defaultMode;
|
||||
}
|
||||
Reference in New Issue
Block a user