@stll/chat 0.2.16 → 0.3.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/html-to-markdown.d.ts +11 -0
- package/dist/html-to-markdown.js +218 -0
- package/package.json +9 -3
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/html-to-markdown.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert sanitized HTML to GFM-flavored Markdown.
|
|
4
|
+
*
|
|
5
|
+
* Inputs come from cheerio with an upstream allowlist
|
|
6
|
+
* sanitizer; tags outside the allowlist are unwrapped
|
|
7
|
+
* (children rendered, tag dropped). Output ends with a
|
|
8
|
+
* single trailing newline when non-empty.
|
|
9
|
+
*/
|
|
10
|
+
export declare const htmlToMarkdown: (html: string) => string;
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import * as cheerio from "cheerio";
|
|
2
|
+
import { isTag, isText } from "domhandler";
|
|
3
|
+
//#region src/html-to-markdown.ts
|
|
4
|
+
const INLINE_ESCAPE = /(?<ch>[\\`*_~[\]<>|])/g;
|
|
5
|
+
const escapeText = (text) => text.replace(INLINE_ESCAPE, "\\$<ch>");
|
|
6
|
+
const LINK_DESTINATION_ESCAPE = /(?<ch>[\\<>])/g;
|
|
7
|
+
const NEEDS_BRACKETED_DESTINATION = /[\s()<>\\]/;
|
|
8
|
+
const renderLinkDestination = (href) => {
|
|
9
|
+
if (!NEEDS_BRACKETED_DESTINATION.test(href)) return href;
|
|
10
|
+
return `<${href.replace(/\r?\n/g, " ").replace(LINK_DESTINATION_ESCAPE, "\\$<ch>")}>`;
|
|
11
|
+
};
|
|
12
|
+
const collapseWhitespace = (text) => text.replace(/\s+/g, " ");
|
|
13
|
+
const endsWithWhitespace = (text) => text.length === 0 || text.endsWith(" ") || text.endsWith("\n");
|
|
14
|
+
const tagNameOf = (node) => node.tagName.toLowerCase();
|
|
15
|
+
const HEADING_LEVELS = {
|
|
16
|
+
h1: 1,
|
|
17
|
+
h2: 2,
|
|
18
|
+
h3: 3,
|
|
19
|
+
h4: 4,
|
|
20
|
+
h5: 5,
|
|
21
|
+
h6: 6
|
|
22
|
+
};
|
|
23
|
+
const BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
24
|
+
"blockquote",
|
|
25
|
+
"h1",
|
|
26
|
+
"h2",
|
|
27
|
+
"h3",
|
|
28
|
+
"h4",
|
|
29
|
+
"h5",
|
|
30
|
+
"h6",
|
|
31
|
+
"hr",
|
|
32
|
+
"ol",
|
|
33
|
+
"p",
|
|
34
|
+
"pre",
|
|
35
|
+
"table",
|
|
36
|
+
"ul"
|
|
37
|
+
]);
|
|
38
|
+
const rawText = (node) => {
|
|
39
|
+
if (isText(node)) return node.data;
|
|
40
|
+
if (isTag(node)) {
|
|
41
|
+
if (tagNameOf(node) === "br") return "\n";
|
|
42
|
+
return node.children.map(rawText).join("");
|
|
43
|
+
}
|
|
44
|
+
return "";
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Wrap inline code so that any backtick run inside the
|
|
48
|
+
* payload survives intact. CommonMark allows the fence to
|
|
49
|
+
* be any number of backticks longer than the longest
|
|
50
|
+
* internal run; pad with one space on each side when the
|
|
51
|
+
* payload itself starts or ends with a backtick.
|
|
52
|
+
*/
|
|
53
|
+
const wrapInlineCode = (text) => {
|
|
54
|
+
if (text === "") return "``";
|
|
55
|
+
let longestRun = 0;
|
|
56
|
+
for (const match of text.matchAll(/`+/g)) longestRun = Math.max(longestRun, match[0].length);
|
|
57
|
+
const fence = "`".repeat(longestRun + 1);
|
|
58
|
+
return `${fence}${text.startsWith("`") || text.endsWith("`") ? ` ${text} ` : text}${fence}`;
|
|
59
|
+
};
|
|
60
|
+
const escapeTableCellPipes = (text, context) => context === "table-cell" ? text.replace(/\|/g, "\\|") : text;
|
|
61
|
+
const CODE_HTML_ESCAPES = {
|
|
62
|
+
"&": "&",
|
|
63
|
+
"<": "<",
|
|
64
|
+
">": ">",
|
|
65
|
+
"|": "|"
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Inline code for a table cell. The row splitter drops one backslash before
|
|
69
|
+
* each `\|`, so code text with its own backslash before a pipe cannot be
|
|
70
|
+
* written as a code span that keeps both the cell and the text: it is written
|
|
71
|
+
* as an HTML `<code>` element, which carries the pipe as an entity.
|
|
72
|
+
*/
|
|
73
|
+
const renderTableCellCode = (text) => text.includes("\\|") ? `<code>${text.replace(/[&<>|]/g, (char) => CODE_HTML_ESCAPES[char] ?? char)}</code>` : escapeTableCellPipes(wrapInlineCode(text), "table-cell");
|
|
74
|
+
const renderInlineElement = (el, context) => {
|
|
75
|
+
const tag = tagNameOf(el);
|
|
76
|
+
switch (tag) {
|
|
77
|
+
case "strong":
|
|
78
|
+
case "b": return `**${renderInline(el.children, context)}**`;
|
|
79
|
+
case "em":
|
|
80
|
+
case "i": return `*${renderInline(el.children, context)}*`;
|
|
81
|
+
case "del":
|
|
82
|
+
case "s": return `~~${renderInline(el.children, context)}~~`;
|
|
83
|
+
case "code": return context === "table-cell" ? renderTableCellCode(rawText(el)) : wrapInlineCode(rawText(el));
|
|
84
|
+
case "br": return " \n";
|
|
85
|
+
case "a": {
|
|
86
|
+
const href = el.attribs["href"] ?? "";
|
|
87
|
+
return `[${renderInline(el.children, context)}](${escapeTableCellPipes(renderLinkDestination(href), context)})`;
|
|
88
|
+
}
|
|
89
|
+
case "u":
|
|
90
|
+
case "sub":
|
|
91
|
+
case "sup": return `<${tag}>${renderInline(el.children, context)}</${tag}>`;
|
|
92
|
+
default: return renderInline(el.children, context);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const renderInline = (nodes, context) => {
|
|
96
|
+
let out = "";
|
|
97
|
+
for (const node of nodes) {
|
|
98
|
+
if (isText(node)) {
|
|
99
|
+
let collapsed = collapseWhitespace(node.data);
|
|
100
|
+
if (collapsed.startsWith(" ") && endsWithWhitespace(out)) collapsed = collapsed.slice(1);
|
|
101
|
+
out += escapeText(collapsed);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (isTag(node)) out += renderInlineElement(node, context);
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
};
|
|
108
|
+
const renderMixed = (nodes) => {
|
|
109
|
+
const blocks = [];
|
|
110
|
+
let inlineRun = [];
|
|
111
|
+
const flushInline = () => {
|
|
112
|
+
if (inlineRun.length === 0) return;
|
|
113
|
+
const text = renderInline(inlineRun, "flow").trim();
|
|
114
|
+
if (text) blocks.push(text);
|
|
115
|
+
inlineRun = [];
|
|
116
|
+
};
|
|
117
|
+
for (const node of nodes) {
|
|
118
|
+
if (isTag(node) && BLOCK_TAGS.has(tagNameOf(node))) {
|
|
119
|
+
flushInline();
|
|
120
|
+
const block = renderBlock(node);
|
|
121
|
+
if (block) blocks.push(block);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
inlineRun.push(node);
|
|
125
|
+
}
|
|
126
|
+
flushInline();
|
|
127
|
+
return blocks.join("\n\n");
|
|
128
|
+
};
|
|
129
|
+
const renderListItem = (li) => renderMixed(li.children).trim();
|
|
130
|
+
const renderList = (el, ordered) => {
|
|
131
|
+
return el.children.filter((c) => isTag(c) && tagNameOf(c) === "li").map((li, index) => {
|
|
132
|
+
const marker = ordered ? `${index + 1}. ` : "- ";
|
|
133
|
+
const indent = " ".repeat(marker.length);
|
|
134
|
+
return marker + renderListItem(li).replace(/\n/g, () => `\n${indent}`);
|
|
135
|
+
}).join("\n");
|
|
136
|
+
};
|
|
137
|
+
const renderTableCell = (cell) => renderInline(cell.children, "table-cell").replace(/\n/g, " ");
|
|
138
|
+
const collectTableRows = (parent) => {
|
|
139
|
+
const rows = [];
|
|
140
|
+
for (const child of parent.children) {
|
|
141
|
+
if (!isTag(child)) continue;
|
|
142
|
+
const tag = tagNameOf(child);
|
|
143
|
+
if (tag === "thead" || tag === "tbody" || tag === "tfoot") {
|
|
144
|
+
rows.push(...collectTableRows(child));
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (tag !== "tr") continue;
|
|
148
|
+
const cells = [];
|
|
149
|
+
for (const cell of child.children) if (isTag(cell) && (tagNameOf(cell) === "th" || tagNameOf(cell) === "td")) cells.push(renderTableCell(cell));
|
|
150
|
+
rows.push(cells);
|
|
151
|
+
}
|
|
152
|
+
return rows;
|
|
153
|
+
};
|
|
154
|
+
const findTableCaption = (el) => {
|
|
155
|
+
const caption = el.children.find((c) => isTag(c) && tagNameOf(c) === "caption");
|
|
156
|
+
return caption ? renderInline(caption.children, "flow").trim() : "";
|
|
157
|
+
};
|
|
158
|
+
const renderTable = (el) => {
|
|
159
|
+
const rows = collectTableRows(el);
|
|
160
|
+
if (rows.length === 0) return "";
|
|
161
|
+
const colCount = Math.max(...rows.map((r) => r.length));
|
|
162
|
+
const [header = [], ...body] = rows.map((row) => {
|
|
163
|
+
const copy = Array.from(row);
|
|
164
|
+
while (copy.length < colCount) copy.push("");
|
|
165
|
+
return copy;
|
|
166
|
+
});
|
|
167
|
+
const tableLines = [
|
|
168
|
+
`| ${header.join(" | ")} |`,
|
|
169
|
+
`| ${header.map(() => "---").join(" | ")} |`,
|
|
170
|
+
...body.map((row) => `| ${row.join(" | ")} |`)
|
|
171
|
+
];
|
|
172
|
+
const caption = findTableCaption(el);
|
|
173
|
+
return caption ? `**${caption}**\n\n${tableLines.join("\n")}` : tableLines.join("\n");
|
|
174
|
+
};
|
|
175
|
+
const renderBlock = (el) => {
|
|
176
|
+
const tag = tagNameOf(el);
|
|
177
|
+
const headingLevel = HEADING_LEVELS[tag];
|
|
178
|
+
if (headingLevel !== void 0) return `${"#".repeat(headingLevel)} ${renderInline(el.children, "flow").trim()}`;
|
|
179
|
+
switch (tag) {
|
|
180
|
+
case "p": return renderInline(el.children, "flow").trim();
|
|
181
|
+
case "hr": return "---";
|
|
182
|
+
case "blockquote": {
|
|
183
|
+
const inner = renderChildren(el.children).trim();
|
|
184
|
+
if (!inner) return "";
|
|
185
|
+
return inner.split("\n").map((line) => `> ${line}`.trimEnd()).join("\n");
|
|
186
|
+
}
|
|
187
|
+
case "pre": {
|
|
188
|
+
const codeEl = el.children.find((c) => isTag(c) && tagNameOf(c) === "code");
|
|
189
|
+
return `\`\`\`${codeEl?.attribs["class"]?.match(/language-(?<lang>\S+)/)?.groups?.["lang"] ?? ""}\n${trimTrailingNewlines(rawText(codeEl ?? el))}\n\`\`\``;
|
|
190
|
+
}
|
|
191
|
+
case "ul": return renderList(el, false);
|
|
192
|
+
case "ol": return renderList(el, true);
|
|
193
|
+
case "table": return renderTable(el);
|
|
194
|
+
default: return renderChildren(el.children).trim();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const renderChildren = (nodes) => renderMixed(nodes);
|
|
198
|
+
const trimTrailingNewlines = (text) => {
|
|
199
|
+
let end = text.length;
|
|
200
|
+
while (text[end - 1] === "\n") end -= 1;
|
|
201
|
+
return text.slice(0, end);
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Convert sanitized HTML to GFM-flavored Markdown.
|
|
205
|
+
*
|
|
206
|
+
* Inputs come from cheerio with an upstream allowlist
|
|
207
|
+
* sanitizer; tags outside the allowlist are unwrapped
|
|
208
|
+
* (children rendered, tag dropped). Output ends with a
|
|
209
|
+
* single trailing newline when non-empty.
|
|
210
|
+
*/
|
|
211
|
+
const htmlToMarkdown = (html) => {
|
|
212
|
+
if (!html.trim()) return "";
|
|
213
|
+
const $ = cheerio.load(html, void 0, false);
|
|
214
|
+
const body = renderChildren($.root().contents().toArray());
|
|
215
|
+
return body ? `${body}\n` : "";
|
|
216
|
+
};
|
|
217
|
+
//#endregion
|
|
218
|
+
export { htmlToMarkdown };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Typed React chat composer, message stream, model selector, and transport runtime primitives.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -49,6 +49,10 @@
|
|
|
49
49
|
"types": "./dist/durable-transport.d.ts",
|
|
50
50
|
"import": "./dist/durable-transport.js"
|
|
51
51
|
},
|
|
52
|
+
"./html-to-markdown": {
|
|
53
|
+
"types": "./dist/html-to-markdown.d.ts",
|
|
54
|
+
"import": "./dist/html-to-markdown.js"
|
|
55
|
+
},
|
|
52
56
|
"./resume-contract": {
|
|
53
57
|
"types": "./dist/resume-contract.d.ts",
|
|
54
58
|
"import": "./dist/resume-contract.js"
|
|
@@ -86,11 +90,13 @@
|
|
|
86
90
|
},
|
|
87
91
|
"dependencies": {
|
|
88
92
|
"@ag-ui/core": "1.0.3",
|
|
89
|
-
"@stll/ai-catalog": "^0.
|
|
90
|
-
"@stll/ui": "^0.
|
|
93
|
+
"@stll/ai-catalog": "^0.9.0",
|
|
94
|
+
"@stll/ui": "^0.60.0",
|
|
91
95
|
"@tanstack/ai-client": "0.38.0",
|
|
92
96
|
"@types/hast": "^3.0.5",
|
|
93
97
|
"better-result": "3.0.1",
|
|
98
|
+
"cheerio": "^1.2.0",
|
|
99
|
+
"domhandler": "^6.0.1",
|
|
94
100
|
"linkify-it": "6.1.0",
|
|
95
101
|
"tldts": "7.4.19",
|
|
96
102
|
"valibot": "1.5.0",
|