@openclaw/feishu 2026.2.25 → 2026.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +2 -0
- package/package.json +2 -1
- package/skills/feishu-doc/SKILL.md +109 -3
- package/src/accounts.test.ts +161 -0
- package/src/accounts.ts +76 -8
- package/src/async.ts +62 -0
- package/src/bitable.ts +189 -215
- package/src/bot.card-action.test.ts +63 -0
- package/src/bot.checkBotMentioned.test.ts +56 -1
- package/src/bot.test.ts +1271 -56
- package/src/bot.ts +499 -215
- package/src/card-action.ts +79 -0
- package/src/channel.ts +26 -4
- package/src/chat-schema.ts +24 -0
- package/src/chat.test.ts +89 -0
- package/src/chat.ts +130 -0
- package/src/client.test.ts +121 -0
- package/src/client.ts +13 -0
- package/src/config-schema.test.ts +101 -1
- package/src/config-schema.ts +66 -11
- package/src/dedup.ts +47 -1
- package/src/doc-schema.ts +135 -0
- package/src/docx-batch-insert.ts +190 -0
- package/src/docx-color-text.ts +149 -0
- package/src/docx-table-ops.ts +298 -0
- package/src/docx.account-selection.test.ts +70 -0
- package/src/docx.test.ts +331 -9
- package/src/docx.ts +996 -72
- package/src/drive.ts +38 -33
- package/src/media.test.ts +227 -7
- package/src/media.ts +52 -11
- package/src/mention.ts +1 -1
- package/src/monitor.account.ts +534 -0
- package/src/monitor.reaction.test.ts +578 -0
- package/src/monitor.startup.test.ts +203 -0
- package/src/monitor.startup.ts +51 -0
- package/src/monitor.state.defaults.test.ts +46 -0
- package/src/monitor.state.ts +152 -0
- package/src/monitor.test-mocks.ts +12 -0
- package/src/monitor.transport.ts +163 -0
- package/src/monitor.ts +44 -346
- package/src/monitor.webhook-security.test.ts +53 -10
- package/src/onboarding.status.test.ts +25 -0
- package/src/onboarding.ts +144 -52
- package/src/outbound.test.ts +181 -0
- package/src/outbound.ts +94 -7
- package/src/perm.ts +37 -30
- package/src/policy.test.ts +56 -1
- package/src/policy.ts +5 -1
- package/src/post.test.ts +105 -0
- package/src/post.ts +274 -0
- package/src/probe.test.ts +271 -0
- package/src/probe.ts +131 -19
- package/src/reply-dispatcher.test.ts +300 -0
- package/src/reply-dispatcher.ts +159 -46
- package/src/secret-input.ts +19 -0
- package/src/send-target.test.ts +74 -0
- package/src/send-target.ts +6 -2
- package/src/send.reply-fallback.test.ts +105 -0
- package/src/send.test.ts +168 -0
- package/src/send.ts +143 -18
- package/src/streaming-card.ts +131 -43
- package/src/targets.test.ts +55 -1
- package/src/targets.ts +32 -7
- package/src/tool-account-routing.test.ts +129 -0
- package/src/tool-account.ts +70 -0
- package/src/tool-factory-test-harness.ts +76 -0
- package/src/tools-config.test.ts +21 -0
- package/src/tools-config.ts +2 -1
- package/src/types.ts +10 -1
- package/src/typing.test.ts +144 -0
- package/src/typing.ts +140 -10
- package/src/wiki.ts +55 -50
package/src/policy.test.ts
CHANGED
|
@@ -1,7 +1,62 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isFeishuGroupAllowed,
|
|
4
|
+
resolveFeishuAllowlistMatch,
|
|
5
|
+
resolveFeishuGroupConfig,
|
|
6
|
+
} from "./policy.js";
|
|
7
|
+
import type { FeishuConfig } from "./types.js";
|
|
3
8
|
|
|
4
9
|
describe("feishu policy", () => {
|
|
10
|
+
describe("resolveFeishuGroupConfig", () => {
|
|
11
|
+
it("falls back to wildcard group config when direct match is missing", () => {
|
|
12
|
+
const cfg = {
|
|
13
|
+
groups: {
|
|
14
|
+
"*": { requireMention: false },
|
|
15
|
+
"oc-explicit": { requireMention: true },
|
|
16
|
+
},
|
|
17
|
+
} as unknown as FeishuConfig;
|
|
18
|
+
|
|
19
|
+
const resolved = resolveFeishuGroupConfig({
|
|
20
|
+
cfg,
|
|
21
|
+
groupId: "oc-missing",
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(resolved).toEqual({ requireMention: false });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("prefers exact group config over wildcard", () => {
|
|
28
|
+
const cfg = {
|
|
29
|
+
groups: {
|
|
30
|
+
"*": { requireMention: false },
|
|
31
|
+
"oc-explicit": { requireMention: true },
|
|
32
|
+
},
|
|
33
|
+
} as unknown as FeishuConfig;
|
|
34
|
+
|
|
35
|
+
const resolved = resolveFeishuGroupConfig({
|
|
36
|
+
cfg,
|
|
37
|
+
groupId: "oc-explicit",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(resolved).toEqual({ requireMention: true });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("keeps case-insensitive matching for explicit group ids", () => {
|
|
44
|
+
const cfg = {
|
|
45
|
+
groups: {
|
|
46
|
+
"*": { requireMention: false },
|
|
47
|
+
OC_UPPER: { requireMention: true },
|
|
48
|
+
},
|
|
49
|
+
} as unknown as FeishuConfig;
|
|
50
|
+
|
|
51
|
+
const resolved = resolveFeishuGroupConfig({
|
|
52
|
+
cfg,
|
|
53
|
+
groupId: "oc_upper",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(resolved).toEqual({ requireMention: true });
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
5
60
|
describe("resolveFeishuAllowlistMatch", () => {
|
|
6
61
|
it("allows wildcard", () => {
|
|
7
62
|
expect(
|
package/src/policy.ts
CHANGED
|
@@ -56,6 +56,7 @@ export function resolveFeishuGroupConfig(params: {
|
|
|
56
56
|
groupId?: string | null;
|
|
57
57
|
}): FeishuGroupConfig | undefined {
|
|
58
58
|
const groups = params.cfg?.groups ?? {};
|
|
59
|
+
const wildcard = groups["*"];
|
|
59
60
|
const groupId = params.groupId?.trim();
|
|
60
61
|
if (!groupId) {
|
|
61
62
|
return undefined;
|
|
@@ -68,7 +69,10 @@ export function resolveFeishuGroupConfig(params: {
|
|
|
68
69
|
|
|
69
70
|
const lowered = groupId.toLowerCase();
|
|
70
71
|
const matchKey = Object.keys(groups).find((key) => key.toLowerCase() === lowered);
|
|
71
|
-
|
|
72
|
+
if (matchKey) {
|
|
73
|
+
return groups[matchKey];
|
|
74
|
+
}
|
|
75
|
+
return wildcard;
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
export function resolveFeishuGroupToolPolicy(
|
package/src/post.test.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parsePostContent } from "./post.js";
|
|
3
|
+
|
|
4
|
+
describe("parsePostContent", () => {
|
|
5
|
+
it("renders title and styled text as markdown", () => {
|
|
6
|
+
const content = JSON.stringify({
|
|
7
|
+
title: "Daily *Plan*",
|
|
8
|
+
content: [
|
|
9
|
+
[
|
|
10
|
+
{ tag: "text", text: "Bold", style: { bold: true } },
|
|
11
|
+
{ tag: "text", text: " " },
|
|
12
|
+
{ tag: "text", text: "Italic", style: { italic: true } },
|
|
13
|
+
{ tag: "text", text: " " },
|
|
14
|
+
{ tag: "text", text: "Underline", style: { underline: true } },
|
|
15
|
+
{ tag: "text", text: " " },
|
|
16
|
+
{ tag: "text", text: "Strike", style: { strikethrough: true } },
|
|
17
|
+
{ tag: "text", text: " " },
|
|
18
|
+
{ tag: "text", text: "Code", style: { code: true, bold: true } },
|
|
19
|
+
],
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const result = parsePostContent(content);
|
|
24
|
+
|
|
25
|
+
expect(result.textContent).toBe(
|
|
26
|
+
"Daily \\*Plan\\*\n\n**Bold** *Italic* <u>Underline</u> ~~Strike~~ `Code`",
|
|
27
|
+
);
|
|
28
|
+
expect(result.imageKeys).toEqual([]);
|
|
29
|
+
expect(result.mentionedOpenIds).toEqual([]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("renders links and mentions", () => {
|
|
33
|
+
const content = JSON.stringify({
|
|
34
|
+
title: "",
|
|
35
|
+
content: [
|
|
36
|
+
[
|
|
37
|
+
{ tag: "a", text: "Docs [v2]", href: "https://example.com/guide(a)" },
|
|
38
|
+
{ tag: "text", text: " " },
|
|
39
|
+
{ tag: "at", user_name: "alice_bob" },
|
|
40
|
+
{ tag: "text", text: " " },
|
|
41
|
+
{ tag: "at", open_id: "ou_123" },
|
|
42
|
+
{ tag: "text", text: " " },
|
|
43
|
+
{ tag: "a", href: "https://example.com/no-text" },
|
|
44
|
+
],
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const result = parsePostContent(content);
|
|
49
|
+
|
|
50
|
+
expect(result.textContent).toBe(
|
|
51
|
+
"[Docs \\[v2\\]](https://example.com/guide(a)) @alice\\_bob @ou\\_123 [https://example.com/no\\-text](https://example.com/no-text)",
|
|
52
|
+
);
|
|
53
|
+
expect(result.mentionedOpenIds).toEqual(["ou_123"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("inserts image placeholders and collects image keys", () => {
|
|
57
|
+
const content = JSON.stringify({
|
|
58
|
+
title: "",
|
|
59
|
+
content: [
|
|
60
|
+
[
|
|
61
|
+
{ tag: "text", text: "Before " },
|
|
62
|
+
{ tag: "img", image_key: "img_1" },
|
|
63
|
+
{ tag: "text", text: " after" },
|
|
64
|
+
],
|
|
65
|
+
[{ tag: "img", image_key: "img_2" }],
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const result = parsePostContent(content);
|
|
70
|
+
|
|
71
|
+
expect(result.textContent).toBe("Before ![image] after\n![image]");
|
|
72
|
+
expect(result.imageKeys).toEqual(["img_1", "img_2"]);
|
|
73
|
+
expect(result.mentionedOpenIds).toEqual([]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("supports locale wrappers", () => {
|
|
77
|
+
const wrappedByPost = JSON.stringify({
|
|
78
|
+
post: {
|
|
79
|
+
zh_cn: {
|
|
80
|
+
title: "标题",
|
|
81
|
+
content: [[{ tag: "text", text: "内容A" }]],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const wrappedByLocale = JSON.stringify({
|
|
86
|
+
zh_cn: {
|
|
87
|
+
title: "标题",
|
|
88
|
+
content: [[{ tag: "text", text: "内容B" }]],
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(parsePostContent(wrappedByPost)).toEqual({
|
|
93
|
+
textContent: "标题\n\n内容A",
|
|
94
|
+
imageKeys: [],
|
|
95
|
+
mediaKeys: [],
|
|
96
|
+
mentionedOpenIds: [],
|
|
97
|
+
});
|
|
98
|
+
expect(parsePostContent(wrappedByLocale)).toEqual({
|
|
99
|
+
textContent: "标题\n\n内容B",
|
|
100
|
+
imageKeys: [],
|
|
101
|
+
mediaKeys: [],
|
|
102
|
+
mentionedOpenIds: [],
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
package/src/post.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { normalizeFeishuExternalKey } from "./external-keys.js";
|
|
2
|
+
|
|
3
|
+
const FALLBACK_POST_TEXT = "[Rich text message]";
|
|
4
|
+
const MARKDOWN_SPECIAL_CHARS = /([\\`*_{}\[\]()#+\-!|>~])/g;
|
|
5
|
+
|
|
6
|
+
type PostParseResult = {
|
|
7
|
+
textContent: string;
|
|
8
|
+
imageKeys: string[];
|
|
9
|
+
mediaKeys: Array<{ fileKey: string; fileName?: string }>;
|
|
10
|
+
mentionedOpenIds: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type PostPayload = {
|
|
14
|
+
title: string;
|
|
15
|
+
content: unknown[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
19
|
+
return typeof value === "object" && value !== null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function toStringOrEmpty(value: unknown): string {
|
|
23
|
+
return typeof value === "string" ? value : "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function escapeMarkdownText(text: string): string {
|
|
27
|
+
return text.replace(MARKDOWN_SPECIAL_CHARS, "\\$1");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toBoolean(value: unknown): boolean {
|
|
31
|
+
return value === true || value === 1 || value === "true";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isStyleEnabled(style: Record<string, unknown> | undefined, key: string): boolean {
|
|
35
|
+
if (!style) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return toBoolean(style[key]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function wrapInlineCode(text: string): string {
|
|
42
|
+
const maxRun = Math.max(0, ...(text.match(/`+/g) ?? []).map((run) => run.length));
|
|
43
|
+
const fence = "`".repeat(maxRun + 1);
|
|
44
|
+
const needsPadding = text.startsWith("`") || text.endsWith("`");
|
|
45
|
+
const body = needsPadding ? ` ${text} ` : text;
|
|
46
|
+
return `${fence}${body}${fence}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function sanitizeFenceLanguage(language: string): string {
|
|
50
|
+
return language.trim().replace(/[^A-Za-z0-9_+#.-]/g, "");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function renderTextElement(element: Record<string, unknown>): string {
|
|
54
|
+
const text = toStringOrEmpty(element.text);
|
|
55
|
+
const style = isRecord(element.style) ? element.style : undefined;
|
|
56
|
+
|
|
57
|
+
if (isStyleEnabled(style, "code")) {
|
|
58
|
+
return wrapInlineCode(text);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let rendered = escapeMarkdownText(text);
|
|
62
|
+
if (!rendered) {
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (isStyleEnabled(style, "bold")) {
|
|
67
|
+
rendered = `**${rendered}**`;
|
|
68
|
+
}
|
|
69
|
+
if (isStyleEnabled(style, "italic")) {
|
|
70
|
+
rendered = `*${rendered}*`;
|
|
71
|
+
}
|
|
72
|
+
if (isStyleEnabled(style, "underline")) {
|
|
73
|
+
rendered = `<u>${rendered}</u>`;
|
|
74
|
+
}
|
|
75
|
+
if (
|
|
76
|
+
isStyleEnabled(style, "strikethrough") ||
|
|
77
|
+
isStyleEnabled(style, "line_through") ||
|
|
78
|
+
isStyleEnabled(style, "lineThrough")
|
|
79
|
+
) {
|
|
80
|
+
rendered = `~~${rendered}~~`;
|
|
81
|
+
}
|
|
82
|
+
return rendered;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function renderLinkElement(element: Record<string, unknown>): string {
|
|
86
|
+
const href = toStringOrEmpty(element.href).trim();
|
|
87
|
+
const rawText = toStringOrEmpty(element.text);
|
|
88
|
+
const text = rawText || href;
|
|
89
|
+
if (!text) {
|
|
90
|
+
return "";
|
|
91
|
+
}
|
|
92
|
+
if (!href) {
|
|
93
|
+
return escapeMarkdownText(text);
|
|
94
|
+
}
|
|
95
|
+
return `[${escapeMarkdownText(text)}](${href})`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function renderMentionElement(element: Record<string, unknown>): string {
|
|
99
|
+
const mention =
|
|
100
|
+
toStringOrEmpty(element.user_name) ||
|
|
101
|
+
toStringOrEmpty(element.user_id) ||
|
|
102
|
+
toStringOrEmpty(element.open_id);
|
|
103
|
+
if (!mention) {
|
|
104
|
+
return "";
|
|
105
|
+
}
|
|
106
|
+
return `@${escapeMarkdownText(mention)}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function renderEmotionElement(element: Record<string, unknown>): string {
|
|
110
|
+
const text =
|
|
111
|
+
toStringOrEmpty(element.emoji) ||
|
|
112
|
+
toStringOrEmpty(element.text) ||
|
|
113
|
+
toStringOrEmpty(element.emoji_type);
|
|
114
|
+
return escapeMarkdownText(text);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function renderCodeBlockElement(element: Record<string, unknown>): string {
|
|
118
|
+
const language = sanitizeFenceLanguage(
|
|
119
|
+
toStringOrEmpty(element.language) || toStringOrEmpty(element.lang),
|
|
120
|
+
);
|
|
121
|
+
const code = (toStringOrEmpty(element.text) || toStringOrEmpty(element.content)).replace(
|
|
122
|
+
/\r\n/g,
|
|
123
|
+
"\n",
|
|
124
|
+
);
|
|
125
|
+
const trailingNewline = code.endsWith("\n") ? "" : "\n";
|
|
126
|
+
return `\`\`\`${language}\n${code}${trailingNewline}\`\`\``;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function renderElement(
|
|
130
|
+
element: unknown,
|
|
131
|
+
imageKeys: string[],
|
|
132
|
+
mediaKeys: Array<{ fileKey: string; fileName?: string }>,
|
|
133
|
+
mentionedOpenIds: string[],
|
|
134
|
+
): string {
|
|
135
|
+
if (!isRecord(element)) {
|
|
136
|
+
return escapeMarkdownText(toStringOrEmpty(element));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const tag = toStringOrEmpty(element.tag).toLowerCase();
|
|
140
|
+
switch (tag) {
|
|
141
|
+
case "text":
|
|
142
|
+
return renderTextElement(element);
|
|
143
|
+
case "a":
|
|
144
|
+
return renderLinkElement(element);
|
|
145
|
+
case "at":
|
|
146
|
+
{
|
|
147
|
+
const mentioned = toStringOrEmpty(element.open_id) || toStringOrEmpty(element.user_id);
|
|
148
|
+
const normalizedMention = normalizeFeishuExternalKey(mentioned);
|
|
149
|
+
if (normalizedMention) {
|
|
150
|
+
mentionedOpenIds.push(normalizedMention);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return renderMentionElement(element);
|
|
154
|
+
case "img": {
|
|
155
|
+
const imageKey = normalizeFeishuExternalKey(toStringOrEmpty(element.image_key));
|
|
156
|
+
if (imageKey) {
|
|
157
|
+
imageKeys.push(imageKey);
|
|
158
|
+
}
|
|
159
|
+
return "![image]";
|
|
160
|
+
}
|
|
161
|
+
case "media": {
|
|
162
|
+
const fileKey = normalizeFeishuExternalKey(toStringOrEmpty(element.file_key));
|
|
163
|
+
if (fileKey) {
|
|
164
|
+
const fileName = toStringOrEmpty(element.file_name) || undefined;
|
|
165
|
+
mediaKeys.push({ fileKey, fileName });
|
|
166
|
+
}
|
|
167
|
+
return "[media]";
|
|
168
|
+
}
|
|
169
|
+
case "emotion":
|
|
170
|
+
return renderEmotionElement(element);
|
|
171
|
+
case "br":
|
|
172
|
+
return "\n";
|
|
173
|
+
case "hr":
|
|
174
|
+
return "\n\n---\n\n";
|
|
175
|
+
case "code": {
|
|
176
|
+
const code = toStringOrEmpty(element.text) || toStringOrEmpty(element.content);
|
|
177
|
+
return code ? wrapInlineCode(code) : "";
|
|
178
|
+
}
|
|
179
|
+
case "code_block":
|
|
180
|
+
case "pre":
|
|
181
|
+
return renderCodeBlockElement(element);
|
|
182
|
+
default:
|
|
183
|
+
return escapeMarkdownText(toStringOrEmpty(element.text));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function toPostPayload(candidate: unknown): PostPayload | null {
|
|
188
|
+
if (!isRecord(candidate) || !Array.isArray(candidate.content)) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
title: toStringOrEmpty(candidate.title),
|
|
193
|
+
content: candidate.content,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function resolveLocalePayload(candidate: unknown): PostPayload | null {
|
|
198
|
+
const direct = toPostPayload(candidate);
|
|
199
|
+
if (direct) {
|
|
200
|
+
return direct;
|
|
201
|
+
}
|
|
202
|
+
if (!isRecord(candidate)) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
for (const value of Object.values(candidate)) {
|
|
206
|
+
const localePayload = toPostPayload(value);
|
|
207
|
+
if (localePayload) {
|
|
208
|
+
return localePayload;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function resolvePostPayload(parsed: unknown): PostPayload | null {
|
|
215
|
+
const direct = toPostPayload(parsed);
|
|
216
|
+
if (direct) {
|
|
217
|
+
return direct;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (!isRecord(parsed)) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const wrappedPost = resolveLocalePayload(parsed.post);
|
|
225
|
+
if (wrappedPost) {
|
|
226
|
+
return wrappedPost;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return resolveLocalePayload(parsed);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function parsePostContent(content: string): PostParseResult {
|
|
233
|
+
try {
|
|
234
|
+
const parsed = JSON.parse(content);
|
|
235
|
+
const payload = resolvePostPayload(parsed);
|
|
236
|
+
if (!payload) {
|
|
237
|
+
return {
|
|
238
|
+
textContent: FALLBACK_POST_TEXT,
|
|
239
|
+
imageKeys: [],
|
|
240
|
+
mediaKeys: [],
|
|
241
|
+
mentionedOpenIds: [],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const imageKeys: string[] = [];
|
|
246
|
+
const mediaKeys: Array<{ fileKey: string; fileName?: string }> = [];
|
|
247
|
+
const mentionedOpenIds: string[] = [];
|
|
248
|
+
const paragraphs: string[] = [];
|
|
249
|
+
|
|
250
|
+
for (const paragraph of payload.content) {
|
|
251
|
+
if (!Array.isArray(paragraph)) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
let renderedParagraph = "";
|
|
255
|
+
for (const element of paragraph) {
|
|
256
|
+
renderedParagraph += renderElement(element, imageKeys, mediaKeys, mentionedOpenIds);
|
|
257
|
+
}
|
|
258
|
+
paragraphs.push(renderedParagraph);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const title = escapeMarkdownText(payload.title.trim());
|
|
262
|
+
const body = paragraphs.join("\n").trim();
|
|
263
|
+
const textContent = [title, body].filter(Boolean).join("\n\n").trim();
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
textContent: textContent || FALLBACK_POST_TEXT,
|
|
267
|
+
imageKeys,
|
|
268
|
+
mediaKeys,
|
|
269
|
+
mentionedOpenIds,
|
|
270
|
+
};
|
|
271
|
+
} catch {
|
|
272
|
+
return { textContent: FALLBACK_POST_TEXT, imageKeys: [], mediaKeys: [], mentionedOpenIds: [] };
|
|
273
|
+
}
|
|
274
|
+
}
|