tiptap-vue-kit-markdown 0.0.0-stage → 0.1.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/README.md CHANGED
@@ -1,3 +1,148 @@
1
- # Temporary Holding Version
1
+ # tiptap-vue-kit-markdown
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 把 **Markdown** 转成与 [tiptap-vue-kit](https://www.npmjs.com/package/tiptap-vue-kit) 兼容的 **HTML**(含 GFM 表格、任务列表、可编辑 LaTeX 公式节点)。
4
+
5
+ - **不依赖** Vue / TipTap 运行时,可单独安装使用
6
+ - 公式在调用 `marked` **之前**抽出,避免 `>` `<` 被转成 `&gt;` `&lt;` 导致 KaTeX 渲染失败
7
+ - 输出节点约定与编辑器、docx、render 包一致
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ npm install tiptap-vue-kit-markdown
13
+ ```
14
+
15
+ ## 快速开始
16
+
17
+ ```ts
18
+ import { markdownToHtml } from 'tiptap-vue-kit-markdown'
19
+
20
+ const md = `
21
+ # 标题
22
+
23
+ 行内公式 $E=mc^2$,以及:
24
+
25
+ $$
26
+ \\sum_{n=1}^{\\infty} \\frac{1}{n^2}
27
+ $$
28
+
29
+ 也支持 \\(a=b>0\\) 与 \\[x^2+y^2=z^2\\]。
30
+ `
31
+
32
+ const html = markdownToHtml(md)
33
+ // 得到含 <span/div data-type="*-math" data-latex="..."> 的 HTML
34
+ ```
35
+
36
+ ## 与编辑器集成(推荐)
37
+
38
+ 编辑器已依赖本包,工具栏「插入 Markdown」内部会调用:
39
+
40
+ ```ts
41
+ markdownToHtml(markdown, { decorateImages: true })
42
+ ```
43
+
44
+ 你也可以在业务里手动转换后插入:
45
+
46
+ ```vue
47
+ <script setup lang="ts">
48
+ import { ref } from 'vue'
49
+ import { RichEditor, defineKit, markdownToHtml } from 'tiptap-vue-kit'
50
+
51
+ const html = ref('<p></p>')
52
+ const kit = defineKit({ placeholder: '输入内容…' })
53
+ const editorRef = ref<{ editor?: { chain: Function } } | null>(null)
54
+
55
+ function insertFromMarkdown(md: string) {
56
+ const content = markdownToHtml(md, { decorateImages: true })
57
+ editorRef.value?.editor?.chain().focus().insertContent(content).run()
58
+ }
59
+ </script>
60
+
61
+ <template>
62
+ <RichEditor ref="editorRef" v-model="html" :kit="kit" />
63
+ <button type="button" @click="insertFromMarkdown('你好 $a>0$')">插入示例</button>
64
+ </template>
65
+ ```
66
+
67
+ 仅使用本包、不装编辑器时:
68
+
69
+ ```ts
70
+ import { markdownToHtml } from 'tiptap-vue-kit-markdown'
71
+
72
+ const html = markdownToHtml(await file.text())
73
+ // 交给自有渲染器、存库、或再交给 tiptap-vue-kit-render
74
+ ```
75
+
76
+ ## API
77
+
78
+ ### `markdownToHtml(markdown, options?)`
79
+
80
+ | 参数 | 类型 | 说明 |
81
+ |------|------|------|
82
+ | `markdown` | `string` | Markdown 源文本 |
83
+ | `options.decorateImages` | `boolean` | 默认 `false`。为 `true` 时给无 class 的 `<img>` 加上编辑器媒体类名 `tvk-media tvk-media-block tvk-media-left` |
84
+
85
+ 返回:`string`(HTML)。
86
+
87
+ ### 其它导出
88
+
89
+ | 导出 | 说明 |
90
+ |------|------|
91
+ | `normalizeMathInHtml(html)` | 在已有 HTML 中识别 `$...$` / `\(...\)` 等并转为公式节点 |
92
+ | `toMathHtml(latex, block)` | 直接生成单个公式节点 HTML |
93
+ | `MarkdownToHtmlOptions` | 选项类型 |
94
+
95
+ ## 公式分隔符
96
+
97
+ | 写法 | 结果 |
98
+ |------|------|
99
+ | `$...$` | 行内公式 |
100
+ | `$$...$$` | 块级公式 |
101
+ | `\(...\)` 或 `\\(...\\)` | 行内公式 |
102
+ | `\[...\]` 或 `\\[...\\]` | 块级公式 |
103
+
104
+ 输出示例:
105
+
106
+ ```html
107
+ <span data-type="inline-math" data-latex="E=mc^2"></span>
108
+ <div data-type="block-math" data-latex="\sum_n n"></div>
109
+ ```
110
+
111
+ 说明:
112
+
113
+ - 代码块 / 行内代码里的 `$`、`\(` **不会**被当成公式
114
+ - 若上游曾把 `>` 转成 `&gt;`,本包会在写入 `data-latex` 前解码,保证 `a=b>0` 能正常渲染
115
+
116
+ ## 还支持什么
117
+
118
+ - GFM 表格
119
+ - 任务列表 → TipTap 的 `taskList` / `taskItem`(`data-type` / `data-checked`)
120
+ - 标题、列表、引用、链接、加粗/斜体、围栏代码块
121
+
122
+ ## 和 render 包联调
123
+
124
+ ```ts
125
+ import { markdownToHtml } from 'tiptap-vue-kit-markdown'
126
+ import { toUniHtml } from 'tiptap-vue-kit-render'
127
+
128
+ const html = markdownToHtml(md)
129
+ // 若只有 HTML,可先入编辑器再 getJSON();或按需 htmlToJson
130
+ const { html: preview, warnings } = toUniHtml(html, { platform: 'h5' })
131
+ ```
132
+
133
+ 更稳妥的链路仍是:**Markdown → 编辑器 → `getJSON()` → render**。
134
+
135
+ ## 开发与发布
136
+
137
+ 本包位于 monorepo 的 `packages/tiptap-vue-kit-markdown`。
138
+
139
+ ```bash
140
+ # 仓库根目录
141
+ npm install
142
+ npm run build -w tiptap-vue-kit-markdown
143
+ npm publish -w tiptap-vue-kit-markdown --access public
144
+ ```
145
+
146
+ ## 许可证
147
+
148
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("marked"),g=/[\u200b\u200c\u200d\ufeff]/g;function f(r){return r.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;")}function m(r){return r.replace(/&nbsp;/gi," ").replace(/&gt;/gi,">").replace(/&lt;/gi,"<").replace(/&quot;/gi,'"').replace(/&#39;/g,"'").replace(/&apos;/gi,"'").replace(/&#(\d+);/g,(t,e)=>String.fromCharCode(Number(e))).replace(/&#x([0-9a-f]+);/gi,(t,e)=>String.fromCharCode(parseInt(e,16))).replace(/&amp;/gi,"&")}function u(r){let t=m(r.replace(g,"")).trim();for(let e=0;e<3&&/\\\\[a-zA-Z([\]\\]/.test(t);e+=1)t=t.replace(/\\\\/g,"\\");return t}function c(r,t){const e=f(u(r));return t?`<div data-type="block-math" data-latex="${e}"></div>`:`<span data-type="inline-math" data-latex="${e}"></span>`}function d(r){if(!r)return r;let t=r.replace(g,"");const e=(n,a)=>{t=t.replace(n,(l,i)=>{const s=u(i??"");return s?c(s,a):l})};return e(/(?:\\\\|\\)\[([\s\S]*?)(?:\\\\|\\)\]/g,!0),e(/\$\$([\s\S]+?)\$\$/g,!0),e(/(?:\\\\|\\)\(([\s\S]*?)(?:\\\\|\\)\)/g,!1),e(new RegExp("(?<!\\$)\\$(?!\\$)([^$\\n]+?)\\$(?!\\$)","g"),!1),t}p.marked.use({gfm:!0,breaks:!1});function o(r,t,e){return r.replace(t,n=>{const a=`\0TVK${e.length}\0`;return e.push(n),a})}function $(r,t){return r.replace(/\u0000TVK(\d+)\u0000/g,(e,n)=>t[Number(n)]??"")}function k(r){const t=[];let e=o(r,/```[\s\S]*?```/g,t);return e=o(e,/`[^`\n]+`/g,t),e=e.replace(/\$\$([\s\S]+?)\$\$/g,(n,a)=>c(String(a||""),!0)),e=e.replace(/(?:\\\\|\\)\[([\s\S]*?)(?:\\\\|\\)\]/g,(n,a)=>c(String(a||""),!0)),e=e.replace(new RegExp("(?<!\\$)\\$(?!\\$)([^$\\n]+?)\\$(?!\\$)","g"),(n,a)=>c(String(a||""),!1)),e=e.replace(/(?:\\\\|\\)\(([\s\S]*?)(?:\\\\|\\)\)/g,(n,a)=>c(String(a||""),!1)),$(e,t)}function h(r){return r.replace(/<ul(?:\s[^>]*)?>([\s\S]*?)<\/ul>/gi,(t,e)=>/<input\b[^>]*type=["']?checkbox/i.test(e)?`<ul data-type="taskList">${e.replace(/<li(?:\s[^>]*)?>([\s\S]*?)<\/li>/gi,(a,l)=>{const i=/\bchecked\b/i.test(l)?"true":"false",s=l.replace(/<input\b[^>]*>/gi,"").trim();return`<li data-type="taskItem" data-checked="${i}"><p>${s}</p></li>`})}</ul>`:t)}function S(r){return r.replace(/<img\b([^>]*?)\/?>/gi,(t,e)=>/\bclass\s*=/.test(e)?t:`<img class="tvk-media tvk-media-block tvk-media-left"${e}>`)}function b(r,t={}){const e=t.decorateImages===!0,n=k(String(r||"").replace(/\r\n/g,`
2
+ `)),a=p.marked.parse(n,{async:!1});let l=typeof a=="string"?a:"";return l=h(l),e&&(l=S(l)),l=d(l),l.replace(/<p>\s*(<div data-type="block-math"[^>]*>\s*<\/div>)\s*<\/p>/gi,"$1")}exports.decodeHtmlEntities=m;exports.markdownToHtml=b;exports.normalizeMathInHtml=d;exports.toMathHtml=c;exports.unescapeLatexBody=u;
@@ -0,0 +1,4 @@
1
+ export { markdownToHtml } from './markdownToHtml';
2
+ export { decodeHtmlEntities, normalizeMathInHtml, toMathHtml, unescapeLatexBody, } from './normalizeLatex';
3
+ export type { MarkdownToHtmlOptions } from './types';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,UAAU,EACV,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,76 @@
1
+ import { marked as p } from "marked";
2
+ const o = /[\u200b\u200c\u200d\ufeff]/g;
3
+ function m(r) {
4
+ return r.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
5
+ }
6
+ function f(r) {
7
+ return r.replace(/&nbsp;/gi, " ").replace(/&gt;/gi, ">").replace(/&lt;/gi, "<").replace(/&quot;/gi, '"').replace(/&#39;/g, "'").replace(/&apos;/gi, "'").replace(/&#(\d+);/g, (t, e) => String.fromCharCode(Number(e))).replace(/&#x([0-9a-f]+);/gi, (t, e) => String.fromCharCode(parseInt(e, 16))).replace(/&amp;/gi, "&");
8
+ }
9
+ function g(r) {
10
+ let t = f(r.replace(o, "")).trim();
11
+ for (let e = 0; e < 3 && /\\\\[a-zA-Z([\]\\]/.test(t); e += 1)
12
+ t = t.replace(/\\\\/g, "\\");
13
+ return t;
14
+ }
15
+ function l(r, t) {
16
+ const e = m(g(r));
17
+ return t ? `<div data-type="block-math" data-latex="${e}"></div>` : `<span data-type="inline-math" data-latex="${e}"></span>`;
18
+ }
19
+ function d(r) {
20
+ if (!r)
21
+ return r;
22
+ let t = r.replace(o, "");
23
+ const e = (n, a) => {
24
+ t = t.replace(n, (c, i) => {
25
+ const s = g(i ?? "");
26
+ return s ? l(s, a) : c;
27
+ });
28
+ };
29
+ return e(/(?:\\\\|\\)\[([\s\S]*?)(?:\\\\|\\)\]/g, !0), e(/\$\$([\s\S]+?)\$\$/g, !0), e(/(?:\\\\|\\)\(([\s\S]*?)(?:\\\\|\\)\)/g, !1), e(new RegExp("(?<!\\$)\\$(?!\\$)([^$\\n]+?)\\$(?!\\$)", "g"), !1), t;
30
+ }
31
+ p.use({ gfm: !0, breaks: !1 });
32
+ function u(r, t, e) {
33
+ return r.replace(t, (n) => {
34
+ const a = `\0TVK${e.length}\0`;
35
+ return e.push(n), a;
36
+ });
37
+ }
38
+ function $(r, t) {
39
+ return r.replace(/\u0000TVK(\d+)\u0000/g, (e, n) => t[Number(n)] ?? "");
40
+ }
41
+ function h(r) {
42
+ const t = [];
43
+ let e = u(r, /```[\s\S]*?```/g, t);
44
+ return e = u(e, /`[^`\n]+`/g, t), e = e.replace(/\$\$([\s\S]+?)\$\$/g, (n, a) => l(String(a || ""), !0)), e = e.replace(
45
+ /(?:\\\\|\\)\[([\s\S]*?)(?:\\\\|\\)\]/g,
46
+ (n, a) => l(String(a || ""), !0)
47
+ ), e = e.replace(
48
+ new RegExp("(?<!\\$)\\$(?!\\$)([^$\\n]+?)\\$(?!\\$)", "g"),
49
+ (n, a) => l(String(a || ""), !1)
50
+ ), e = e.replace(
51
+ /(?:\\\\|\\)\(([\s\S]*?)(?:\\\\|\\)\)/g,
52
+ (n, a) => l(String(a || ""), !1)
53
+ ), $(e, t);
54
+ }
55
+ function k(r) {
56
+ return r.replace(/<ul(?:\s[^>]*)?>([\s\S]*?)<\/ul>/gi, (t, e) => /<input\b[^>]*type=["']?checkbox/i.test(e) ? `<ul data-type="taskList">${e.replace(/<li(?:\s[^>]*)?>([\s\S]*?)<\/li>/gi, (a, c) => {
57
+ const i = /\bchecked\b/i.test(c) ? "true" : "false", s = c.replace(/<input\b[^>]*>/gi, "").trim();
58
+ return `<li data-type="taskItem" data-checked="${i}"><p>${s}</p></li>`;
59
+ })}</ul>` : t);
60
+ }
61
+ function S(r) {
62
+ return r.replace(/<img\b([^>]*?)\/?>/gi, (t, e) => /\bclass\s*=/.test(e) ? t : `<img class="tvk-media tvk-media-block tvk-media-left"${e}>`);
63
+ }
64
+ function x(r, t = {}) {
65
+ const e = t.decorateImages === !0, n = h(String(r || "").replace(/\r\n/g, `
66
+ `)), a = p.parse(n, { async: !1 });
67
+ let c = typeof a == "string" ? a : "";
68
+ return c = k(c), e && (c = S(c)), c = d(c), c.replace(/<p>\s*(<div data-type="block-math"[^>]*>\s*<\/div>)\s*<\/p>/gi, "$1");
69
+ }
70
+ export {
71
+ f as decodeHtmlEntities,
72
+ x as markdownToHtml,
73
+ d as normalizeMathInHtml,
74
+ l as toMathHtml,
75
+ g as unescapeLatexBody
76
+ };
@@ -0,0 +1,7 @@
1
+ import { MarkdownToHtmlOptions } from './types';
2
+ /**
3
+ * Markdown → HTML with TipTap-compatible math nodes
4
+ * (`span/div[data-type=*-math][data-latex=...]`), GFM tables, and task lists.
5
+ */
6
+ export declare function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions): string;
7
+ //# sourceMappingURL=markdownToHtml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdownToHtml.d.ts","sourceRoot":"","sources":["../src/markdownToHtml.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAyDrD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,UAYnF"}
@@ -0,0 +1,6 @@
1
+ /** Undo HTML entity encoding introduced by markdown/HTML parsers (e.g. `>` → `&gt;`). */
2
+ export declare function decodeHtmlEntities(value: string): string;
3
+ export declare function unescapeLatexBody(latex: string): string;
4
+ export declare function toMathHtml(latex: string, block: boolean): string;
5
+ export declare function normalizeMathInHtml(input: string): string;
6
+ //# sourceMappingURL=normalizeLatex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalizeLatex.d.ts","sourceRoot":"","sources":["../src/normalizeLatex.ts"],"names":[],"mappings":"AAMA,yFAAyF;AACzF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,UAW/C;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,UAM9C;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,UAKvD;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,UAoBhD"}
@@ -0,0 +1,9 @@
1
+ export interface MarkdownToHtmlOptions {
2
+ /**
3
+ * Add tiptap-vue-kit media classes on bare `<img>` tags.
4
+ * Default `false` so standalone consumers are not tied to `tvk-*`.
5
+ * The editor kit enables this when inserting Markdown.
6
+ */
7
+ decorateImages?: boolean;
8
+ }
9
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B"}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "tiptap-vue-kit-markdown",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
4
+ "description": "将 Markdown 转为带可编辑 LaTeX 公式节点的 HTML,供 tiptap-vue-kit 使用",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "superlu",
8
+ "homepage": "https://www.npmjs.com/package/tiptap-vue-kit-markdown",
9
+ "bugs": {
10
+ "url": "https://www.npmjs.com/package/tiptap-vue-kit-markdown"
11
+ },
12
+ "keywords": [
13
+ "markdown",
14
+ "marked",
15
+ "gfm",
16
+ "latex",
17
+ "tiptap",
18
+ "math"
19
+ ],
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "main": "./dist/index.cjs",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "require": "./dist/index.cjs"
32
+ }
33
+ },
34
+ "scripts": {
35
+ "build": "vite build",
36
+ "typecheck": "tsc --noEmit -p tsconfig.json",
37
+ "prepublishOnly": "npm run build"
38
+ },
39
+ "dependencies": {
40
+ "marked": "^18.1.1"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^26.6.5",
44
+ "typescript": "~5.7.3",
45
+ "vite": "^6.3.5",
46
+ "vite-plugin-dts": "^4.5.4"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public",
50
+ "registry": "https://registry.npmjs.org/"
51
+ }
52
+ }