@wanlianyida/portal-kit 0.1.1 → 0.1.3

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @wanlianyida/portal-kit
2
2
 
3
- 开放平台官网、文档中心、控制台共用的界面。当前包含右下角问题反馈,以及正文划线评论。
3
+ 开放平台官网、文档中心、控制台共用的界面。包含 Markdown 正文预览、代码块、右下角问题反馈,以及正文划线评论。
4
4
 
5
5
  页面、语言、应用和企业由各项目传入。飞书 webhook 留在各项目的环境变量里,不写进这个包。
6
6
 
@@ -66,3 +66,134 @@ import { SelectionComment } from '@wanlianyida/portal-kit';
66
66
  ```
67
67
 
68
68
  `dev` 为 true 时,即使还没配 webhook 也会显示入口。生产环境传 false,地址不是飞书 https 域名时入口不出现。
69
+
70
+ ## Markdown 正文预览
71
+
72
+ admin 的预览和 docs 的文章正文都将 Markdown 直接传给 `MarkdownPreview`。组件统一处理正文排版、提示块、表格、代码块、HTML 消毒及标题锚点;页面标题、更新时间、目录布局和业务请求由宿主负责。
73
+
74
+ ```vue
75
+ <script setup lang="ts">
76
+ import { ref } from 'vue';
77
+ import { MarkdownPreview, type HeadingSection } from '@wanlianyida/portal-kit';
78
+
79
+ defineProps<{ markdown: string }>();
80
+ const sections = ref<HeadingSection[]>([]);
81
+
82
+ // 下面的基址来自宿主,公共包不读取宿主的环境变量。
83
+ const assetBase = import.meta.env.VITE_DOC_BASE || import.meta.env.BASE_URL;
84
+ const routeBase = import.meta.env.BASE_URL.replace(/\/+$/, '');
85
+
86
+ function resolveAssetUrl(src: string): string {
87
+ if (/^(https?:\/\/|\/\/|data:|blob:)/i.test(src)) return src;
88
+ return `${assetBase.replace(/\/+$/, '')}/${src.replace(/^\/+/, '')}`;
89
+ }
90
+
91
+ function resolveLink(href: string): string | null {
92
+ // docs 的发布产物已将内部引用重写为 /s/...;残留 doc:// 显示为失效引用。
93
+ if (/^doc:\/\//i.test(href)) return null;
94
+ return href.startsWith('/s/') ? `${routeBase}${href}` : href;
95
+ }
96
+ </script>
97
+
98
+ <template>
99
+ <MarkdownPreview
100
+ :markdown="markdown"
101
+ :resolve-asset-url="resolveAssetUrl"
102
+ :resolve-link="resolveLink"
103
+ @rendered="sections = $event"
104
+ />
105
+ </template>
106
+ ```
107
+
108
+ 样式随包入口自动加载,无需引入 Vditor 样式或依赖 docs 的全局 CSS。
109
+
110
+ ### 接口
111
+
112
+ | 属性 | 类型 | 默认行为 |
113
+ | ------------------- | ----------------------------------------------- | -------------------------------------------------------- |
114
+ | `markdown` | `string`,必传 | 正文唯一内容输入,空串显示为空 |
115
+ | `resolve-asset-url` | `(src: string) => string` | 图片地址保持原样;返回空串移除图片地址 |
116
+ | `resolve-link` | `(href: string) => string \| null \| undefined` | 普通链接保持原样,`doc://` 显示为失效引用 |
117
+ | `id-prefix` | `string` | 使用 Vue 实例的独立前缀,避免同页多实例冲突 |
118
+ | `locale` | `string` | `zh-CN`,支持 `en` / `en-US`,用于复制交互及表格区域标签 |
119
+
120
+ `rendered` 事件返回 `HeadingSection[]`(`id`、`label`、`level`)。事件在正文及代码组件挂载完成后触发,宿主可据此更新目录;空内容返回空数组,快速更新不会输出过期目录。每次内容更新只解析一次 Markdown。
121
+
122
+ H1/H2 进入目录,H3–H6 也有锚点。**docs 迁移时传 `id-prefix=""` 可沿用旧文章的锚点**;同页展示多篇正文时,应保留默认前缀或显式设置不同前缀。
123
+
124
+ 地址解析函数同步执行,不负责请求数据;宿主应提前准备好草稿节点到路由的映射。回调返回的地址仍经过协议校验及 DOMPurify 消毒。外部 HTTP(S) 链接新窗口打开并附带 `noopener noreferrer nofollow`;无法定位或协议不安全的链接显示为不可跳转文本。图片支持 HTTP(S)、相对路径、blob 及 PNG/JPEG/GIF/WebP/BMP 的 data URI。正文内联 `style`、`srcset`、脚本和事件属性会被过滤。
125
+
126
+ ### 支持的内容
127
+
128
+ - 标题、普通段落、加粗、斜体、删除线、行内代码、链接、图片和分割线。
129
+ - 有序/无序列表、嵌套列表、只读任务列表及段首全角空格缩进。
130
+ - 普通引用及 `[!NOTE]`、`[!WARNING]`、`[!CAUTION]` 提示块,对应信息、提醒、警告;自定义提示标题保留。
131
+ - 任意列数的表格,保留列对齐,滚动容器可通过键盘聚焦。
132
+ - 代码卡片、JSON 高亮和原文复制;未注册的语言回退纯文本,保留缩进与内部换行。普通换行按 Markdown 段落规则处理,强制换行使用行末两个空格或 `<br>`。
133
+
134
+ ### 两端接入
135
+
136
+ **admin**:继续使用 Vditor 编辑、上传图片和插入内部链接;进入预览时将 `editor.getValue()` 交给公共组件,替换原 `getHTML()`、代码块装饰及预览表格包装逻辑。图片解析使用 admin 的 `VITE_DOC_ASSET_BASE`。`doc://node/{id}` 由 admin 的节点路由映射解析;无法定位时返回 `null`。保留编辑器本身需要的 Vditor 样式,预览容器移除 `vditor-reset` 和旧正文样式。
137
+
138
+ **docs**:保留 `ArticlePane` 的文章头、全文复制和 `DocToc`,用公共组件替换正文渲染;可直接传入宿主已有的 `resolveAssetUrl`,并将 `/s/...` 补齐站点挂载前缀。目录使用 `rendered` 返回的数据;待引用迁移完成后,清理原 Markdown 解析、正文排版和代码块增强逻辑。`doc-table.css` 仍可能被 API 参数表使用,需检查引用后再清理。
139
+
140
+ **划线评论**:将 `SelectionComment` 包在 `MarkdownPreview` 外层即可,正文组件不依赖反馈通道。
141
+
142
+ ### 主题变量
143
+
144
+ 在组件或祖先节点上设置以下 CSS 变量,默认值独立于宿主的主题变量:
145
+
146
+ | CSS 变量 | 默认值 |
147
+ | --------------------------------- | --------------------------------------- |
148
+ | `--portal-markdown-text-color` | `#646a73` |
149
+ | `--portal-markdown-heading-color` | `#1f2329` |
150
+ | `--portal-markdown-link-color` | `#2468f2` |
151
+ | `--portal-markdown-font-size` | `15px` |
152
+ | `--portal-markdown-line-height` | `1.78` |
153
+ | `--portal-markdown-anchor-offset` | `84px` |
154
+ | `--portal-markdown-mono` | `'SFMono-Regular', Consolas, monospace` |
155
+
156
+ 公共正文样式均位于 `.portal-markdown-preview` 下,代码组件使用 `.portal-code-preview`,避免全局 reset 及普通 `pre/code/table` 选择器影响其它页面。
157
+
158
+ ### 单独使用代码块或渲染函数
159
+
160
+ `CodePreview` 可用于 API 请求、响应示例,属性为 `code`(必传)、`title`、`language` 和 `locale`。复制时优先使用 Clipboard API,失败后尝试临时文本域方式,并恢复原焦点。
161
+
162
+ ```vue
163
+ <CodePreview code='{"page": 1}' language="json" title="请求示例" />
164
+ ```
165
+
166
+ `MarkdownPreview` 提供 `code` 作用域插槽,宿主可以只接管特定语言的代码块,其它语言仍回退到公共 `CodePreview`。admin 和 docs 可据此接入已有的 mermaid、markmap 图表组件,不需要把图表依赖放进公共包:
167
+
168
+ ```vue
169
+ <MarkdownPreview :markdown="markdown">
170
+ <template #code="{ code, language }">
171
+ <ChartPreview
172
+ v-if="hasRichMediaRenderer(language)"
173
+ :code="code"
174
+ :language="language"
175
+ />
176
+ <CodePreview v-else :code="code" :language="language" />
177
+ </template>
178
+ </MarkdownPreview>
179
+ ```
180
+
181
+ 需要自行管理 HTML 容器时,可使用 `renderMarkdown(markdown, options)`,返回 `{ html, sections }`。`options` 支持与组件相同的地址解析和 `idPrefix` 选项,但渲染函数默认不加锚点前缀;HTML 已消毒,表格滚动与代码卡片增强由 `MarkdownPreview` 完成。
182
+
183
+ 预览组件和渲染函数面向浏览器 DOM,SSR 场景应在客户端挂载后使用。解析器与消毒器按调用隔离,不修改宿主的 marked、highlight.js 或 DOMPurify 全局实例。
184
+
185
+ ## 开发与验证
186
+
187
+ ```bash
188
+ pnpm test
189
+ pnpm build
190
+ npm pack --dry-run
191
+ ```
192
+
193
+ 开发栈沿用 Vue 3 Composition API、TypeScript strict、Vite 库模式及类型声明生成。`vue` 和 `element-plus` 由宿主提供;marked、DOMPurify 和 highlight.js 随库构建,JSON 按需注册,无需额外加载 CDN。
194
+
195
+ `pnpm dev` 后进入「正文预览」,可编辑 Markdown、切换窄屏和交互语言,检查代码复制、表格、提示块及划线评论组合。该示例未配置 webhook,评论只用于本地体验,不会发往反馈通道。
196
+
197
+ 消毒与正文 DOM 测试使用 jsdom:happy-dom 对当前 DOMPurify 的 DOM 遍历模拟不完整,不适合验证消毒结果。其它既有组件测试继续使用 happy-dom。
198
+
199
+ 发布前使用 `pnpm pack` 生成 tarball,在宿主项目安装该产物并检查类型、样式及运行效果;正式发布沿用 `prepublishOnly` 的测试与构建检查。
package/dist/index.d.ts CHANGED
@@ -16,6 +16,16 @@ merchant: string;
16
16
  root: HTMLDivElement;
17
17
  }, HTMLDivElement>;
18
18
 
19
+ declare const __VLS_component_2: DefineComponent<MarkdownPreviewProps, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {
20
+ rendered: (sections: HeadingSection[]) => any;
21
+ }, string, PublicProps, Readonly<MarkdownPreviewProps> & Readonly<{
22
+ onRendered?: ((sections: HeadingSection[]) => any) | undefined;
23
+ }>, {
24
+ locale: string;
25
+ }, {}, {}, {}, string, ComponentProvideOptions, false, {
26
+ contentRef: HTMLDivElement;
27
+ }, HTMLDivElement>;
28
+
19
29
  declare function __VLS_template(): {
20
30
  attrs: Partial<{}>;
21
31
  slots: {
@@ -27,16 +37,61 @@ declare function __VLS_template(): {
27
37
  rootEl: HTMLDivElement;
28
38
  };
29
39
 
40
+ declare function __VLS_template_2(): {
41
+ attrs: Partial<{}>;
42
+ slots: Readonly<{
43
+ /** 可选的代码块渲染插槽;宿主可在这里接入 mermaid/markmap 等专用组件。 */
44
+ code?: (props: {
45
+ code: string;
46
+ language: string;
47
+ locale: string;
48
+ }) => unknown;
49
+ }> & {
50
+ /** 可选的代码块渲染插槽;宿主可在这里接入 mermaid/markmap 等专用组件。 */
51
+ code?: (props: {
52
+ code: string;
53
+ language: string;
54
+ locale: string;
55
+ }) => unknown;
56
+ };
57
+ refs: {
58
+ contentRef: HTMLDivElement;
59
+ };
60
+ rootEl: HTMLDivElement;
61
+ };
62
+
30
63
  declare type __VLS_TemplateResult = ReturnType<typeof __VLS_template>;
31
64
 
65
+ declare type __VLS_TemplateResult_2 = ReturnType<typeof __VLS_template_2>;
66
+
32
67
  declare type __VLS_WithTemplateSlots<T, S> = T & {
33
68
  new (): {
34
69
  $slots: S;
35
70
  };
36
71
  };
37
72
 
73
+ declare type __VLS_WithTemplateSlots_2<T, S> = T & {
74
+ new (): {
75
+ $slots: S;
76
+ };
77
+ };
78
+
38
79
  export declare function buildFeedbackPayload(input: FeedbackInput, now?: Date): FeedbackPayload;
39
80
 
81
+ export declare const CodePreview: DefineComponent<CodePreviewProps, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<CodePreviewProps> & Readonly<{}>, {
82
+ title: string;
83
+ locale: string;
84
+ language: string;
85
+ }, {}, {}, {}, string, ComponentProvideOptions, false, {}, HTMLDivElement>;
86
+
87
+ export declare interface CodePreviewProps {
88
+ code: string;
89
+ title?: string;
90
+ /** 首期注册 JSON 高亮;其它语言保留原文展示。 */
91
+ language?: string;
92
+ locale?: string;
93
+ }
94
+
40
95
  /** 未传入地址时写入的飞书多维表格。各项目仍可用 webhookUrl 覆盖。 */
41
96
  export declare const DEFAULT_FEEDBACK_WEBHOOK = "https://bytedance.larkoffice.com/base/workflow/webhook/event/D9h2a3gAkwESDohg6xKcqhq7nZe";
42
97
 
@@ -128,9 +183,39 @@ export declare interface FeedbackWidgetProps {
128
183
 
129
184
  export declare function formatFeedbackTime(date: Date): string;
130
185
 
186
+ export declare interface HeadingSection {
187
+ id: string;
188
+ label: string;
189
+ level: number;
190
+ }
191
+
131
192
  /** 只接受飞书 / Lark 的 https 地址,避免把企业名和页面路径发到任意域名。 */
132
193
  export declare function isFeishuWebhook(url: string): boolean;
133
194
 
195
+ export declare const MarkdownPreview: __VLS_WithTemplateSlots_2<typeof __VLS_component_2, __VLS_TemplateResult_2["slots"]>;
196
+
197
+ export declare interface MarkdownPreviewProps extends MarkdownRenderOptions {
198
+ markdown: string;
199
+ /** `zh-CN` 或 `en` / `en-US`;其它值按中文显示。 */
200
+ locale?: string;
201
+ }
202
+
203
+ export declare interface MarkdownRenderOptions {
204
+ /** 相对资源 key 的解析方式;返回的地址仍会经过安全校验。 */
205
+ resolveAssetUrl?: (src: string) => string;
206
+ /** 同步解析链接;返回 null/undefined 表示无法定位,显示为失效引用。 */
207
+ resolveLink?: (href: string) => string | null | undefined;
208
+ /** 标题 id 前缀。渲染函数默认不加前缀,组件默认使用独立实例前缀。 */
209
+ idPrefix?: string;
210
+ }
211
+
212
+ export declare interface MarkdownRenderResult {
213
+ /** 已消毒的 HTML;正文组件会继续增强代码块和表格。 */
214
+ html: string;
215
+ /** H1/H2 的目录数据;H3–H6 也生成锚点,但不进入目录。 */
216
+ sections: HeadingSection[];
217
+ }
218
+
134
219
  /**
135
220
  * text/plain 属于浏览器的简单请求,不会先发 OPTIONS。
136
221
  * 飞书不回 Access-Control-Allow-Origin,带 application/json 会被预检拦住。
@@ -138,6 +223,9 @@ export declare function isFeishuWebhook(url: string): boolean;
138
223
  */
139
224
  export declare function postFeedback(input: FeedbackInput, webhookUrl: string, now?: Date): Promise<void>;
140
225
 
226
+ /** 浏览器端同步渲染。每次使用独立解析器和消毒器,不修改宿主的全局实例。 */
227
+ export declare function renderMarkdown(markdown: string, options?: MarkdownRenderOptions): MarkdownRenderResult;
228
+
141
229
  export declare const SelectionComment: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_TemplateResult["slots"]>;
142
230
 
143
231
  export declare interface SelectionCommentProps {