@liguoshuai/pi-web-chat 2.14.5 → 2.15.4
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 +1 -3
- package/docs/ARCHITECTURE.md +3 -6
- package/package.json +4 -4
- package/public/app.js +65 -247
- package/public/index.html +1 -0
- package/public/markdown.js +263 -0
- package/public/style.css +6 -0
- package/docs/CHANGELOG.md +0 -947
- package/docs/ISSUES.md +0 -268
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// markdown.js — Lightweight, safe Markdown renderer for pi-web-chat
|
|
2
|
+
// Handles code fence blocks, tables, lists, task checkboxes, headers, blockquotes, inline formatting, and safe URLs.
|
|
3
|
+
|
|
4
|
+
(function (global) {
|
|
5
|
+
function escapeHtml(s) {
|
|
6
|
+
if (!s) return "";
|
|
7
|
+
return s.replace(/&/g, "&")
|
|
8
|
+
.replace(/</g, "<")
|
|
9
|
+
.replace(/>/g, ">")
|
|
10
|
+
.replace(/"/g, """)
|
|
11
|
+
.replace(/'/g, "'");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function copyToClipboard(text) {
|
|
15
|
+
if (!text) return false;
|
|
16
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
17
|
+
try {
|
|
18
|
+
await navigator.clipboard.writeText(text);
|
|
19
|
+
return true;
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.warn("navigator.clipboard.writeText failed:", e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const ta = document.createElement("textarea");
|
|
26
|
+
ta.value = text;
|
|
27
|
+
ta.style.position = "fixed";
|
|
28
|
+
ta.style.left = "-9999px";
|
|
29
|
+
ta.style.top = "-9999px";
|
|
30
|
+
document.body.appendChild(ta);
|
|
31
|
+
ta.focus();
|
|
32
|
+
ta.select();
|
|
33
|
+
const ok = document.execCommand("copy");
|
|
34
|
+
document.body.removeChild(ta);
|
|
35
|
+
return ok;
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.error("Fallback copy error:", e);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function sanitizeUrl(url) {
|
|
43
|
+
if (!url) return "#";
|
|
44
|
+
const trimmed = url.trim();
|
|
45
|
+
if (/^(https?:\/\/|mailto:)/i.test(trimmed)) {
|
|
46
|
+
return trimmed.replace(/"/g, """).replace(/'/g, "'");
|
|
47
|
+
}
|
|
48
|
+
return "#";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function mdInlineBlock(text) {
|
|
52
|
+
let s = escapeHtml(text);
|
|
53
|
+
// bold
|
|
54
|
+
s = s.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
|
|
55
|
+
s = s.replace(/__(.+?)__/g, "<strong>$1</strong>");
|
|
56
|
+
// strikethrough
|
|
57
|
+
s = s.replace(/~~(.+?)~~/g, "<del>$1</del>");
|
|
58
|
+
// italic (asterisk)
|
|
59
|
+
s = s.replace(/(^|[^*])\*([^*\s](?:[^*]*?[^*\s])?)\*(?!\*)/g, "$1<em>$2</em>");
|
|
60
|
+
// italic (underscore): only match boundary/space so identifier_names are preserved
|
|
61
|
+
s = s.replace(/(^|[\s(\[<,;:])_([^_\s](?:[^_]*?[^_\s])?)_(?=[\s)\]>,;:!?.]|$)/g, "$1<em>$2</em>");
|
|
62
|
+
// links [txt](url) - strictly sanitize URL
|
|
63
|
+
s = s.replace(/\[([^\]]+)\]\(((?:https?:\/\/|mailto:)[^\s)]+)\)/g, (match, txt, url) => {
|
|
64
|
+
const safeUrl = sanitizeUrl(url);
|
|
65
|
+
return `<a href="${safeUrl}" target="_blank" rel="noopener">${txt}</a>`;
|
|
66
|
+
});
|
|
67
|
+
return s;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function mdTable(lines) {
|
|
71
|
+
const parseRow = (l) => l.split("|").map(c => c.trim()).filter((_, i, arr) => !(i === 0 && arr[0] === "") && !(i === arr.length - 1 && arr[arr.length - 1] === ""));
|
|
72
|
+
const header = parseRow(lines[0]);
|
|
73
|
+
const body = lines.slice(2).filter(l => l.trim()).map(parseRow);
|
|
74
|
+
let h = '<table><thead><tr>';
|
|
75
|
+
header.forEach((c) => h += `<th>${mdInlineBlock(c)}</th>`);
|
|
76
|
+
h += '</tr></thead><tbody>';
|
|
77
|
+
body.forEach((r) => {
|
|
78
|
+
h += '<tr>';
|
|
79
|
+
r.forEach((c) => h += `<td>${mdInlineBlock(c)}</td>`);
|
|
80
|
+
h += '</tr>';
|
|
81
|
+
});
|
|
82
|
+
h += '</tbody></table>';
|
|
83
|
+
return h;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function renderInlineMd(text) {
|
|
87
|
+
// tables, then markdown-ish transforms. Escape first.
|
|
88
|
+
// Split out inline code first using placeholders to protect them.
|
|
89
|
+
const codeChunks = [];
|
|
90
|
+
let t = text.replace(/`([^`\n]+)`/g, (m) => {
|
|
91
|
+
const i = codeChunks.length;
|
|
92
|
+
codeChunks.push(m);
|
|
93
|
+
return `\u0000CODE${i}\u0000`;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const lines = t.split("\n");
|
|
97
|
+
const out = [];
|
|
98
|
+
let i = 0;
|
|
99
|
+
while (i < lines.length) {
|
|
100
|
+
if (lines[i].includes("|") && i + 1 < lines.length && /^\s*\|?[\s\-:|]+\|?\s*$/.test(lines[i + 1]) && lines[i+1].includes("-")) {
|
|
101
|
+
const header = lines[i];
|
|
102
|
+
const tblLines = [header, lines[i + 1]];
|
|
103
|
+
let j = i + 2;
|
|
104
|
+
while (j < lines.length && lines[j].includes("|")) { tblLines.push(lines[j]); j++; }
|
|
105
|
+
out.push({ kind: "table", lines: tblLines });
|
|
106
|
+
i = j;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
out.push({ kind: "line", text: lines[i] });
|
|
110
|
+
i++;
|
|
111
|
+
}
|
|
112
|
+
let outHtml = "";
|
|
113
|
+
let para = [];
|
|
114
|
+
let linkListOpen = null;
|
|
115
|
+
let linkListOrdered = null;
|
|
116
|
+
|
|
117
|
+
function flushList() {
|
|
118
|
+
if (linkListOpen) {
|
|
119
|
+
outHtml += linkListOpen === "ol" ? "</ol>" : "</ul>";
|
|
120
|
+
linkListOpen = null;
|
|
121
|
+
linkListOrdered = null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function flushPara() {
|
|
126
|
+
if (para.length === 0) return;
|
|
127
|
+
const block = para.join("\n").trim();
|
|
128
|
+
para = [];
|
|
129
|
+
outHtml += "<p>" + mdInlineBlock(block).replace(/\n/g, "<br>") + "</p>";
|
|
130
|
+
}
|
|
131
|
+
for (const seg of out) {
|
|
132
|
+
if (seg.kind === "table") {
|
|
133
|
+
flushPara();
|
|
134
|
+
flushList();
|
|
135
|
+
outHtml += mdTable(seg.lines);
|
|
136
|
+
} else if (seg.kind === "line") {
|
|
137
|
+
// horizontal rule
|
|
138
|
+
if (/^\s*([-*_])\s*\1\s*\1[\s\-_*]*$/.test(seg.text)) {
|
|
139
|
+
flushPara();
|
|
140
|
+
flushList();
|
|
141
|
+
outHtml += "<hr>";
|
|
142
|
+
} else if (/^(#{1,6})\s+(.*)$/.test(seg.text)) {
|
|
143
|
+
// headings
|
|
144
|
+
const m = seg.text.match(/^(#{1,6})\s+(.*)$/);
|
|
145
|
+
flushPara();
|
|
146
|
+
flushList();
|
|
147
|
+
const level = m[1].length;
|
|
148
|
+
outHtml += `<h${level}>${mdInlineBlock(m[2])}</h${level}>`;
|
|
149
|
+
} else if (/^\s*$/.test(seg.text)) {
|
|
150
|
+
flushPara();
|
|
151
|
+
flushList();
|
|
152
|
+
} else if (/^>\s?/.test(seg.text)) {
|
|
153
|
+
// blockquote line
|
|
154
|
+
flushPara();
|
|
155
|
+
flushList();
|
|
156
|
+
outHtml += `<blockquote>${mdInlineBlock(seg.text.replace(/^>\s?/, ""))}</blockquote>`;
|
|
157
|
+
} else if (/^\s*[-*+]\s+/.test(seg.text) || /^\s*\d+\.\s+/.test(seg.text)) {
|
|
158
|
+
// list item
|
|
159
|
+
const isOrdered = /^\s*\d+\.\s+/.test(seg.text);
|
|
160
|
+
if (!linkListOpen || linkListOrdered !== isOrdered) {
|
|
161
|
+
flushPara();
|
|
162
|
+
flushList();
|
|
163
|
+
linkListOpen = isOrdered ? "ol" : "ul";
|
|
164
|
+
linkListOrdered = isOrdered;
|
|
165
|
+
outHtml += "<" + linkListOpen + ">";
|
|
166
|
+
}
|
|
167
|
+
let itemText = seg.text.replace(/^\s*([-*+]|\d+\.)\s+/, "");
|
|
168
|
+
let taskPrefix = "";
|
|
169
|
+
if (/^\[ \]\s+/.test(itemText)) {
|
|
170
|
+
taskPrefix = '<input type="checkbox" disabled class="task-list-item-checkbox"> ';
|
|
171
|
+
itemText = itemText.replace(/^\[ \]\s+/, "");
|
|
172
|
+
} else if (/^\[[xX]\]\s+/.test(itemText)) {
|
|
173
|
+
taskPrefix = '<input type="checkbox" checked disabled class="task-list-item-checkbox"> ';
|
|
174
|
+
itemText = itemText.replace(/^\[[xX]\]\s+/, "");
|
|
175
|
+
}
|
|
176
|
+
outHtml += `<li>${taskPrefix}${mdInlineBlock(itemText)}</li>`;
|
|
177
|
+
} else {
|
|
178
|
+
flushList();
|
|
179
|
+
para.push(seg.text);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
flushList();
|
|
184
|
+
flushPara();
|
|
185
|
+
// restore inline code
|
|
186
|
+
outHtml = outHtml.replace(/\u0000CODE(\d+)\u0000/g, (_, n) => {
|
|
187
|
+
const chunk = codeChunks[+n];
|
|
188
|
+
return chunk ? `<code>${escapeHtml(chunk.slice(1, -1))}</code>` : "";
|
|
189
|
+
});
|
|
190
|
+
return outHtml;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function renderMarkdown(md) {
|
|
194
|
+
if (!md) return "";
|
|
195
|
+
if (typeof md !== "string") md = String(md);
|
|
196
|
+
const parts = [];
|
|
197
|
+
let rest = md;
|
|
198
|
+
while (rest.length) {
|
|
199
|
+
const fenceMatch = rest.match(/(?:^|\n)```/);
|
|
200
|
+
if (!fenceMatch) {
|
|
201
|
+
parts.push({ kind: "md", text: rest });
|
|
202
|
+
rest = "";
|
|
203
|
+
} else {
|
|
204
|
+
const fenceIdx = fenceMatch.index + (fenceMatch[0].length - 3);
|
|
205
|
+
if (fenceIdx > 0) parts.push({ kind: "md", text: rest.slice(0, fenceIdx) });
|
|
206
|
+
rest = rest.slice(fenceIdx + 3);
|
|
207
|
+
const nl = rest.indexOf("\n");
|
|
208
|
+
let lang = "";
|
|
209
|
+
if (nl !== -1) {
|
|
210
|
+
const firstLine = rest.slice(0, nl).trim();
|
|
211
|
+
if (firstLine && !firstLine.includes("```")) lang = firstLine;
|
|
212
|
+
rest = rest.slice(nl + 1);
|
|
213
|
+
}
|
|
214
|
+
const closeIdx = rest.indexOf("```");
|
|
215
|
+
let code;
|
|
216
|
+
if (closeIdx === -1) { code = rest; rest = ""; }
|
|
217
|
+
else { code = rest.slice(0, closeIdx); rest = rest.slice(closeIdx + 3).replace(/^\n/, ""); }
|
|
218
|
+
parts.push({ kind: "code", lang, code });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
let html = "";
|
|
222
|
+
for (const p of parts) {
|
|
223
|
+
if (p.kind === "code") {
|
|
224
|
+
const lang = p.lang || "";
|
|
225
|
+
const displayLang = lang || "code";
|
|
226
|
+
html += `<div class="code-block-wrapper">` +
|
|
227
|
+
`<div class="code-block-header">` +
|
|
228
|
+
`<span class="code-block-lang">${escapeHtml(displayLang)}</span>` +
|
|
229
|
+
`<button class="btn-copy-code" type="button" aria-label="复制代码" title="复制代码">` +
|
|
230
|
+
`<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>` +
|
|
231
|
+
`<span>复制</span>` +
|
|
232
|
+
`</button>` +
|
|
233
|
+
`</div>` +
|
|
234
|
+
`<pre><code data-lang="${escapeHtml(lang)}">${escapeHtml(p.code)}</code></pre>` +
|
|
235
|
+
`</div>`;
|
|
236
|
+
} else {
|
|
237
|
+
html += renderInlineMd(p.text);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return html;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Export to global scope & Node/module exports
|
|
244
|
+
const PiMarkdown = {
|
|
245
|
+
escapeHtml,
|
|
246
|
+
copyToClipboard,
|
|
247
|
+
sanitizeUrl,
|
|
248
|
+
mdInlineBlock,
|
|
249
|
+
mdTable,
|
|
250
|
+
renderInlineMd,
|
|
251
|
+
renderMarkdown
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
if (typeof exports === "object" && typeof module !== "undefined") {
|
|
255
|
+
module.exports = PiMarkdown;
|
|
256
|
+
}
|
|
257
|
+
const root = typeof window !== "undefined" ? window : (typeof globalThis !== "undefined" ? globalThis : global);
|
|
258
|
+
root.PiMarkdown = PiMarkdown;
|
|
259
|
+
root.escapeHtml = escapeHtml;
|
|
260
|
+
root.copyToClipboard = copyToClipboard;
|
|
261
|
+
root.sanitizeUrl = sanitizeUrl;
|
|
262
|
+
root.renderMarkdown = renderMarkdown;
|
|
263
|
+
})(typeof window !== "undefined" ? window : (typeof globalThis !== "undefined" ? globalThis : this));
|
package/public/style.css
CHANGED
|
@@ -734,6 +734,12 @@ body {
|
|
|
734
734
|
.btn-copy-code svg, .btn-copy-msg svg, .btn-copy-tool svg {
|
|
735
735
|
width: 12px;
|
|
736
736
|
height: 12px;
|
|
737
|
+
fill: none;
|
|
738
|
+
stroke: currentColor;
|
|
739
|
+
stroke-width: 2;
|
|
740
|
+
stroke-linecap: round;
|
|
741
|
+
stroke-linejoin: round;
|
|
742
|
+
flex-shrink: 0;
|
|
737
743
|
}
|
|
738
744
|
.msg.assistant .content ul, .msg.assistant .content ol { margin: 8px 0 12px 24px; }
|
|
739
745
|
.msg.assistant .content li { margin: 4px 0; }
|