@easy-cms/richtext 0.1.0
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/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +120 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @easy-cms/richtext
|
|
2
|
+
|
|
3
|
+
Renders [Easy CMS](https://github.com/maritonx/easy-crm) rich text (Tiptap JSON) to safe HTML.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { renderRichText, richTextToPlainText } from '@easy-cms/richtext'
|
|
7
|
+
|
|
8
|
+
const html = renderRichText(post.body) // escaped, unsafe URLs removed
|
|
9
|
+
const text = richTextToPlainText(post.body)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Supports headings, paragraphs, bold, italic, underline, strike, code, links, lists, quotes, code
|
|
13
|
+
blocks, hard breaks, rules and images. Pass `{ nodes: { image: (node, children) => '…' } }` to
|
|
14
|
+
customize a node. No dependencies; works in Node and browsers.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** A Tiptap / ProseMirror JSON node. */
|
|
2
|
+
interface RichTextNode {
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly attrs?: Readonly<Record<string, unknown>>;
|
|
5
|
+
readonly content?: readonly RichTextNode[];
|
|
6
|
+
readonly text?: string;
|
|
7
|
+
readonly marks?: readonly {
|
|
8
|
+
readonly type: string;
|
|
9
|
+
readonly attrs?: Readonly<Record<string, unknown>>;
|
|
10
|
+
}[];
|
|
11
|
+
}
|
|
12
|
+
/** What rich text fields hold: `@easy-cms/core`'s `RichTextDocument` fits. */
|
|
13
|
+
type RichTextInput = RichTextNode | {
|
|
14
|
+
readonly type: string;
|
|
15
|
+
readonly content?: readonly unknown[];
|
|
16
|
+
};
|
|
17
|
+
interface RenderOptions {
|
|
18
|
+
/** Override or add node renderers. Receives the node and its rendered children. */
|
|
19
|
+
nodes?: Record<string, (node: RichTextNode, children: string) => string>;
|
|
20
|
+
/** Attributes added to every link. Default adds `rel="noopener noreferrer"` to external links. */
|
|
21
|
+
linkAttributes?: (href: string) => Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
/** Escapes text for use in HTML content and quoted attributes. */
|
|
24
|
+
declare function escapeHtml(text: string): string;
|
|
25
|
+
/** Allows http(s), mailto, tel, relative URLs and fragments. Everything else (javascript:, data:, …) is dropped. */
|
|
26
|
+
declare function safeUrl(value: unknown): string | undefined;
|
|
27
|
+
/** Renders a rich text document to HTML. Text is escaped and unsafe URLs are removed. */
|
|
28
|
+
declare function renderRichText(doc: RichTextInput | null | undefined, options?: RenderOptions): string;
|
|
29
|
+
/** Plain text of a document, e.g. for excerpts or search. Blocks are separated by newlines. */
|
|
30
|
+
declare function richTextToPlainText(doc: RichTextInput | null | undefined): string;
|
|
31
|
+
|
|
32
|
+
export { type RenderOptions, type RichTextInput, type RichTextNode, escapeHtml, renderRichText, richTextToPlainText, safeUrl };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var ESCAPES = {
|
|
3
|
+
"&": "&",
|
|
4
|
+
"<": "<",
|
|
5
|
+
">": ">",
|
|
6
|
+
'"': """,
|
|
7
|
+
"'": "'"
|
|
8
|
+
};
|
|
9
|
+
function escapeHtml(text) {
|
|
10
|
+
return text.replace(/[&<>"']/g, (c) => ESCAPES[c]);
|
|
11
|
+
}
|
|
12
|
+
function safeUrl(value) {
|
|
13
|
+
if (typeof value !== "string") return void 0;
|
|
14
|
+
const url = value.trim();
|
|
15
|
+
const compact = url.replace(/[\u0000-\u001F\u007F\s]/g, "").toLowerCase();
|
|
16
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/.exec(compact)?.[1];
|
|
17
|
+
if (scheme && !["http", "https", "mailto", "tel"].includes(scheme)) return void 0;
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
function attributes(attrs) {
|
|
21
|
+
return Object.entries(attrs).filter(([, v]) => v !== void 0).map(([k, v]) => ` ${k}="${escapeHtml(v)}"`).join("");
|
|
22
|
+
}
|
|
23
|
+
var defaultLinkAttributes = (href) => /^https?:\/\//i.test(href) ? { rel: "noopener noreferrer" } : {};
|
|
24
|
+
function renderMarks(text, marks, options) {
|
|
25
|
+
let html = escapeHtml(text);
|
|
26
|
+
for (const mark of marks ?? []) {
|
|
27
|
+
switch (mark.type) {
|
|
28
|
+
case "bold":
|
|
29
|
+
html = `<strong>${html}</strong>`;
|
|
30
|
+
break;
|
|
31
|
+
case "italic":
|
|
32
|
+
html = `<em>${html}</em>`;
|
|
33
|
+
break;
|
|
34
|
+
case "underline":
|
|
35
|
+
html = `<u>${html}</u>`;
|
|
36
|
+
break;
|
|
37
|
+
case "strike":
|
|
38
|
+
html = `<s>${html}</s>`;
|
|
39
|
+
break;
|
|
40
|
+
case "code":
|
|
41
|
+
html = `<code>${html}</code>`;
|
|
42
|
+
break;
|
|
43
|
+
case "link": {
|
|
44
|
+
const href = safeUrl(mark.attrs?.href);
|
|
45
|
+
if (!href) break;
|
|
46
|
+
const extra = (options.linkAttributes ?? defaultLinkAttributes)(href);
|
|
47
|
+
const target = mark.attrs?.target === "_blank" ? "_blank" : void 0;
|
|
48
|
+
html = `<a${attributes({ href, target, ...extra })}>${html}</a>`;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return html;
|
|
54
|
+
}
|
|
55
|
+
function renderNode(node, options) {
|
|
56
|
+
if (!node || typeof node !== "object") return "";
|
|
57
|
+
if (node.type === "text") return renderMarks(String(node.text ?? ""), node.marks, options);
|
|
58
|
+
const children = (Array.isArray(node.content) ? node.content : []).map((c) => renderNode(c, options)).join("");
|
|
59
|
+
const custom = options.nodes?.[node.type];
|
|
60
|
+
if (custom) return custom(node, children);
|
|
61
|
+
switch (node.type) {
|
|
62
|
+
case "doc":
|
|
63
|
+
return children;
|
|
64
|
+
case "paragraph":
|
|
65
|
+
return `<p>${children}</p>`;
|
|
66
|
+
case "heading": {
|
|
67
|
+
const level = Math.min(6, Math.max(1, Number(node.attrs?.level) || 2));
|
|
68
|
+
return `<h${level}>${children}</h${level}>`;
|
|
69
|
+
}
|
|
70
|
+
case "bulletList":
|
|
71
|
+
return `<ul>${children}</ul>`;
|
|
72
|
+
case "orderedList": {
|
|
73
|
+
const start = Number(node.attrs?.start);
|
|
74
|
+
return `<ol${Number.isInteger(start) && start !== 1 ? ` start="${start}"` : ""}>${children}</ol>`;
|
|
75
|
+
}
|
|
76
|
+
case "listItem":
|
|
77
|
+
return `<li>${children}</li>`;
|
|
78
|
+
case "blockquote":
|
|
79
|
+
return `<blockquote>${children}</blockquote>`;
|
|
80
|
+
case "codeBlock": {
|
|
81
|
+
const language = typeof node.attrs?.language === "string" ? node.attrs.language : void 0;
|
|
82
|
+
return `<pre><code${attributes({ class: language ? `language-${language}` : void 0 })}>${children}</code></pre>`;
|
|
83
|
+
}
|
|
84
|
+
case "hardBreak":
|
|
85
|
+
return "<br>";
|
|
86
|
+
case "horizontalRule":
|
|
87
|
+
return "<hr>";
|
|
88
|
+
case "image": {
|
|
89
|
+
const src = safeUrl(node.attrs?.src);
|
|
90
|
+
if (!src) return "";
|
|
91
|
+
const alt = typeof node.attrs?.alt === "string" ? node.attrs.alt : "";
|
|
92
|
+
const title = typeof node.attrs?.title === "string" ? node.attrs.title : void 0;
|
|
93
|
+
return `<img${attributes({ src, alt, title })}>`;
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return children;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function renderRichText(doc, options = {}) {
|
|
100
|
+
if (!doc) return "";
|
|
101
|
+
return renderNode(doc, options);
|
|
102
|
+
}
|
|
103
|
+
function richTextToPlainText(doc) {
|
|
104
|
+
if (!doc) return "";
|
|
105
|
+
const blocks = /* @__PURE__ */ new Set(["paragraph", "heading", "listItem", "blockquote", "codeBlock"]);
|
|
106
|
+
const walk = (node) => {
|
|
107
|
+
if (node.type === "text") return String(node.text ?? "");
|
|
108
|
+
if (node.type === "hardBreak") return "\n";
|
|
109
|
+
const inner = (node.content ?? []).map(walk).join("");
|
|
110
|
+
return blocks.has(node.type) ? `${inner}
|
|
111
|
+
` : inner;
|
|
112
|
+
};
|
|
113
|
+
return walk(doc).replace(/\n{2,}/g, "\n").trim();
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
escapeHtml,
|
|
117
|
+
renderRichText,
|
|
118
|
+
richTextToPlainText,
|
|
119
|
+
safeUrl
|
|
120
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easy-cms/richtext",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Render Easy CMS rich text (Tiptap JSON) to safe HTML",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/maritonx/easy-crm.git",
|
|
24
|
+
"directory": "packages/richtext"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"easy-cms",
|
|
28
|
+
"rich-text",
|
|
29
|
+
"tiptap",
|
|
30
|
+
"html"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"test": "vitest run"
|
|
36
|
+
}
|
|
37
|
+
}
|