@easy-cms/richtext 0.17.0 → 0.18.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/dist/index.d.ts +10 -1
- package/dist/index.js +96 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -28,5 +28,14 @@ declare function safeUrl(value: unknown): string | undefined;
|
|
|
28
28
|
declare function renderRichText(doc: RichTextInput | null | undefined, options?: RenderOptions): string;
|
|
29
29
|
/** Plain text of a document, e.g. for excerpts or search. Blocks are separated by newlines. */
|
|
30
30
|
declare function richTextToPlainText(doc: RichTextInput | null | undefined): string;
|
|
31
|
+
interface MarkdownOptions {
|
|
32
|
+
/** Override or add node renderers. Receives the node and its rendered children. */
|
|
33
|
+
nodes?: Record<string, (node: RichTextNode, children: string) => string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Renders a rich text document to Markdown, e.g. for `llms.txt` or `.md` versions of pages.
|
|
37
|
+
* Text that looks like Markdown is escaped, and unsafe URLs are removed.
|
|
38
|
+
*/
|
|
39
|
+
declare function renderMarkdown(doc: RichTextInput | null | undefined, options?: MarkdownOptions): string;
|
|
31
40
|
|
|
32
|
-
export { type RenderOptions, type RichTextInput, type RichTextNode, escapeHtml, renderRichText, richTextToPlainText, safeUrl };
|
|
41
|
+
export { type MarkdownOptions, type RenderOptions, type RichTextInput, type RichTextNode, escapeHtml, renderMarkdown, renderRichText, richTextToPlainText, safeUrl };
|
package/dist/index.js
CHANGED
|
@@ -112,8 +112,104 @@ function richTextToPlainText(doc) {
|
|
|
112
112
|
};
|
|
113
113
|
return walk(doc).replace(/\n{2,}/g, "\n").trim();
|
|
114
114
|
}
|
|
115
|
+
function escapeMarkdown(text) {
|
|
116
|
+
return text.replace(/[\\`*_[\]<>|]/g, (c) => `\\${c}`);
|
|
117
|
+
}
|
|
118
|
+
function escapeLineStart(text) {
|
|
119
|
+
return text.replace(
|
|
120
|
+
/^(\s*)(#{1,6}\s|>|[-+]\s|\d+[.)]\s)/gm,
|
|
121
|
+
(_, space, start) => `${space}\\${start}`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
function markdownMarks(text, marks) {
|
|
125
|
+
let md = escapeMarkdown(text);
|
|
126
|
+
if (md.trim() === "") return md;
|
|
127
|
+
for (const mark of marks ?? []) {
|
|
128
|
+
switch (mark.type) {
|
|
129
|
+
case "bold":
|
|
130
|
+
md = `**${md}**`;
|
|
131
|
+
break;
|
|
132
|
+
case "italic":
|
|
133
|
+
md = `*${md}*`;
|
|
134
|
+
break;
|
|
135
|
+
case "strike":
|
|
136
|
+
md = `~~${md}~~`;
|
|
137
|
+
break;
|
|
138
|
+
case "code":
|
|
139
|
+
md = `\`${text.replace(/`/g, "'")}\``;
|
|
140
|
+
break;
|
|
141
|
+
case "link": {
|
|
142
|
+
const href = safeUrl(mark.attrs?.href);
|
|
143
|
+
if (href) md = `[${md}](${href.replace(/[()\s]/g, urlChar)})`;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return md;
|
|
149
|
+
}
|
|
150
|
+
var urlChar = (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`;
|
|
151
|
+
var indent = (text, by) => text.replace(/\n(?!\n|$)/g, `
|
|
152
|
+
${by}`);
|
|
153
|
+
function markdownNode(node, options) {
|
|
154
|
+
if (!node || typeof node !== "object") return "";
|
|
155
|
+
if (node.type === "text") return markdownMarks(String(node.text ?? ""), node.marks);
|
|
156
|
+
const content = Array.isArray(node.content) ? node.content : [];
|
|
157
|
+
const inline = () => content.map((c) => markdownNode(c, options)).join("");
|
|
158
|
+
const blocks = () => content.map((c) => markdownNode(c, options)).filter((b) => b.trim() !== "").join("\n\n");
|
|
159
|
+
const custom = options.nodes?.[node.type];
|
|
160
|
+
if (custom) return custom(node, blocks());
|
|
161
|
+
switch (node.type) {
|
|
162
|
+
case "doc":
|
|
163
|
+
return blocks();
|
|
164
|
+
case "paragraph":
|
|
165
|
+
return escapeLineStart(inline());
|
|
166
|
+
case "heading": {
|
|
167
|
+
const level = Math.min(6, Math.max(1, Number(node.attrs?.level) || 2));
|
|
168
|
+
return `${"#".repeat(level)} ${inline().replace(/\n/g, " ")}`;
|
|
169
|
+
}
|
|
170
|
+
case "bulletList":
|
|
171
|
+
case "orderedList": {
|
|
172
|
+
const start = Number(node.attrs?.start);
|
|
173
|
+
let n = Number.isInteger(start) ? start : 1;
|
|
174
|
+
return content.map((item) => {
|
|
175
|
+
const marker = node.type === "orderedList" ? `${n++}.` : "-";
|
|
176
|
+
const body = markdownNode(item, options);
|
|
177
|
+
return `${marker} ${indent(body, " ".repeat(marker.length + 1))}`;
|
|
178
|
+
}).join("\n");
|
|
179
|
+
}
|
|
180
|
+
case "listItem":
|
|
181
|
+
return content.map((c) => markdownNode(c, options)).filter((b) => b.trim() !== "").join("\n");
|
|
182
|
+
case "blockquote":
|
|
183
|
+
return blocks().split("\n").map((line) => line ? `> ${line}` : ">").join("\n");
|
|
184
|
+
case "codeBlock": {
|
|
185
|
+
const language = typeof node.attrs?.language === "string" ? node.attrs.language : "";
|
|
186
|
+
const code = content.map((c) => String(c.text ?? "")).join("");
|
|
187
|
+
const fence = code.includes("```") ? "~~~~" : "```";
|
|
188
|
+
return `${fence}${language}
|
|
189
|
+
${code}
|
|
190
|
+
${fence}`;
|
|
191
|
+
}
|
|
192
|
+
case "hardBreak":
|
|
193
|
+
return " \n";
|
|
194
|
+
case "horizontalRule":
|
|
195
|
+
return "---";
|
|
196
|
+
case "image": {
|
|
197
|
+
const src = safeUrl(node.attrs?.src);
|
|
198
|
+
if (!src) return "";
|
|
199
|
+
const alt = typeof node.attrs?.alt === "string" ? escapeMarkdown(node.attrs.alt) : "";
|
|
200
|
+
return `\s]/g, urlChar)})`;
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
return content.some((c) => c.type === "text") ? inline() : blocks();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function renderMarkdown(doc, options = {}) {
|
|
207
|
+
if (!doc) return "";
|
|
208
|
+
return markdownNode(doc, options).trim();
|
|
209
|
+
}
|
|
115
210
|
export {
|
|
116
211
|
escapeHtml,
|
|
212
|
+
renderMarkdown,
|
|
117
213
|
renderRichText,
|
|
118
214
|
richTextToPlainText,
|
|
119
215
|
safeUrl
|