node-resource-utils 0.0.0-stage → 1.1.7

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.
Files changed (229) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -2
  3. package/lib/sync.js +121 -0
  4. package/package.json +14 -4
  5. package/resources/.cache/ref.b64 +1 -0
  6. package/resources/README.md +97 -0
  7. package/resources/_.env +6 -0
  8. package/resources/_.env.example +25 -0
  9. package/resources/_.gitignore +18 -0
  10. package/resources/_.lintstagedrc.json +9 -0
  11. package/resources/_.npmrc +5 -0
  12. package/resources/_.prettierrc +7 -0
  13. package/resources/commitlint.config.mjs +1 -0
  14. package/resources/content/docs/README.md +63 -0
  15. package/resources/content/docs/faq.md +107 -0
  16. package/resources/content/docs/features/_meta.json +5 -0
  17. package/resources/content/docs/features/ai.md +71 -0
  18. package/resources/content/docs/features/appearance.md +47 -0
  19. package/resources/content/docs/features/feedback.md +79 -0
  20. package/resources/content/docs/features/files.md +74 -0
  21. package/resources/content/docs/features/graph.md +66 -0
  22. package/resources/content/docs/features/inbox.md +61 -0
  23. package/resources/content/docs/guide/_meta.json +5 -0
  24. package/resources/content/docs/guide/account.md +76 -0
  25. package/resources/content/docs/guide/data.md +87 -0
  26. package/resources/content/docs/guide/navigation.md +81 -0
  27. package/resources/content/docs/guide/permissions.md +67 -0
  28. package/resources/content/docs/guide/system.md +92 -0
  29. package/resources/content/docs/quick-start.md +70 -0
  30. package/resources/content/docs/shortcuts.md +69 -0
  31. package/resources/eslint.config.mjs +17 -0
  32. package/resources/next-env.d.ts +6 -0
  33. package/resources/next.config.ts +13 -0
  34. package/resources/postcss.config.mjs +7 -0
  35. package/resources/prisma/org.config.ts +98 -0
  36. package/resources/prisma/schema.prisma +336 -0
  37. package/resources/prisma/seed.ts +721 -0
  38. package/resources/prisma/seedSafe.ts +347 -0
  39. package/resources/prisma.config.ts +14 -0
  40. package/resources/scripts/check-config.mjs +773 -0
  41. package/resources/scripts/lib/schema-graph.mjs +410 -0
  42. package/resources/scripts/patch-next-trace.mjs +86 -0
  43. package/resources/scripts/suggest-relations.mjs +105 -0
  44. package/resources/src/app/(admin)/ai/page.tsx +469 -0
  45. package/resources/src/app/(admin)/dashboard/page.tsx +260 -0
  46. package/resources/src/app/(admin)/demo/employees/page.tsx +508 -0
  47. package/resources/src/app/(admin)/demo/files/page.tsx +451 -0
  48. package/resources/src/app/(admin)/demo/loading/page.tsx +513 -0
  49. package/resources/src/app/(admin)/error.tsx +96 -0
  50. package/resources/src/app/(admin)/feedback/page.tsx +573 -0
  51. package/resources/src/app/(admin)/graph/page.tsx +1224 -0
  52. package/resources/src/app/(admin)/inbox/page.tsx +31 -0
  53. package/resources/src/app/(admin)/layout.tsx +58 -0
  54. package/resources/src/app/(admin)/loading.tsx +31 -0
  55. package/resources/src/app/(admin)/system/depts/page.tsx +415 -0
  56. package/resources/src/app/(admin)/system/logs/page.tsx +215 -0
  57. package/resources/src/app/(admin)/system/menus/page.tsx +403 -0
  58. package/resources/src/app/(admin)/system/roles/page.tsx +433 -0
  59. package/resources/src/app/(admin)/system/users/page.tsx +620 -0
  60. package/resources/src/app/api/account-recovery/route.ts +132 -0
  61. package/resources/src/app/api/ai/act/route.ts +70 -0
  62. package/resources/src/app/api/ai/ask/route.ts +82 -0
  63. package/resources/src/app/api/ai/chat/route.ts +404 -0
  64. package/resources/src/app/api/ai/report/route.ts +164 -0
  65. package/resources/src/app/api/auth/[...nextauth]/route.ts +3 -0
  66. package/resources/src/app/api/auth/change-password/route.ts +57 -0
  67. package/resources/src/app/api/auth/forgot/route.ts +98 -0
  68. package/resources/src/app/api/auth/register/route.ts +95 -0
  69. package/resources/src/app/api/auth/register-config/route.ts +51 -0
  70. package/resources/src/app/api/badges/route.ts +39 -0
  71. package/resources/src/app/api/depts/[id]/route.ts +145 -0
  72. package/resources/src/app/api/depts/route.ts +126 -0
  73. package/resources/src/app/api/docs/export/route.ts +87 -0
  74. package/resources/src/app/api/docs/parse/route.ts +57 -0
  75. package/resources/src/app/api/docs/search/route.ts +22 -0
  76. package/resources/src/app/api/employees/[id]/route.ts +147 -0
  77. package/resources/src/app/api/employees/export/route.ts +115 -0
  78. package/resources/src/app/api/employees/import/route.ts +205 -0
  79. package/resources/src/app/api/employees/route.ts +149 -0
  80. package/resources/src/app/api/export/route.ts +254 -0
  81. package/resources/src/app/api/favorites/route.ts +56 -0
  82. package/resources/src/app/api/feedback/ai/route.ts +31 -0
  83. package/resources/src/app/api/feedback/approve/route.ts +67 -0
  84. package/resources/src/app/api/feedback/report/route.ts +213 -0
  85. package/resources/src/app/api/feedback/route.ts +246 -0
  86. package/resources/src/app/api/files/[name]/route.ts +82 -0
  87. package/resources/src/app/api/graph/focus/route.ts +75 -0
  88. package/resources/src/app/api/graph/node/move/route.ts +48 -0
  89. package/resources/src/app/api/graph/node/route.ts +274 -0
  90. package/resources/src/app/api/graph/route.ts +272 -0
  91. package/resources/src/app/api/health/route.ts +11 -0
  92. package/resources/src/app/api/inbox/read/route.ts +36 -0
  93. package/resources/src/app/api/inbox/summary/route.ts +15 -0
  94. package/resources/src/app/api/logs/route.ts +82 -0
  95. package/resources/src/app/api/menus/[id]/route.ts +132 -0
  96. package/resources/src/app/api/menus/mine/route.ts +23 -0
  97. package/resources/src/app/api/menus/route.ts +89 -0
  98. package/resources/src/app/api/roles/[id]/menus/route.ts +73 -0
  99. package/resources/src/app/api/roles/[id]/route.ts +96 -0
  100. package/resources/src/app/api/roles/route.ts +114 -0
  101. package/resources/src/app/api/search/route.ts +22 -0
  102. package/resources/src/app/api/stats/route.ts +92 -0
  103. package/resources/src/app/api/upload/route.ts +38 -0
  104. package/resources/src/app/api/users/[id]/password/route.ts +55 -0
  105. package/resources/src/app/api/users/[id]/route.ts +145 -0
  106. package/resources/src/app/api/users/route.ts +153 -0
  107. package/resources/src/app/api/visits/route.ts +48 -0
  108. package/resources/src/app/change-password/ChangePasswordForm.tsx +174 -0
  109. package/resources/src/app/change-password/page.tsx +19 -0
  110. package/resources/src/app/docs/[[...slug]]/page.tsx +56 -0
  111. package/resources/src/app/docs/layout.tsx +34 -0
  112. package/resources/src/app/docs/loading.tsx +19 -0
  113. package/resources/src/app/docs/not-found.tsx +25 -0
  114. package/resources/src/app/forgot/page.tsx +392 -0
  115. package/resources/src/app/global-error.tsx +75 -0
  116. package/resources/src/app/globals.css +235 -0
  117. package/resources/src/app/layout.tsx +44 -0
  118. package/resources/src/app/login/page.tsx +267 -0
  119. package/resources/src/app/page.tsx +14 -0
  120. package/resources/src/app/register/page.tsx +237 -0
  121. package/resources/src/auth.ts +215 -0
  122. package/resources/src/components/AiPanel.tsx +970 -0
  123. package/resources/src/components/AuthContext.tsx +82 -0
  124. package/resources/src/components/DataTable.tsx +459 -0
  125. package/resources/src/components/EChart.tsx +140 -0
  126. package/resources/src/components/FeedbackAiModal.tsx +476 -0
  127. package/resources/src/components/FeedbackReportModal.tsx +479 -0
  128. package/resources/src/components/Markdown.tsx +532 -0
  129. package/resources/src/components/OntologyGraph.tsx +1196 -0
  130. package/resources/src/components/Providers.tsx +99 -0
  131. package/resources/src/components/VisitTracker.tsx +89 -0
  132. package/resources/src/components/admin/AdminShell.tsx +749 -0
  133. package/resources/src/components/admin/CountBadge.tsx +67 -0
  134. package/resources/src/components/admin/GlobalSearch.tsx +509 -0
  135. package/resources/src/components/admin/InboxBell.tsx +78 -0
  136. package/resources/src/components/admin/VisitStats.tsx +145 -0
  137. package/resources/src/components/admin/menu-icons.tsx +56 -0
  138. package/resources/src/components/ai/AiInputForm.tsx +351 -0
  139. package/resources/src/components/appearance/AppearanceDrawer.tsx +268 -0
  140. package/resources/src/components/appearance/AppearanceProvider.tsx +160 -0
  141. package/resources/src/components/docs/DocArticle.tsx +106 -0
  142. package/resources/src/components/docs/DocToc.tsx +100 -0
  143. package/resources/src/components/docs/DocsChrome.tsx +259 -0
  144. package/resources/src/components/docs/DocsHome.tsx +209 -0
  145. package/resources/src/components/docs/DocsSearch.tsx +365 -0
  146. package/resources/src/components/docs/docs.css +1458 -0
  147. package/resources/src/components/feedback/AsyncButton.tsx +108 -0
  148. package/resources/src/components/feedback/LoadingOverlay.tsx +125 -0
  149. package/resources/src/components/feedback/PressableCard.tsx +92 -0
  150. package/resources/src/components/feedback/README.md +97 -0
  151. package/resources/src/components/feedback/RouteProgress.tsx +108 -0
  152. package/resources/src/components/feedback/Skeleton.tsx +249 -0
  153. package/resources/src/components/feedback/StatCard.tsx +70 -0
  154. package/resources/src/components/feedback/TaskHint.tsx +153 -0
  155. package/resources/src/components/feedback/index.ts +93 -0
  156. package/resources/src/components/feedback/motion.css +204 -0
  157. package/resources/src/components/feedback/motion.ts +63 -0
  158. package/resources/src/components/feedback/useCountUp.ts +110 -0
  159. package/resources/src/components/feedback/usePending.ts +209 -0
  160. package/resources/src/components/feedback/useRowFlash.ts +40 -0
  161. package/resources/src/components/inbox/InboxView.tsx +409 -0
  162. package/resources/src/components/ui/button.tsx +35 -0
  163. package/resources/src/components/ui/card.tsx +37 -0
  164. package/resources/src/components/ui/input.tsx +19 -0
  165. package/resources/src/lib/ai.ts +310 -0
  166. package/resources/src/lib/aiContext.ts +357 -0
  167. package/resources/src/lib/aiForm.test.ts +191 -0
  168. package/resources/src/lib/aiForm.ts +532 -0
  169. package/resources/src/lib/aiFormTypes.ts +83 -0
  170. package/resources/src/lib/aiToken.ts +72 -0
  171. package/resources/src/lib/aiTools.ts +823 -0
  172. package/resources/src/lib/api.ts +160 -0
  173. package/resources/src/lib/appInfo.ts +128 -0
  174. package/resources/src/lib/appearance.ts +643 -0
  175. package/resources/src/lib/badge-count.ts +146 -0
  176. package/resources/src/lib/badges.test.ts +58 -0
  177. package/resources/src/lib/badges.ts +79 -0
  178. package/resources/src/lib/brand-mark.test.ts +87 -0
  179. package/resources/src/lib/brand-mark.ts +45 -0
  180. package/resources/src/lib/docs-shared.test.ts +148 -0
  181. package/resources/src/lib/docs-shared.ts +422 -0
  182. package/resources/src/lib/docs.ts +381 -0
  183. package/resources/src/lib/err-text.ts +21 -0
  184. package/resources/src/lib/excel.ts +152 -0
  185. package/resources/src/lib/favorite-path.ts +60 -0
  186. package/resources/src/lib/favorites.ts +59 -0
  187. package/resources/src/lib/features.ts +69 -0
  188. package/resources/src/lib/feedback-access.ts +17 -0
  189. package/resources/src/lib/feedbackAi.ts +1621 -0
  190. package/resources/src/lib/formRules.ts +76 -0
  191. package/resources/src/lib/format.ts +52 -0
  192. package/resources/src/lib/graph-cascade.ts +304 -0
  193. package/resources/src/lib/graph-focus.ts +301 -0
  194. package/resources/src/lib/graph-link.ts +17 -0
  195. package/resources/src/lib/graph-node-write.ts +150 -0
  196. package/resources/src/lib/graph-schema.ts +475 -0
  197. package/resources/src/lib/graph-scope.test.ts +46 -0
  198. package/resources/src/lib/graph-scope.ts +28 -0
  199. package/resources/src/lib/http.ts +157 -0
  200. package/resources/src/lib/inbox-types.ts +57 -0
  201. package/resources/src/lib/inbox.ts +55 -0
  202. package/resources/src/lib/menu-service.ts +204 -0
  203. package/resources/src/lib/notifications.ts +113 -0
  204. package/resources/src/lib/office.ts +531 -0
  205. package/resources/src/lib/ontology.ts +277 -0
  206. package/resources/src/lib/openaiCompat.ts +257 -0
  207. package/resources/src/lib/pending.ts +98 -0
  208. package/resources/src/lib/prisma.ts +30 -0
  209. package/resources/src/lib/rbac.test.ts +106 -0
  210. package/resources/src/lib/rbac.ts +185 -0
  211. package/resources/src/lib/search-data.ts +158 -0
  212. package/resources/src/lib/security-questions.ts +20 -0
  213. package/resources/src/lib/skin-theme.ts +155 -0
  214. package/resources/src/lib/system-config.ts +55 -0
  215. package/resources/src/lib/theme.ts +96 -0
  216. package/resources/src/lib/todos/feedback.ts +86 -0
  217. package/resources/src/lib/todos/index.ts +73 -0
  218. package/resources/src/lib/todos/types.ts +33 -0
  219. package/resources/src/lib/upload.ts +106 -0
  220. package/resources/src/lib/utils.test.ts +9 -0
  221. package/resources/src/lib/utils.ts +6 -0
  222. package/resources/src/lib/validation.test.ts +297 -0
  223. package/resources/src/lib/validation.ts +658 -0
  224. package/resources/src/lib/visit-stats.ts +109 -0
  225. package/resources/src/lib/visits.ts +111 -0
  226. package/resources/src/lib/zodField.ts +44 -0
  227. package/resources/tsconfig.json +41 -0
  228. package/resources/vitest.config.ts +16 -0
  229. package/resources/vitest.setup.ts +1 -0
@@ -0,0 +1,532 @@
1
+ 'use client';
2
+
3
+ import React, { useEffect, useRef, useState } from 'react';
4
+ import Link from 'next/link';
5
+ import { fs, MILITARY } from '@/lib/theme';
6
+ import { headingId } from '@/lib/docs-shared';
7
+
8
+ /**
9
+ * 轻量 Markdown 渲染器(无第三方依赖)。
10
+ *
11
+ * 为什么自己写:
12
+ * 比赛环境是离线内网,装不了 react-markdown / marked;
13
+ * 而模型的回答天然是 Markdown,直接当纯文本显示会看到一堆 ** 和 | 表格。
14
+ *
15
+ * 支持:标题 / 段落 / 有序无序列表 / 代码块 / 行内代码 / 粗斜体 / 删除线 /
16
+ * 链接 / 引用 / 表格 / 分割线。
17
+ *
18
+ * 安全:全部用 React 元素拼装,**绝不使用 dangerouslySetInnerHTML**,
19
+ * 模型就算输出 <script> 也只会被当成普通文字。
20
+ */
21
+
22
+ /* ------------------------------ 块级解析 ------------------------------ */
23
+
24
+ type Block =
25
+ | { type: 'code'; lang: string; code: string }
26
+ | { type: 'table'; head: string[]; rows: string[][] }
27
+ | { type: 'list'; ordered: boolean; items: string[] }
28
+ | { type: 'quote'; lines: string[] }
29
+ | { type: 'heading'; level: number; text: string }
30
+ | { type: 'hr' }
31
+ | { type: 'para'; lines: string[] };
32
+
33
+ const LIST_RE = /^([-*+]|\d+[.)])\s+(.*)$/;
34
+ const HEADING_RE = /^(#{1,6})\s+(.*)$/;
35
+
36
+ function isTableSep(line: string): boolean {
37
+ const t = line.trim();
38
+ if (!t.includes('-') || !t.includes('|')) return false;
39
+ const cells = t.replace(/^\|/, '').replace(/\|$/, '').split('|');
40
+ return cells.length > 0 && cells.every((c) => /^:?-{2,}:?$/.test(c.trim()));
41
+ }
42
+
43
+ function splitRow(line: string): string[] {
44
+ return line
45
+ .trim()
46
+ .replace(/^\|/, '')
47
+ .replace(/\|$/, '')
48
+ .split('|')
49
+ .map((c) => c.trim());
50
+ }
51
+
52
+ function isBlockStart(line: string): boolean {
53
+ const t = line.trim();
54
+ return (
55
+ /^```/.test(t) ||
56
+ HEADING_RE.test(t) ||
57
+ /^>\s?/.test(t) ||
58
+ LIST_RE.test(t) ||
59
+ /^([-*_])\1{2,}$/.test(t.replace(/\s+/g, ''))
60
+ );
61
+ }
62
+
63
+ /** 把 Markdown 文本切成块(对未闭合的代码块也容错 —— 流式输出时会遇到) */
64
+ export function parseBlocks(src: string): Block[] {
65
+ const lines = String(src ?? '').replace(/\r\n?/g, '\n').split('\n');
66
+ const blocks: Block[] = [];
67
+ let i = 0;
68
+
69
+ while (i < lines.length) {
70
+ const raw = lines[i];
71
+ const t = raw.trim();
72
+
73
+ if (!t) {
74
+ i += 1;
75
+ continue;
76
+ }
77
+
78
+ // 代码块
79
+ const fence = t.match(/^```(\w*)/);
80
+ if (fence) {
81
+ const lang = fence[1] ?? '';
82
+ const buf: string[] = [];
83
+ i += 1;
84
+ while (i < lines.length && !lines[i].trim().startsWith('```')) {
85
+ buf.push(lines[i]);
86
+ i += 1;
87
+ }
88
+ if (i < lines.length) i += 1; // 跳过结束围栏
89
+ blocks.push({ type: 'code', lang, code: buf.join('\n') });
90
+ continue;
91
+ }
92
+
93
+ // 标题
94
+ const h = t.match(HEADING_RE);
95
+ if (h) {
96
+ blocks.push({ type: 'heading', level: h[1].length, text: h[2] });
97
+ i += 1;
98
+ continue;
99
+ }
100
+
101
+ // 分割线
102
+ if (/^([-*_])\1{2,}$/.test(t.replace(/\s+/g, ''))) {
103
+ blocks.push({ type: 'hr' });
104
+ i += 1;
105
+ continue;
106
+ }
107
+
108
+ // 表格(当前行有 |,下一行是分隔行)
109
+ if (t.includes('|') && i + 1 < lines.length && isTableSep(lines[i + 1])) {
110
+ const head = splitRow(t);
111
+ i += 2;
112
+ const rows: string[][] = [];
113
+ while (i < lines.length && lines[i].includes('|') && lines[i].trim()) {
114
+ rows.push(splitRow(lines[i]));
115
+ i += 1;
116
+ }
117
+ blocks.push({ type: 'table', head, rows });
118
+ continue;
119
+ }
120
+
121
+ // 引用
122
+ if (/^>\s?/.test(t)) {
123
+ const buf: string[] = [];
124
+ while (i < lines.length && /^>\s?/.test(lines[i].trim())) {
125
+ buf.push(lines[i].trim().replace(/^>\s?/, ''));
126
+ i += 1;
127
+ }
128
+ blocks.push({ type: 'quote', lines: buf });
129
+ continue;
130
+ }
131
+
132
+ // 列表(连续的列表项合并成一个 list;缩进的续行并到上一项)
133
+ const li = t.match(LIST_RE);
134
+ if (li) {
135
+ const ordered = /^\d/.test(li[1]);
136
+ const items: string[] = [];
137
+ while (i < lines.length) {
138
+ const cur = lines[i];
139
+ const m = cur.trim().match(LIST_RE);
140
+ if (m) {
141
+ items.push(m[2]);
142
+ i += 1;
143
+ continue;
144
+ }
145
+ if (cur.trim() && /^\s{2,}/.test(cur) && items.length) {
146
+ items[items.length - 1] += ` ${cur.trim()}`;
147
+ i += 1;
148
+ continue;
149
+ }
150
+ break;
151
+ }
152
+ blocks.push({ type: 'list', ordered, items });
153
+ continue;
154
+ }
155
+
156
+ // 段落:吃到空行或下一个块开头
157
+ const buf: string[] = [];
158
+ while (i < lines.length) {
159
+ const cur = lines[i];
160
+ const tt = cur.trim();
161
+ if (!tt || isBlockStart(cur)) break;
162
+ if (tt.includes('|') && i + 1 < lines.length && isTableSep(lines[i + 1])) break;
163
+ buf.push(tt);
164
+ i += 1;
165
+ }
166
+ if (buf.length) blocks.push({ type: 'para', lines: buf });
167
+ else i += 1; // 防御:避免死循环
168
+ }
169
+
170
+ return blocks;
171
+ }
172
+
173
+ /* ------------------------------ 行内解析 ------------------------------ */
174
+
175
+ const INLINE_RE =
176
+ /(`[^`]+`|\*\*[^*]+\*\*|__[^_]+__|~~[^~]+~~|\[[^\]]+\]\([^)\s]+\)|\*[^*\n]+\*|_[^_\n]+_|<br\s*\/?>)/gi;
177
+
178
+ /** 文档站的可选渲染能力;默认全部关闭,AI 面板等既有调用不受影响 */
179
+ type RenderOptions = {
180
+ /** 给标题生成锚点 id,并在标题前渲染可点击的 # */
181
+ anchors?: boolean;
182
+ /** 站内链接用 Next Link(不新开标签页) */
183
+ internalLinks?: boolean;
184
+ /** 代码块右上角显示复制按钮 */
185
+ copyable?: boolean;
186
+ /** 同一篇文档内标题 id 去重 */
187
+ headingIds?: Set<string>;
188
+ };
189
+
190
+ function renderInline(text: string, keyBase: string, opts: RenderOptions = {}): React.ReactNode[] {
191
+ const out: React.ReactNode[] = [];
192
+ let last = 0;
193
+ let k = 0;
194
+ let m: RegExpExecArray | null;
195
+ INLINE_RE.lastIndex = 0;
196
+
197
+ while ((m = INLINE_RE.exec(text)) !== null) {
198
+ if (m.index > last) out.push(text.slice(last, m.index));
199
+ const tok = m[0];
200
+ const key = `${keyBase}-i${k++}`;
201
+
202
+ if (tok.startsWith('`')) {
203
+ out.push(
204
+ <code
205
+ key={key}
206
+ style={{
207
+ background: MILITARY.soft,
208
+ border: `1px solid ${MILITARY.borderSoft}`,
209
+ borderRadius: 4,
210
+ padding: '1px 5px',
211
+ fontSize: '0.92em',
212
+ fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
213
+ }}
214
+ >
215
+ {tok.slice(1, -1)}
216
+ </code>
217
+ );
218
+ } else if (tok.startsWith('**') || tok.startsWith('__')) {
219
+ out.push(<strong key={key}>{tok.slice(2, -2)}</strong>);
220
+ } else if (tok.startsWith('~~')) {
221
+ out.push(<del key={key}>{tok.slice(2, -2)}</del>);
222
+ } else if (/^<br\s*\/?>$/i.test(tok)) {
223
+ // 模型常在表格单元格里用 <br> 分隔多条依据,直接当文字显示会很脏
224
+ out.push(<br key={key} />);
225
+ } else if (tok.startsWith('[')) {
226
+ const link = tok.match(/^\[([^\]]+)\]\(([^)]+)\)$/);
227
+ if (link) {
228
+ const href = link[2];
229
+ const isInternal =
230
+ href.startsWith('/') || href.startsWith('#') || href.startsWith('?');
231
+ // 文档正文里的站内链接不新开标签页,否则读两页就开出一排标签;
232
+ // 外链保持 target=_blank,AI 回答里的参考链接也沿用这个行为。
233
+ // 哈希链接仍用原生 a,交给浏览器做页内跳转。
234
+ const isRouteLink = isInternal && href.startsWith('/');
235
+ out.push(
236
+ isRouteLink && opts.internalLinks ? (
237
+ <Link key={key} href={href} style={{ color: MILITARY.primary }}>
238
+ {link[1]}
239
+ </Link>
240
+ ) : (
241
+ <a
242
+ key={key}
243
+ href={href}
244
+ target={isInternal ? undefined : '_blank'}
245
+ rel={isInternal ? undefined : 'noreferrer'}
246
+ style={{ color: MILITARY.primary }}
247
+ >
248
+ {link[1]}
249
+ </a>
250
+ )
251
+ );
252
+ } else {
253
+ out.push(tok);
254
+ }
255
+ } else {
256
+ out.push(<em key={key}>{tok.slice(1, -1)}</em>);
257
+ }
258
+ last = m.index + tok.length;
259
+ }
260
+
261
+ if (last < text.length) out.push(text.slice(last));
262
+ return out;
263
+ }
264
+
265
+ /* ------------------------------ 渲染 ------------------------------ */
266
+
267
+ const HEADING_SIZE: Record<number, number> = { 1: 18, 2: 16, 3: 15, 4: 14, 5: 13.5, 6: 13.5 };
268
+
269
+ /** 复制代码:优先 Clipboard API,HTTP 内网环境回退到 execCommand */
270
+ function copyText(text: string): Promise<void> {
271
+ if (typeof navigator !== 'undefined' && navigator.clipboard && window.isSecureContext) {
272
+ return navigator.clipboard.writeText(text);
273
+ }
274
+ return new Promise((resolve, reject) => {
275
+ try {
276
+ const ta = document.createElement('textarea');
277
+ ta.value = text;
278
+ ta.setAttribute('readonly', '');
279
+ ta.style.position = 'fixed';
280
+ ta.style.top = '-9999px';
281
+ document.body.appendChild(ta);
282
+ ta.select();
283
+ const ok = document.execCommand('copy');
284
+ document.body.removeChild(ta);
285
+ if (ok) resolve();
286
+ else reject(new Error('copy failed'));
287
+ } catch (e) {
288
+ reject(e);
289
+ }
290
+ });
291
+ }
292
+
293
+ /** 带语言标签与复制按钮的代码块(只有文档站会开启 copyable) */
294
+ function CodeBlock({ code, lang, copyable }: { code: string; lang: string; copyable?: boolean }) {
295
+ const [copied, setCopied] = useState(false);
296
+ const timer = useRef<number | null>(null);
297
+
298
+ useEffect(
299
+ () => () => {
300
+ if (timer.current) window.clearTimeout(timer.current);
301
+ },
302
+ []
303
+ );
304
+
305
+ const preStyle: React.CSSProperties = {
306
+ background: MILITARY.paper,
307
+ border: `1px solid ${MILITARY.borderSoft}`,
308
+ borderRadius: 6,
309
+ padding: '8px 10px',
310
+ margin: '6px 0',
311
+ overflowX: 'auto',
312
+ maxHeight: 320,
313
+ overflowY: 'auto',
314
+ fontSize: fs(12),
315
+ lineHeight: 1.6,
316
+ fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
317
+ whiteSpace: 'pre',
318
+ };
319
+
320
+ if (!copyable) {
321
+ return (
322
+ <pre style={preStyle}>
323
+ <code>{code}</code>
324
+ </pre>
325
+ );
326
+ }
327
+
328
+ const onCopy = async () => {
329
+ try {
330
+ await copyText(code);
331
+ setCopied(true);
332
+ if (timer.current) window.clearTimeout(timer.current);
333
+ timer.current = window.setTimeout(() => setCopied(false), 1600);
334
+ } catch {
335
+ /* 复制失败不打断阅读,用户可以手动选择代码 */
336
+ }
337
+ };
338
+
339
+ return (
340
+ <div className="wb-md-code">
341
+ {lang && <span className="wb-md-code-lang">{lang}</span>}
342
+ <pre style={{ ...preStyle, margin: 0, padding: '30px 12px 12px', maxHeight: 520 }}>
343
+ <code>{code}</code>
344
+ </pre>
345
+ <button type="button" className="wb-md-copy" onClick={() => void onCopy()}>
346
+ {copied ? '已复制' : '复制'}
347
+ </button>
348
+ </div>
349
+ );
350
+ }
351
+
352
+ function renderBlock(b: Block, key: string, opts: RenderOptions = {}): React.ReactNode {
353
+ switch (b.type) {
354
+ case 'code':
355
+ return <CodeBlock key={key} code={b.code} lang={b.lang} copyable={opts.copyable} />;
356
+
357
+ case 'heading': {
358
+ // 用真实 h 标签(antd 的 reset 会把标题字号抹平,所以样式全部显式给)
359
+ const tag = (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as const)[Math.min(b.level, 6) - 1];
360
+ const id = opts.anchors ? headingId(b.text, opts.headingIds) : undefined;
361
+ return React.createElement(
362
+ tag,
363
+ {
364
+ key,
365
+ id,
366
+ style: {
367
+ fontSize: fs(HEADING_SIZE[b.level] ?? 14),
368
+ fontWeight: 600,
369
+ // 标题左侧一条浅军绿定位条,报告里层级更清楚(比下划线更克制)
370
+ borderLeft: b.level <= 3 ? `3px solid ${MILITARY.softStrong}` : undefined,
371
+ paddingLeft: b.level <= 3 ? 8 : undefined,
372
+ margin: b.level <= 2 ? '12px 0 7px' : '8px 0 5px',
373
+ color: MILITARY.text,
374
+ lineHeight: 1.5,
375
+ // 顶部固定栏会挡住跳转目标,留出滚动余量
376
+ scrollMarginTop: opts.anchors ? 78 : undefined,
377
+ },
378
+ },
379
+ <>
380
+ {id && (
381
+ <a className="wb-docs-anchor" href={`#${id}`} aria-label="链接到本节">
382
+ #
383
+ </a>
384
+ )}
385
+ {renderInline(b.text, key, opts)}
386
+ </>
387
+ );
388
+ }
389
+
390
+ case 'hr':
391
+ return <hr key={key} style={{ border: 'none', borderTop: `1px dashed ${MILITARY.border}`, margin: '10px 0' }} />;
392
+
393
+ case 'quote':
394
+ return (
395
+ <blockquote
396
+ key={key}
397
+ style={{
398
+ margin: '8px 0',
399
+ padding: '7px 10px',
400
+ borderRadius: '0 6px 6px 0',
401
+ borderLeft: `3px solid ${MILITARY.softStrong}`,
402
+ color: MILITARY.textSecondary,
403
+ background: MILITARY.soft,
404
+ }}
405
+ >
406
+ {b.lines.map((l, idx) => (
407
+ <div key={`${key}-q${idx}`}>{renderInline(l, `${key}-q${idx}`, opts)}</div>
408
+ ))}
409
+ </blockquote>
410
+ );
411
+
412
+ case 'list': {
413
+ const items = b.items.map((it, idx) => (
414
+ <li key={`${key}-l${idx}`} style={{ margin: '3px 0' }}>
415
+ {renderInline(it, `${key}-l${idx}`, opts)}
416
+ </li>
417
+ ));
418
+ // ★ antd 的全局 reset 会把 ol/ul 的 list-style 清掉(变成纯缩进文本),
419
+ // 所以这里必须显式给 listStyleType,否则列表看不出层级。
420
+ const listStyle: React.CSSProperties = {
421
+ margin: '4px 0',
422
+ paddingLeft: 24,
423
+ listStylePosition: 'outside',
424
+ listStyleType: b.ordered ? 'decimal' : 'disc',
425
+ };
426
+ return b.ordered ? (
427
+ <ol key={key} style={listStyle}>
428
+ {items}
429
+ </ol>
430
+ ) : (
431
+ <ul key={key} style={listStyle}>
432
+ {items}
433
+ </ul>
434
+ );
435
+ }
436
+
437
+ case 'table':
438
+ return (
439
+ <div key={key} style={{ overflowX: 'auto', margin: '8px 0' }}>
440
+ <table style={{ borderCollapse: 'collapse', width: '100%', fontSize: fs(12.5) }}>
441
+ <thead>
442
+ <tr>
443
+ {b.head.map((h, idx) => (
444
+ <th
445
+ key={`${key}-h${idx}`}
446
+ style={{
447
+ border: `1px solid ${MILITARY.border}`,
448
+ background: MILITARY.soft,
449
+ padding: '5px 8px',
450
+ textAlign: 'left',
451
+ fontWeight: 600,
452
+ whiteSpace: 'nowrap',
453
+ }}
454
+ >
455
+ {renderInline(h, `${key}-h${idx}`, opts)}
456
+ </th>
457
+ ))}
458
+ </tr>
459
+ </thead>
460
+ <tbody>
461
+ {b.rows.map((row, r) => (
462
+ <tr key={`${key}-r${r}`}>
463
+ {row.map((cell, c) => (
464
+ <td
465
+ key={`${key}-r${r}c${c}`}
466
+ style={{ border: `1px solid ${MILITARY.border}`, padding: '5px 8px', verticalAlign: 'top' }}
467
+ >
468
+ {renderInline(cell, `${key}-r${r}c${c}`, opts)}
469
+ </td>
470
+ ))}
471
+ </tr>
472
+ ))}
473
+ </tbody>
474
+ </table>
475
+ </div>
476
+ );
477
+
478
+ case 'para':
479
+ default:
480
+ return (
481
+ <p key={key} style={{ margin: '4px 0' }}>
482
+ {b.lines.map((l, idx) => (
483
+ <React.Fragment key={`${key}-p${idx}`}>
484
+ {idx > 0 && <br />}
485
+ {renderInline(l, `${key}-p${idx}`, opts)}
486
+ </React.Fragment>
487
+ ))}
488
+ </p>
489
+ );
490
+ }
491
+ }
492
+
493
+ export default function Markdown({
494
+ text,
495
+ style,
496
+ anchors = false,
497
+ internalLinks = false,
498
+ copyable = false,
499
+ }: {
500
+ text: string;
501
+ style?: React.CSSProperties;
502
+ /** 文档站:标题生成 id 与锚点链接 */
503
+ anchors?: boolean;
504
+ /** 文档站:站内链接不新开标签页 */
505
+ internalLinks?: boolean;
506
+ /** 文档站:代码块提供复制按钮 */
507
+ copyable?: boolean;
508
+ }) {
509
+ if (!text) return null;
510
+ const blocks = parseBlocks(text);
511
+ // 与 extractToc 共用同一套 id 规则和去重顺序,目录才能准确跳到标题
512
+ const opts: RenderOptions = {
513
+ anchors,
514
+ internalLinks,
515
+ copyable,
516
+ headingIds: anchors ? new Set<string>() : undefined,
517
+ };
518
+ return (
519
+ <div
520
+ data-markdown="1"
521
+ style={{
522
+ fontSize: fs(13.5),
523
+ lineHeight: 1.75,
524
+ color: MILITARY.text,
525
+ wordBreak: 'break-word',
526
+ ...style,
527
+ }}
528
+ >
529
+ {blocks.map((b, i) => renderBlock(b, `b${i}`, opts))}
530
+ </div>
531
+ );
532
+ }