create-heresay 0.2.25 → 0.2.27
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/package.json +1 -1
- package/release/VERSION +1 -1
- package/release/functions/lib/core/chat.js +89 -18
- package/release/functions/lib/core/handler.js +2 -1
- package/release/functions/lib/core/types.js +2 -0
- package/release/functions/lib/core/version.js +1 -1
- package/release/public/dashboard.css +20 -1
- package/release/public/dashboard.js +127 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-heresay",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.27",
|
|
4
4
|
"description": "Set up your own Heresay: in-app feedback that every report gets a hearing. Creates a Firebase project in your Google account and deploys the dashboard, API and SDK to it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/release/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.27
|
|
@@ -26,11 +26,15 @@ export const CHAT_LIMITS = [
|
|
|
26
26
|
// approval or what counts as sensitive ("Suspicious Instruction"). Their own rules cover that.
|
|
27
27
|
const INSTRUCTIONS = [
|
|
28
28
|
'Heresay is this team\'s inbox for what should change in their apps: bugs, confusing parts, improvements and ideas.',
|
|
29
|
-
'
|
|
30
|
-
'
|
|
31
|
-
'
|
|
32
|
-
'
|
|
29
|
+
'Sending takes two steps. draft_request prepares one and returns a preview (the app, the type, the text, and the other',
|
|
30
|
+
'apps); nothing is sent. send_draft sends a draft, to the app in the preview or to another one. `text` is the request as',
|
|
31
|
+
'a teammate would read it; `details` is what the conversation worked out (why it matters, what was considered or ruled',
|
|
32
|
+
'out, examples, constraints, open questions); both may be Markdown. `document` attaches a longer Markdown write-up (a spec,',
|
|
33
|
+
'a summary of the whole conversation), up to 100,000 characters. A sent request stays open until someone on the team accepts it; sending',
|
|
34
|
+
'does not start any work. my_requests says what happened to the ones this member sent.',
|
|
33
35
|
].join(' ');
|
|
36
|
+
/** How long a draft waits to be sent. */
|
|
37
|
+
const DRAFT_TTL_MS = 2 * 3600_000;
|
|
34
38
|
const TOOLS = [
|
|
35
39
|
{
|
|
36
40
|
name: 'list_apps',
|
|
@@ -40,22 +44,40 @@ const TOOLS = [
|
|
|
40
44
|
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
41
45
|
},
|
|
42
46
|
{
|
|
43
|
-
name: '
|
|
44
|
-
title: '
|
|
45
|
-
description: '
|
|
46
|
-
+ '
|
|
47
|
+
name: 'draft_request',
|
|
48
|
+
title: 'Draft a Heresay request',
|
|
49
|
+
description: 'Prepares a bug, a confusing part, an improvement or an idea for one app\'s Heresay inbox and returns a preview: '
|
|
50
|
+
+ 'the app it would go to, its type and text, and the other apps. Nothing is sent. Without an app it returns the list of apps.',
|
|
47
51
|
inputSchema: {
|
|
48
52
|
type: 'object',
|
|
49
53
|
properties: {
|
|
50
|
-
app: { type: 'string', description: 'The app\'s id or name, from list_apps' },
|
|
54
|
+
app: { type: 'string', description: 'The app\'s id or name, from list_apps, when the conversation names it' },
|
|
51
55
|
type: { type: 'string', enum: [...REPORT_TYPES], description: 'broken: doesn\'t work. confusing: works, but people can\'t tell how. improvement: works, could be better. idea: doesn\'t exist yet.' },
|
|
52
56
|
text: { type: 'string', maxLength: LIMITS.textMax, description: 'The request in a few plain sentences, as a teammate would read it.' },
|
|
53
|
-
details: { type: 'string', maxLength: LIMITS.detailsMax, description: 'Context from the conversation: why, what was considered or ruled out, examples, constraints, open questions.' },
|
|
57
|
+
details: { type: 'string', maxLength: LIMITS.detailsMax, description: 'Context from the conversation: why, what was considered or ruled out, examples, constraints, open questions. Markdown.' },
|
|
58
|
+
document: { type: 'string', maxLength: LIMITS.documentMax, description: 'A longer Markdown document attached to the request: a spec, a write-up of the conversation, notes. Optional.' },
|
|
59
|
+
document_title: { type: 'string', maxLength: LIMITS.documentTitleMax, description: 'The attached document\'s title.' },
|
|
54
60
|
},
|
|
55
|
-
required: ['
|
|
61
|
+
required: ['type', 'text'],
|
|
56
62
|
additionalProperties: false,
|
|
57
63
|
},
|
|
58
|
-
annotations: { readOnlyHint:
|
|
64
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'send_draft',
|
|
68
|
+
title: 'Send a Heresay draft',
|
|
69
|
+
description: 'Sends a draft from draft_request to the inbox, as the signed-in team member. `app` sends it to a different app '
|
|
70
|
+
+ 'than the preview named. It stays open until someone on the team accepts it.',
|
|
71
|
+
inputSchema: {
|
|
72
|
+
type: 'object',
|
|
73
|
+
properties: {
|
|
74
|
+
draft_id: { type: 'string', description: 'From draft_request' },
|
|
75
|
+
app: { type: 'string', description: 'Another app\'s id or name, to send it there instead' },
|
|
76
|
+
},
|
|
77
|
+
required: ['draft_id'],
|
|
78
|
+
additionalProperties: false,
|
|
79
|
+
},
|
|
80
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
59
81
|
},
|
|
60
82
|
{
|
|
61
83
|
name: 'my_requests',
|
|
@@ -132,8 +154,11 @@ async function one(m, member, via, req, deps) {
|
|
|
132
154
|
try {
|
|
133
155
|
if (name === 'list_apps')
|
|
134
156
|
return rpcResult(msg.id, await listApps(deps));
|
|
135
|
-
|
|
136
|
-
|
|
157
|
+
// send_request is what older bridges call; it drafts too, so nothing is sent unseen.
|
|
158
|
+
if (name === 'draft_request' || name === 'send_request')
|
|
159
|
+
return rpcResult(msg.id, await draftRequest(args, member, via, deps));
|
|
160
|
+
if (name === 'send_draft')
|
|
161
|
+
return rpcResult(msg.id, await sendDraft(args, member, req, deps));
|
|
137
162
|
if (name === 'my_requests')
|
|
138
163
|
return rpcResult(msg.id, await myRequests(args, member, req, deps));
|
|
139
164
|
return rpcError(msg.id, -32602, `Unknown tool: ${String(name)}`);
|
|
@@ -167,14 +192,60 @@ async function findApp(v, deps) {
|
|
|
167
192
|
return hit;
|
|
168
193
|
}
|
|
169
194
|
const dashboardLink = (req, deps, p) => `${baseUrl(req, deps)}/app/#/apps/${p.id}`;
|
|
170
|
-
|
|
171
|
-
|
|
195
|
+
const appNames = (ps) => ps.map((p) => `${p.name} (${p.platform})`).join(', ');
|
|
196
|
+
/** A preview, nothing sent: which app, what type, the text, and where else it could go. */
|
|
197
|
+
async function draftRequest(a, member, via, deps) {
|
|
172
198
|
if (!isReportType(a.type))
|
|
173
199
|
throw new ToolError(`type must be one of ${REPORT_TYPES.join(', ')}.`);
|
|
174
200
|
const text = str(a.text, LIMITS.textMax);
|
|
175
201
|
if (!text)
|
|
176
202
|
throw new ToolError('Say what the request is, in text.');
|
|
177
203
|
const details = str(a.details, LIMITS.detailsMax);
|
|
204
|
+
// Refused rather than cut, so a long write-up isn't silently missing its end.
|
|
205
|
+
if (typeof a.document === 'string' && a.document.length > LIMITS.documentMax) {
|
|
206
|
+
throw new ToolError(`The document is ${a.document.length} characters; the limit is ${LIMITS.documentMax}. Shorten it or split it across requests.`);
|
|
207
|
+
}
|
|
208
|
+
const markdown = typeof a.document === 'string' && a.document.trim() ? a.document.replace(/\r\n/g, '\n').trim() : null;
|
|
209
|
+
const document = markdown ? { title: str(a.document_title, LIMITS.documentTitleMax), markdown } : null;
|
|
210
|
+
const all = (await deps.store.listProjects()).sort((x, y) => x.created_at.localeCompare(y.created_at));
|
|
211
|
+
if (!str(a.app, 100)) {
|
|
212
|
+
return result(`No app named, so no draft yet. The apps on this Heresay: ${appNames(all) || 'none yet'}. Which one is it for?`, { draft: null, apps: all.map((p) => ({ id: p.id, name: p.name, platform: p.platform })) });
|
|
213
|
+
}
|
|
214
|
+
const project = await findApp(a.app, deps);
|
|
215
|
+
const id = createHash('sha256').update(`${member.email}${deps.now()}${Math.random()}`).digest('hex').slice(0, 24);
|
|
216
|
+
await deps.store.putAuth({
|
|
217
|
+
kind: 'draft', id, expires_at: new Date(deps.now() + DRAFT_TTL_MS).toISOString(),
|
|
218
|
+
data: { email: member.email, project_id: project.id, type: a.type, text, details, document, via },
|
|
219
|
+
});
|
|
220
|
+
const others = all.filter((p) => p.id !== project.id);
|
|
221
|
+
return result([
|
|
222
|
+
`Draft, not sent yet. App: ${project.name} (${project.platform}). Type: ${a.type}.`,
|
|
223
|
+
`Text: ${text}`,
|
|
224
|
+
details ? `Details: ${details}` : null,
|
|
225
|
+
document ? `Attached document: ${document.title ? `"${document.title}", ` : ''}${document.markdown.length} characters of Markdown.` : null,
|
|
226
|
+
others.length ? `Other apps it could go to instead: ${appNames(others)}.` : null,
|
|
227
|
+
`send_draft with draft_id ${id} sends it${others.length ? ' (with app set to send it elsewhere)' : ''}. Drafts last two hours.`,
|
|
228
|
+
].filter(Boolean).join('\n'), {
|
|
229
|
+
draft_id: id, sent: false, app: project.name, type: a.type, text, details,
|
|
230
|
+
document: document ? { title: document.title, characters: document.markdown.length } : null,
|
|
231
|
+
other_apps: others.map((p) => ({ id: p.id, name: p.name, platform: p.platform })),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async function sendDraft(a, member, req, deps) {
|
|
235
|
+
const id = str(a.draft_id, 64);
|
|
236
|
+
const d = id ? await deps.store.getAuth('draft', id) : null;
|
|
237
|
+
if (!d || String(d.data.email).toLowerCase() !== member.email.toLowerCase() || Date.parse(d.expires_at ?? '') < deps.now()) {
|
|
238
|
+
throw new ToolError('That draft doesn\'t exist, has expired or was already sent. Make a new one with draft_request.');
|
|
239
|
+
}
|
|
240
|
+
const project = a.app ? await findApp(a.app, deps) : await deps.store.getProject(String(d.data.project_id));
|
|
241
|
+
if (!project)
|
|
242
|
+
throw new ToolError('The draft\'s app was deleted. Name another app.');
|
|
243
|
+
// Once only, however many times it's called.
|
|
244
|
+
if (!(await deps.store.takeAuth('draft', id)))
|
|
245
|
+
throw new ToolError('That draft was already sent.');
|
|
246
|
+
return createChatReport(project, d.data.type, String(d.data.text), d.data.details ?? null, d.data.document ?? null, member, String(d.data.via ?? 'a chat app'), req, deps);
|
|
247
|
+
}
|
|
248
|
+
async function createChatReport(project, type, text, details, document, member, via, req, deps) {
|
|
178
249
|
const now = deps.now();
|
|
179
250
|
for (const l of CHAT_LIMITS) {
|
|
180
251
|
const start = Math.floor(now / l.windowMs) * l.windowMs;
|
|
@@ -185,8 +256,8 @@ async function sendRequest(a, member, via, req, deps) {
|
|
|
185
256
|
const at = new Date(now).toISOString();
|
|
186
257
|
const report = {
|
|
187
258
|
id: `r_${createHash('sha256').update(`${member.email}${now}${Math.random()}`).digest('base64url').slice(0, 16)}`,
|
|
188
|
-
project_id: project.id, device_id: chatDevice(member.email), type
|
|
189
|
-
source: 'chat', from_member: member.email, via: str(via, 80), details,
|
|
259
|
+
project_id: project.id, device_id: chatDevice(member.email), type, text,
|
|
260
|
+
source: 'chat', from_member: member.email, via: str(via, 80), details, document,
|
|
190
261
|
context: {
|
|
191
262
|
route: null, app_version: null, platform: 'chat', os: null, browser: null,
|
|
192
263
|
user_id: null, user_label: null, user_email: null, framework: null,
|
|
@@ -129,6 +129,7 @@ export function agentPrompt(project, t) {
|
|
|
129
129
|
fence,
|
|
130
130
|
t.text.replaceAll(fence, "''"),
|
|
131
131
|
...(t.details ? ['', 'Context from the conversation:', t.details.replaceAll(fence, "''")] : []),
|
|
132
|
+
...(t.document ? ['', `Attached document${t.document.title ? ` "${t.document.title.replaceAll(fence, "''")}"` : ''} (Markdown):`, t.document.markdown.replaceAll(fence, "''")] : []),
|
|
132
133
|
...(t.reporter_note ? ['', `About their setup: ${t.reporter_note.replaceAll(fence, "''")}`] : []),
|
|
133
134
|
...(worst ? ['', `Last thing that went wrong, ${duration(worst.ago)} before ${worst.run === 'last' ? 'the previous run ended' : 'they sent it'}: ${worst.text.replaceAll(fence, "''")}`] : []),
|
|
134
135
|
...(t.trail?.length ? ['', 'What happened in the app before they sent it, recorded by the SDK on their device (oldest first):',
|
|
@@ -585,7 +586,7 @@ async function triage(project, report_id, action, uid, req, deps) {
|
|
|
585
586
|
id: report.id, project_id: project.id, report_id: report.id, type: report.type,
|
|
586
587
|
text: report.text, context: report.context, reporter_note: report.reporter?.note ?? null,
|
|
587
588
|
trail: report.trail ?? null,
|
|
588
|
-
...(report.source === 'chat' ? { source: 'chat', from_member: report.from_member ?? null, via: report.via ?? null, details: report.details ?? null } : {}),
|
|
589
|
+
...(report.source === 'chat' ? { source: 'chat', from_member: report.from_member ?? null, via: report.via ?? null, details: report.details ?? null, document: report.document ?? null } : {}),
|
|
589
590
|
note: str(body.note, LIMITS.reasonMax),
|
|
590
591
|
accepted_by: uid, accepted_at: at, repo: asked ?? repos[0] ?? null, claim: null, notes: [],
|
|
591
592
|
};
|
|
@@ -440,7 +440,26 @@ a.platform:hover { border-color:var(--acc); box-shadow:0 0 0 3px var(--mist); }
|
|
|
440
440
|
.pill.chat-pill { background:#eef2ff; color:#3730a3; }
|
|
441
441
|
.chat-details { margin-top:8px; font-size:14px; }
|
|
442
442
|
.chat-details summary { cursor:pointer; color:var(--mute); font-size:13px; }
|
|
443
|
-
.chat-details
|
|
443
|
+
.chat-details > .md { margin:6px 0 0; padding:10px 14px; border-radius:10px; background:var(--soft, #f5f6f7); }
|
|
444
|
+
.chat-doc > .md { max-height:520px; overflow:auto; }
|
|
445
|
+
/* Markdown from chat apps */
|
|
446
|
+
.md { font-size:14px; line-height:1.55; word-break:break-word; }
|
|
447
|
+
.md > :first-child { margin-top:0; } .md > :last-child { margin-bottom:0; }
|
|
448
|
+
.md p { margin:0 0 8px; }
|
|
449
|
+
.md h3, .md h4, .md h5, .md h6 { margin:14px 0 6px; line-height:1.3; }
|
|
450
|
+
.md h3 { font-size:17px; } .md h4 { font-size:15px; } .md h5, .md h6 { font-size:14px; }
|
|
451
|
+
.md ul, .md ol { margin:0 0 8px; padding-left:22px; }
|
|
452
|
+
.md li { margin:2px 0; } .md li.task { list-style:none; margin-left:-20px; } .md li.task .check { margin-right:6px; }
|
|
453
|
+
.md code { font:12.5px var(--mono, ui-monospace, monospace); padding:1px 5px; border-radius:5px; background:rgba(127,127,127,.14); }
|
|
454
|
+
.md pre.md-code { margin:0 0 8px; padding:10px 12px; border-radius:8px; background:rgba(127,127,127,.12); overflow:auto; }
|
|
455
|
+
.md pre.md-code code { padding:0; background:none; white-space:pre; }
|
|
456
|
+
.md blockquote { margin:0 0 8px; padding:2px 0 2px 12px; border-left:3px solid var(--line); color:var(--mute); }
|
|
457
|
+
.md hr { border:0; border-top:1px solid var(--line); margin:12px 0; }
|
|
458
|
+
.md a { color:var(--deep, #0b5e57); }
|
|
459
|
+
.md-table { overflow:auto; margin:0 0 8px; }
|
|
460
|
+
.md table { border-collapse:collapse; font-size:13px; }
|
|
461
|
+
.md th, .md td { border:1px solid var(--line); padding:4px 8px; text-align:left; vertical-align:top; }
|
|
462
|
+
.report > .md { margin:6px 0 4px; font-size:15px; }
|
|
444
463
|
.howto { margin:0; padding-left:20px; line-height:1.7; }
|
|
445
464
|
.howto li + li { margin-top:4px; }
|
|
446
465
|
.howto .copy-prompt { flex-direction:row; align-items:center; flex-wrap:wrap; }
|
|
@@ -1572,9 +1572,13 @@
|
|
|
1572
1572
|
el('time', { datetime: r.created_at, text: new Date(r.created_at).toLocaleString() }),
|
|
1573
1573
|
status,
|
|
1574
1574
|
]),
|
|
1575
|
-
el('p', { class: 'text', text: r.text }),
|
|
1575
|
+
r.source === 'chat' ? markdown(r.text) : el('p', { class: 'text', text: r.text }),
|
|
1576
1576
|
r.source === 'chat' ? el('div', { class: 'ctx', text: 'From the team: ' + (r.from_member || 'a team member') + ', in a conversation in ' + (r.via || 'a chat app') }) : null,
|
|
1577
|
-
r.details ? el('details', { class: 'chat-details', open: true }, [el('summary', { text: 'From the conversation' }),
|
|
1577
|
+
r.details ? el('details', { class: 'chat-details', open: true }, [el('summary', { text: 'From the conversation' }), markdown(r.details)]) : null,
|
|
1578
|
+
r.document ? el('details', { class: 'chat-details chat-doc' }, [
|
|
1579
|
+
el('summary', { text: 'Attached document' + (r.document.title ? ': ' + r.document.title : '') + ' · ' + (r.document.markdown.length < 1000 ? r.document.markdown.length + ' characters' : Math.round(r.document.markdown.length / 1000) + 'k characters') }),
|
|
1580
|
+
markdown(r.document.markdown),
|
|
1581
|
+
]) : null,
|
|
1578
1582
|
r.source === 'chat' ? null : el('div', { class: 'ctx', text: ctxLine(r.context) }),
|
|
1579
1583
|
r.source !== 'chat' && deviceLine(r.context) ? el('div', { class: 'ctx', text: deviceLine(r.context) }) : null,
|
|
1580
1584
|
r.context.page_url ? el('div', { class: 'ctx' }, [el('a', { href: r.context.page_url, target: '_blank', rel: 'noopener noreferrer', text: r.context.page_url })]) : null,
|
|
@@ -1985,6 +1989,127 @@
|
|
|
1985
1989
|
]);
|
|
1986
1990
|
}
|
|
1987
1991
|
|
|
1992
|
+
// ---- Markdown, for what team members send from chat apps -------------------------------
|
|
1993
|
+
|
|
1994
|
+
/**
|
|
1995
|
+
* A small Markdown renderer: headings, paragraphs, lists (nested by indent), quotes, code
|
|
1996
|
+
* blocks, tables, rules, and inline code, bold, italic, strikethrough and links. It builds
|
|
1997
|
+
* DOM nodes and sets only text, never HTML, so nothing in a request can run or restyle the
|
|
1998
|
+
* page. Links are http(s) or mailto only, and open in a new tab. Used for chat requests from
|
|
1999
|
+
* the team; reports from the public stay plain text.
|
|
2000
|
+
*/
|
|
2001
|
+
function markdown(src) {
|
|
2002
|
+
var root = el('div', { class: 'md' });
|
|
2003
|
+
var lines = String(src || '').replace(/\r\n?/g, '\n').split('\n');
|
|
2004
|
+
var i = 0;
|
|
2005
|
+
var isBlank = function (l) { return !l.trim(); };
|
|
2006
|
+
var startsBlock = function (l) {
|
|
2007
|
+
return /^\s{0,3}(#{1,6}\s|```|~~~|>|[-*+]\s|\d+[.)]\s|(-{3,}|\*{3,}|_{3,})\s*$|\|)/.test(l);
|
|
2008
|
+
};
|
|
2009
|
+
while (i < lines.length) {
|
|
2010
|
+
var line = lines[i];
|
|
2011
|
+
if (isBlank(line)) { i++; continue; }
|
|
2012
|
+
var m;
|
|
2013
|
+
if ((m = /^\s{0,3}(```|~~~)\s*([\w+-]*)\s*$/.exec(line))) {
|
|
2014
|
+
var fence = m[1], code = [];
|
|
2015
|
+
i++;
|
|
2016
|
+
while (i < lines.length && !new RegExp('^\\s{0,3}' + fence + '\\s*$').test(lines[i])) code.push(lines[i++]);
|
|
2017
|
+
i++;
|
|
2018
|
+
root.appendChild(el('pre', { class: 'md-code' }, [el('code', { text: code.join('\n'), 'data-lang': m[2] || null })]));
|
|
2019
|
+
continue;
|
|
2020
|
+
}
|
|
2021
|
+
if ((m = /^\s{0,3}(#{1,6})\s+(.*?)\s*#*\s*$/.exec(line))) {
|
|
2022
|
+
// Shifted down two levels, so a request's "#" never outranks the dashboard's own headings.
|
|
2023
|
+
root.appendChild(inline(el('h' + Math.min(6, m[1].length + 2), {}), m[2]));
|
|
2024
|
+
i++;
|
|
2025
|
+
continue;
|
|
2026
|
+
}
|
|
2027
|
+
if (/^\s{0,3}(-{3,}|\*{3,}|_{3,})\s*$/.test(line)) { root.appendChild(el('hr')); i++; continue; }
|
|
2028
|
+
if (/^\s{0,3}>/.test(line)) {
|
|
2029
|
+
var quote = [];
|
|
2030
|
+
while (i < lines.length && /^\s{0,3}>/.test(lines[i])) quote.push(lines[i++].replace(/^\s{0,3}>\s?/, ''));
|
|
2031
|
+
root.appendChild(el('blockquote', {}, [markdown(quote.join('\n'))]));
|
|
2032
|
+
continue;
|
|
2033
|
+
}
|
|
2034
|
+
if (/^\s*\|.*\|\s*$/.test(line) && i + 1 < lines.length && /^\s*\|?\s*:?-{2,}/.test(lines[i + 1])) {
|
|
2035
|
+
var cells = function (l) { return l.trim().replace(/^\||\|$/g, '').split('|').map(function (c) { return c.trim(); }); };
|
|
2036
|
+
var head = cells(line), body = [];
|
|
2037
|
+
i += 2;
|
|
2038
|
+
while (i < lines.length && /^\s*\|.*\|\s*$/.test(lines[i])) body.push(cells(lines[i++]));
|
|
2039
|
+
root.appendChild(el('div', { class: 'md-table' }, [el('table', {}, [
|
|
2040
|
+
el('thead', {}, [el('tr', {}, head.map(function (c) { return inline(el('th'), c); }))]),
|
|
2041
|
+
el('tbody', {}, body.map(function (r) { return el('tr', {}, r.map(function (c) { return inline(el('td'), c); })); })),
|
|
2042
|
+
])]));
|
|
2043
|
+
continue;
|
|
2044
|
+
}
|
|
2045
|
+
if (/^\s*([-*+]|\d+[.)])\s+/.test(line)) {
|
|
2046
|
+
var block = [];
|
|
2047
|
+
while (i < lines.length && (/^\s*([-*+]|\d+[.)])\s+/.test(lines[i]) || (/^\s{2,}\S/.test(lines[i]) && block.length))) block.push(lines[i++]);
|
|
2048
|
+
root.appendChild(list(block));
|
|
2049
|
+
continue;
|
|
2050
|
+
}
|
|
2051
|
+
var para = [line];
|
|
2052
|
+
i++;
|
|
2053
|
+
while (i < lines.length && !isBlank(lines[i]) && !startsBlock(lines[i])) para.push(lines[i++]);
|
|
2054
|
+
var p = el('p');
|
|
2055
|
+
para.forEach(function (l, k) {
|
|
2056
|
+
if (k) p.appendChild(el('br'));
|
|
2057
|
+
inline(p, l.trim());
|
|
2058
|
+
});
|
|
2059
|
+
root.appendChild(p);
|
|
2060
|
+
}
|
|
2061
|
+
return root;
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
/** List lines into nested <ul>/<ol>, by indent. Task items ("- [x]") get a checkbox mark. */
|
|
2065
|
+
function list(block) {
|
|
2066
|
+
var indent = function (l) { return /^\s*/.exec(l)[0].replace(/\t/g, ' ').length; };
|
|
2067
|
+
var base = indent(block[0]);
|
|
2068
|
+
var ordered = /^\s*\d+[.)]\s/.test(block[0]);
|
|
2069
|
+
var out = el(ordered ? 'ol' : 'ul');
|
|
2070
|
+
var j = 0;
|
|
2071
|
+
while (j < block.length) {
|
|
2072
|
+
var item = block[j].replace(/^\s*([-*+]|\d+[.)])\s+/, '');
|
|
2073
|
+
var li = el('li');
|
|
2074
|
+
var task = /^\[([ xX])\]\s+/.exec(item);
|
|
2075
|
+
if (task) { li.className = 'task'; li.appendChild(el('span', { class: 'check', text: task[1] === ' ' ? '☐' : '☑', 'aria-hidden': 'true' })); item = item.slice(task[0].length); }
|
|
2076
|
+
inline(li, item);
|
|
2077
|
+
j++;
|
|
2078
|
+
var sub = [];
|
|
2079
|
+
while (j < block.length && indent(block[j]) > base) sub.push(block[j++]);
|
|
2080
|
+
if (sub.length) li.appendChild(/^\s*([-*+]|\d+[.)])\s/.test(sub[0]) ? list(sub) : inline(el('div'), sub.join(' ').trim()));
|
|
2081
|
+
out.appendChild(li);
|
|
2082
|
+
}
|
|
2083
|
+
return out;
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
/** Inline Markdown into `node`, as text nodes and elements. Returns `node`. */
|
|
2087
|
+
function inline(node, s) {
|
|
2088
|
+
var re = /(`+)([\s\S]*?)\1|\*\*([^*]+?)\*\*|__([^_]+?)__|~~([^~]+?)~~|\*([^*\s][^*]*?)\*|(^|[^\w])_([^_\s][^_]*?)_(?=[^\w]|$)|\[([^\]]+)\]\(([^)\s]+)\)|(https?:\/\/[^\s<>()]+[^\s<>().,;:!?'"])/g;
|
|
2089
|
+
var at = 0, m;
|
|
2090
|
+
while ((m = re.exec(s))) {
|
|
2091
|
+
var start = m.index + (m[7] !== undefined ? m[7].length : 0);
|
|
2092
|
+
if (start > at) node.appendChild(document.createTextNode(s.slice(at, start)));
|
|
2093
|
+
if (m[1]) node.appendChild(el('code', { text: m[2] }));
|
|
2094
|
+
else if (m[3] || m[4]) node.appendChild(inline(el('strong'), m[3] || m[4]));
|
|
2095
|
+
else if (m[5]) node.appendChild(inline(el('del'), m[5]));
|
|
2096
|
+
else if (m[6]) node.appendChild(inline(el('em'), m[6]));
|
|
2097
|
+
else if (m[8]) node.appendChild(inline(el('em'), m[8]));
|
|
2098
|
+
else if (m[9]) node.appendChild(link(m[10], m[9]));
|
|
2099
|
+
else if (m[11]) node.appendChild(link(m[11], m[11]));
|
|
2100
|
+
at = re.lastIndex;
|
|
2101
|
+
}
|
|
2102
|
+
if (at < s.length) node.appendChild(document.createTextNode(s.slice(at)));
|
|
2103
|
+
return node;
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
/** A link only to the web or an email address; anything else (javascript:, data:) stays text. */
|
|
2107
|
+
function link(href, label) {
|
|
2108
|
+
var ok = /^(https?:\/\/|mailto:)/i.test(href);
|
|
2109
|
+
if (!ok) return document.createTextNode(label === href ? href : label + ' (' + href + ')');
|
|
2110
|
+
return inline(el('a', { href: href, target: '_blank', rel: 'noopener noreferrer nofollow' }), label);
|
|
2111
|
+
}
|
|
2112
|
+
|
|
1988
2113
|
// ---- chat apps -------------------------------------------------------------------------
|
|
1989
2114
|
|
|
1990
2115
|
/**
|