backlog-tiny-mcp 0.34.2__py3-none-any.whl

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.
Files changed (97) hide show
  1. backlog_mcp/__init__.py +29 -0
  2. backlog_mcp/__main__.py +10 -0
  3. backlog_mcp/auth.py +199 -0
  4. backlog_mcp/backlog.py +696 -0
  5. backlog_mcp/cli.py +2046 -0
  6. backlog_mcp/client.py +320 -0
  7. backlog_mcp/computer.py +33 -0
  8. backlog_mcp/database.py +80 -0
  9. backlog_mcp/embeddings.py +900 -0
  10. backlog_mcp/global_rules.md +126 -0
  11. backlog_mcp/guidance.py +11 -0
  12. backlog_mcp/guide/index.md +11 -0
  13. backlog_mcp/guide/memory.md +27 -0
  14. backlog_mcp/guide/projects.md +16 -0
  15. backlog_mcp/guide/search.md +72 -0
  16. backlog_mcp/guide/tasks.md +40 -0
  17. backlog_mcp/guide/workflow.md +22 -0
  18. backlog_mcp/httpd.py +270 -0
  19. backlog_mcp/migrate.py +113 -0
  20. backlog_mcp/migrations/0001-initial.sql +194 -0
  21. backlog_mcp/migrations/0002-task-assignment.sql +2 -0
  22. backlog_mcp/migrations/0003-task-edit-claims.sql +5 -0
  23. backlog_mcp/migrations/0004-memory-visibility.sql +42 -0
  24. backlog_mcp/model/CITATION.bib +9 -0
  25. backlog_mcp/model/LICENSE +202 -0
  26. backlog_mcp/model/README.md +12 -0
  27. backlog_mcp/model/UPSTREAM.md +2053 -0
  28. backlog_mcp/model/config.json +12 -0
  29. backlog_mcp/model/model.safetensors +4 -0
  30. backlog_mcp/model/modules.json +14 -0
  31. backlog_mcp/model/provenance.json +16 -0
  32. backlog_mcp/model/tokenizer.json +1 -0
  33. backlog_mcp/models.py +581 -0
  34. backlog_mcp/names.py +35 -0
  35. backlog_mcp/paths.py +53 -0
  36. backlog_mcp/protocol_storage.py +104 -0
  37. backlog_mcp/queries.py +7 -0
  38. backlog_mcp/queries.sql +531 -0
  39. backlog_mcp/records.py +215 -0
  40. backlog_mcp/render.py +479 -0
  41. backlog_mcp/server.py +2864 -0
  42. backlog_mcp/skills/autopilot/SKILL.md +96 -0
  43. backlog_mcp/skills/planning/SKILL.md +106 -0
  44. backlog_mcp/skills/planning/references/splitting.md +45 -0
  45. backlog_mcp/skills/triage/SKILL.md +104 -0
  46. backlog_mcp/skills/triage/references/conventions.md +43 -0
  47. backlog_mcp/skills.py +56 -0
  48. backlog_mcp/static/agent-config.js +20 -0
  49. backlog_mcp/static/app.css +1950 -0
  50. backlog_mcp/static/app.js +1301 -0
  51. backlog_mcp/static/assignments.js +96 -0
  52. backlog_mcp/static/breadcrumbs.js +54 -0
  53. backlog_mcp/static/comment-composer.js +95 -0
  54. backlog_mcp/static/connect.css +98 -0
  55. backlog_mcp/static/connect.html +74 -0
  56. backlog_mcp/static/connect.js +160 -0
  57. backlog_mcp/static/credentials.js +72 -0
  58. backlog_mcp/static/diagrams.js +128 -0
  59. backlog_mcp/static/dom.js +162 -0
  60. backlog_mcp/static/editor-claim.js +104 -0
  61. backlog_mcp/static/editor.js +355 -0
  62. backlog_mcp/static/index.html +137 -0
  63. backlog_mcp/static/markdown.css +33 -0
  64. backlog_mcp/static/markdown.js +72 -0
  65. backlog_mcp/static/mcp.js +177 -0
  66. backlog_mcp/static/memory-append.js +34 -0
  67. backlog_mcp/static/memory-editor.js +173 -0
  68. backlog_mcp/static/memory.js +279 -0
  69. backlog_mcp/static/package.json +1 -0
  70. backlog_mcp/static/project-form.js +71 -0
  71. backlog_mcp/static/project-settings.js +225 -0
  72. backlog_mcp/static/scope.js +90 -0
  73. backlog_mcp/static/statistics.js +87 -0
  74. backlog_mcp/static/task-browser.js +100 -0
  75. backlog_mcp/static/task-pagination.js +94 -0
  76. backlog_mcp/static/task-picker.js +164 -0
  77. backlog_mcp/static/task-table.js +74 -0
  78. backlog_mcp/static/vendor/README.md +12 -0
  79. backlog_mcp/static/vendor/dompurify-LICENSE +202 -0
  80. backlog_mcp/static/vendor/highlight-LICENSE +29 -0
  81. backlog_mcp/static/vendor/highlight-dark.css +118 -0
  82. backlog_mcp/static/vendor/highlight-light.css +118 -0
  83. backlog_mcp/static/vendor/highlight.min.js +1262 -0
  84. backlog_mcp/static/vendor/marked-LICENSE +44 -0
  85. backlog_mcp/static/vendor/marked.esm.js +80 -0
  86. backlog_mcp/static/vendor/marked.esm.js.map +7 -0
  87. backlog_mcp/static/vendor/mermaid-LICENSE +21 -0
  88. backlog_mcp/static/vendor/mermaid.min.js +4376 -0
  89. backlog_mcp/static/vendor/purify.es.mjs +2722 -0
  90. backlog_mcp/static/vendor/purify.es.mjs.map +1 -0
  91. backlog_mcp/transaction.py +47 -0
  92. backlog_mcp/version.py +79 -0
  93. backlog_mcp/webui.py +63 -0
  94. backlog_tiny_mcp-0.34.2.dist-info/METADATA +84 -0
  95. backlog_tiny_mcp-0.34.2.dist-info/RECORD +97 -0
  96. backlog_tiny_mcp-0.34.2.dist-info/WHEEL +4 -0
  97. backlog_tiny_mcp-0.34.2.dist-info/entry_points.txt +4 -0
@@ -0,0 +1,355 @@
1
+ import { el, button, link, select, field, projectRoute, taskRoute, modal } from "./dom.js";
2
+ import { editClaim } from "./editor-claim.js";
3
+
4
+ const fields = ["title", "priority", "status", "tags", "body"];
5
+ const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
6
+ export function changed(base, values) {
7
+ return Object.fromEntries(
8
+ fields.filter((key) => !equal(base[key], values[key])).map((key) => [key, values[key]]),
9
+ );
10
+ }
11
+ export function editor(task, project, actions) {
12
+ const { call, notify, navigate, markdown, principal } = actions;
13
+ const claim = task ? editClaim(task, actions) : null;
14
+ let ready = Promise.resolve(),
15
+ disposed = false;
16
+
17
+ const key = `backlog:draft:${principal}:${task ? task.id : "new:" + project}`;
18
+ let base = task || { title: "", priority: "normal", status: "todo", tags: [], body: "" };
19
+ let saved;
20
+ try {
21
+ saved = JSON.parse(localStorage.getItem(key));
22
+ } catch {
23
+ saved = null;
24
+ }
25
+ let values = saved?.values || base;
26
+ if (task && saved?.base) base = saved.base;
27
+ const title = el("input", { name: "title", required: true, value: values.title });
28
+ const priority = select(["urgent", "high", "normal", "low"], values.priority, {
29
+ name: "priority",
30
+ });
31
+ const status = select(["todo", "doing", "blocked", "done", "cancelled"], values.status, {
32
+ name: "status",
33
+ });
34
+ const tags = el("input", {
35
+ name: "tags",
36
+ value: (values.tags || []).join(", "),
37
+ placeholder: "Comma-separated tags",
38
+ });
39
+ const body = el("textarea", {
40
+ name: "body",
41
+ id: "task-description",
42
+ value: values.body || "",
43
+ "aria-label": "Description",
44
+ });
45
+ const preview = el("div", { class: "editor-preview markdown", hidden: true });
46
+ const conflict = el("section", { class: "panel conflict", hidden: true });
47
+ const submit = el(
48
+ "button",
49
+ { type: "submit", class: "primary" },
50
+ task ? "Save changes" : "Create task",
51
+ );
52
+ function read() {
53
+ return {
54
+ title: title.value,
55
+ priority: priority.value,
56
+ status: status.value,
57
+ tags: [
58
+ ...new Set(
59
+ tags.value
60
+ .split(",")
61
+ .map((t) => t.trim())
62
+ .filter(Boolean),
63
+ ),
64
+ ],
65
+ body: body.value,
66
+ };
67
+ }
68
+ function persist() {
69
+ try {
70
+ localStorage.setItem(key, JSON.stringify({ base, values: read() }));
71
+ } catch {
72
+ notify(new Error("The browser could not save this draft."));
73
+ }
74
+ }
75
+ function fill(next) {
76
+ title.value = next.title;
77
+ priority.value = next.priority;
78
+ status.value = next.status;
79
+ tags.value = next.tags.join(", ");
80
+ body.value = next.body;
81
+ }
82
+ const form = el(
83
+ "form",
84
+ { class: "editor" },
85
+ link("← " + project, projectRoute(project)),
86
+ el("div", { class: "eyebrow", style: "margin-top:24px" }, task ? `Task #${task.id}` : "Create"),
87
+ el("h1", {}, task ? "Edit task" : "New task"),
88
+ el(
89
+ "p",
90
+ { class: "muted" },
91
+ saved
92
+ ? "Your saved draft has been restored. Changes are saved automatically in this browser."
93
+ : "Your draft is saved automatically in this browser.",
94
+ ),
95
+ el(
96
+ "div",
97
+ { class: "panel" },
98
+ field("Title", title),
99
+ el(
100
+ "div",
101
+ { class: "form-grid" },
102
+ field("Priority", priority),
103
+ field("Status", status),
104
+ field("Tags", tags),
105
+ ),
106
+ el("label", { htmlFor: "task-description" }, "Description"),
107
+ el(
108
+ "div",
109
+ { class: "editor-tabs", "aria-label": "Description view" },
110
+ button("Text", () => {
111
+ body.hidden = false;
112
+ preview.hidden = true;
113
+ updateTabs();
114
+ }),
115
+ button("Preview", () => {
116
+ preview.replaceChildren(markdown(body.value));
117
+ preview.hidden = false;
118
+ body.hidden = true;
119
+ updateTabs();
120
+ }),
121
+ ),
122
+ body,
123
+ preview,
124
+ ),
125
+ conflict,
126
+ el(
127
+ "div",
128
+ { class: "form-actions" },
129
+ submit,
130
+ link("Cancel", task ? taskRoute(task.id) : projectRoute(project), "button"),
131
+ button(
132
+ "Discard draft",
133
+ async () => {
134
+ if (
135
+ await modal({
136
+ title: "Discard draft?",
137
+ description: "Your unsaved changes will be removed from this browser.",
138
+ action: "Discard draft",
139
+ danger: true,
140
+ })
141
+ ) {
142
+ localStorage.removeItem(key);
143
+ navigate(task ? taskRoute(task.id) : projectRoute(project));
144
+ }
145
+ },
146
+ "quiet",
147
+ ),
148
+ ),
149
+ );
150
+ const editFields = form.querySelector(".panel");
151
+ function updateTabs() {
152
+ const tabs = form.querySelectorAll(".editor-tabs button");
153
+ tabs[0].setAttribute("aria-pressed", String(!body.hidden));
154
+ tabs[1].setAttribute("aria-pressed", String(body.hidden));
155
+ }
156
+ updateTabs();
157
+ form.addEventListener("input", persist);
158
+ form.addEventListener("change", persist);
159
+ const differs = (current) =>
160
+ fields.some((key) => !equal(current[key], base[key])) || current.project !== base.project;
161
+ form.mount = () => {
162
+ if (!task) return;
163
+ submit.disabled = true;
164
+ ready = (async () => {
165
+ try {
166
+ const current = await claim.acquire(await call("task_get", { id: task.id }));
167
+ if (!disposed && differs(current)) showConflict(current, read());
168
+ } catch (error) {
169
+ if (!disposed) {
170
+ showConflict(await call("task_get", { id: task.id }), read(), error.message);
171
+ }
172
+ } finally {
173
+ submit.disabled = !conflict.hidden;
174
+ }
175
+ })().catch(notify);
176
+ };
177
+ form.dispose = async () => {
178
+ disposed = true;
179
+ await ready;
180
+ try {
181
+ await claim?.release();
182
+ } catch (error) {
183
+ notify(error);
184
+ }
185
+ };
186
+ async function save() {
187
+ values = read();
188
+ persist();
189
+ submit.disabled = true;
190
+ form.inert = true;
191
+ try {
192
+ if (!task) {
193
+ const created = await call("task_add", { project, ...values });
194
+ localStorage.removeItem(key);
195
+ navigate(taskRoute(created.id));
196
+ return;
197
+ }
198
+ await ready;
199
+ const current = await call("task_get", { id: task.id });
200
+ if (!claim.token || current.assigned !== claim.token || differs(current)) {
201
+ showConflict(current, values);
202
+ return;
203
+ }
204
+ const updates = changed(base, values);
205
+ await call("task_update", {
206
+ id: task.id,
207
+ ...updates,
208
+ expected_assigned: claim.token,
209
+ release_edit: true,
210
+ });
211
+ claim.saved();
212
+ localStorage.removeItem(key);
213
+ navigate(taskRoute(task.id));
214
+ } catch (error) {
215
+ if (!task) notify(error);
216
+ if (task) {
217
+ try {
218
+ showConflict(await call("task_get", { id: task.id }), read(), error.message);
219
+ } catch (readError) {
220
+ notify(readError);
221
+ }
222
+ }
223
+ } finally {
224
+ form.inert = false;
225
+ submit.disabled = !conflict.hidden;
226
+ }
227
+ }
228
+ function showConflict(current, ours, reason = "The saved content or edit claim changed.") {
229
+ conflict.hidden = false;
230
+ editFields.hidden = true;
231
+ submit.disabled = true;
232
+ if (/AssignmentConflict|assignment conflict|expected_assigned/.test(reason))
233
+ reason = "The edit claim changed. Your draft was not saved.";
234
+ else if (/being edited|another browser tab/.test(reason))
235
+ reason = "Another editor has this task open. Your draft is kept; retry after they finish.";
236
+ const serverBody = el("textarea", {
237
+ readOnly: true,
238
+ value: current.body || "",
239
+ "aria-label": "Server Markdown",
240
+ });
241
+ const mergedBody = el("textarea", { value: ours.body || "", "aria-label": "Your Markdown" });
242
+ const serverPreview = el(
243
+ "div",
244
+ { class: "markdown", hidden: true },
245
+ markdown(current.body || ""),
246
+ );
247
+ const oursPreview = el("div", { class: "markdown", hidden: true });
248
+ mergedBody.oninput = () => {
249
+ body.value = mergedBody.value;
250
+ persist();
251
+ };
252
+ const accept = async (useServer) => {
253
+ const controls = [...conflict.querySelectorAll("button")];
254
+ controls.forEach((control) => (control.disabled = true));
255
+ try {
256
+ const latest = await call("task_get", { id: task.id });
257
+ if (
258
+ fields.some((key) => !equal(latest[key], current[key])) ||
259
+ latest.project !== current.project
260
+ ) {
261
+ showConflict(latest, read(), "The server changed again. Review the new version.");
262
+ return;
263
+ }
264
+ const claimed = await claim.acquire(latest);
265
+ if (
266
+ fields.some((key) => !equal(claimed[key], current[key])) ||
267
+ claimed.project !== current.project
268
+ ) {
269
+ showConflict(
270
+ claimed,
271
+ read(),
272
+ "The server changed while acquiring the claim. Review it again.",
273
+ );
274
+ return;
275
+ }
276
+ const updates = { ...changed(base, read()), body: read().body };
277
+ base = claimed;
278
+ fill(useServer ? claimed : { ...claimed, ...updates });
279
+ conflict.hidden = true;
280
+ editFields.hidden = false;
281
+ submit.disabled = false;
282
+ persist();
283
+ body.hidden = false;
284
+ preview.hidden = true;
285
+ updateTabs();
286
+ body.focus();
287
+ } catch (error) {
288
+ notify(error);
289
+ } finally {
290
+ controls.forEach((control) => (control.disabled = false));
291
+ }
292
+ };
293
+ conflict.replaceChildren(
294
+ el("h2", {}, "Resolve editing conflict"),
295
+ el("p", { role: "status" }, reason),
296
+ el(
297
+ "p",
298
+ {},
299
+ "Your draft is kept. Compare the server version with your Markdown, edit your draft, then continue and save.",
300
+ ),
301
+ el(
302
+ "p",
303
+ { class: "muted" },
304
+ `Current assignment: ${current.assigned ? current.assigned.split(":")[0] : "Unassigned"}`,
305
+ ),
306
+ el(
307
+ "div",
308
+ { class: "row" },
309
+ button("Compare text", () => {
310
+ serverBody.hidden = mergedBody.hidden = false;
311
+ serverPreview.hidden = oursPreview.hidden = true;
312
+ }),
313
+ button("Compare preview", () => {
314
+ oursPreview.replaceChildren(markdown(mergedBody.value));
315
+ serverBody.hidden = mergedBody.hidden = true;
316
+ serverPreview.hidden = oursPreview.hidden = false;
317
+ }),
318
+ ),
319
+ el(
320
+ "div",
321
+ { class: "conflict-markdown" },
322
+ el("section", {}, el("h3", {}, "Saved on server"), serverBody, serverPreview),
323
+ el("section", {}, el("h3", {}, "Your draft / merged result"), mergedBody, oursPreview),
324
+ ),
325
+ ...fields
326
+ .filter((key) => key !== "body" && !equal(current[key], ours[key]))
327
+ .map((key) =>
328
+ el(
329
+ "p",
330
+ {},
331
+ `${key}: server ${JSON.stringify(current[key])}; your draft ${JSON.stringify(ours[key])}`,
332
+ ),
333
+ ),
334
+ el(
335
+ "div",
336
+ { class: "row" },
337
+ button("Continue with my draft", () => accept(false)),
338
+ button("Use server version", () => accept(true)),
339
+ button("Refresh server version", async () => {
340
+ try {
341
+ showConflict(await call("task_get", { id: task.id }), read());
342
+ } catch (error) {
343
+ notify(error);
344
+ }
345
+ }),
346
+ ),
347
+ );
348
+ conflict.scrollIntoView({ block: "start", behavior: "smooth" });
349
+ }
350
+ form.onsubmit = (event) => {
351
+ event.preventDefault();
352
+ save();
353
+ };
354
+ return form;
355
+ }
@@ -0,0 +1,137 @@
1
+ <!doctype html>
2
+ <html lang="en" data-endpoint="/mcp" data-starting>
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width,initial-scale=1" />
6
+ <title>Backlog</title>
7
+ <link rel="icon" href="data:," />
8
+ <link rel="stylesheet" href="/ui/static/app.css" />
9
+ <link rel="stylesheet" href="/ui/static/markdown.css" />
10
+ <link
11
+ rel="stylesheet"
12
+ href="/ui/static/vendor/highlight-light.css"
13
+ media="(prefers-color-scheme: light)"
14
+ />
15
+ <link
16
+ rel="stylesheet"
17
+ href="/ui/static/vendor/highlight-dark.css"
18
+ media="(prefers-color-scheme: dark)"
19
+ />
20
+ <script defer src="/ui/static/vendor/highlight.min.js"></script>
21
+ <script type="module" src="/ui/static/app.js"></script>
22
+ </head>
23
+ <body>
24
+ <header inert>
25
+ <a class="brand" href="/ui/">Backlog<span class="brand-dot">.</span></a>
26
+ <nav id="breadcrumbs" aria-label="Breadcrumb" hidden></nav>
27
+ <nav id="section-switch" class="view-switch" aria-label="Sections" hidden>
28
+ <a id="tasks-open" href="/ui/" class="button quiet">Tasks</a>
29
+ <a id="memory-open" href="/ui/?view=memory" class="button quiet">Memory</a>
30
+ </nav>
31
+ <button id="search-open" class="quiet">Search <kbd>⌘ K</kbd></button
32
+ ><a href="/ui/static/connect.html" class="button primary connect-agent">Connect agent</a
33
+ ><span id="principal"></span><button id="logout" class="quiet" hidden>Sign out</button>
34
+ </header>
35
+ <main id="content" aria-live="polite" aria-busy="true">
36
+ <section class="initial-loading" aria-label="Loading page">
37
+ <p role="status">Loading Backlog…</p>
38
+ <div class="initial-placeholder" aria-hidden="true">
39
+ <div class="placeholder-title"></div>
40
+ <div class="placeholder-line"></div>
41
+ <div class="placeholder-panel"></div>
42
+ </div>
43
+ </section>
44
+ </main>
45
+ <footer class="mobile-footer">
46
+ <a href="/ui/static/connect.html">Connect agent</a>
47
+ </footer>
48
+ <div id="page-loading" role="status" hidden>Loading…</div>
49
+ <div id="toast" role="alert" hidden></div>
50
+ <dialog id="search-dialog" aria-label="Search">
51
+ <div class="row">
52
+ <input id="search-query" placeholder="Search backlog" aria-label="Search query" /><button
53
+ id="search-close"
54
+ class="quiet"
55
+ >
56
+ Close
57
+ </button>
58
+ </div>
59
+ <details class="search-history">
60
+ <summary>Recent searches</summary>
61
+ <div id="search-history-list"></div>
62
+ <button id="search-history-clear" class="quiet" type="button">Clear history</button>
63
+ </details>
64
+ <details class="search-help">
65
+ <summary>query syntax</summary>
66
+ <div class="search-help-content markdown" aria-live="polite"></div>
67
+ </details>
68
+ <fieldset class="search-kinds">
69
+ <legend>Search in:</legend>
70
+ <label class="inline"><input id="search-all" type="checkbox" />All projects</label>
71
+ <label class="inline"
72
+ ><input type="checkbox" name="search-kind" value="tasks" checked />Tasks</label
73
+ >
74
+ <label class="inline"
75
+ ><input type="checkbox" name="search-kind" value="comments" checked />Comments</label
76
+ >
77
+ <label class="inline"
78
+ ><input type="checkbox" name="search-kind" value="memory" checked />Memory</label
79
+ >
80
+ <label class="inline"
81
+ ><input type="checkbox" name="search-kind" value="projects" checked />Projects</label
82
+ >
83
+ </fieldset>
84
+ <details class="search-advanced">
85
+ <summary>Filters</summary>
86
+ <div class="search-options">
87
+ <label
88
+ >Task status<select id="search-status">
89
+ <option value="">Any status</option>
90
+ <option>todo</option>
91
+ <option>doing</option>
92
+ <option>blocked</option>
93
+ <option>done</option>
94
+ <option>cancelled</option>
95
+ <option>deleted</option>
96
+ </select></label
97
+ >
98
+ <label>Tag<input id="search-tag" placeholder="Any tag" /></label>
99
+ <label
100
+ >Computer scope<select id="search-scope">
101
+ <option value="all">All computers</option>
102
+ <option value="current">This computer</option>
103
+ <option value="computer">By name</option>
104
+ </select></label
105
+ >
106
+ <label id="search-computer-field" hidden
107
+ >Computer<input id="search-computer" placeholder="Computer name"
108
+ /></label>
109
+ <label
110
+ >Results per type<input
111
+ id="search-limit"
112
+ type="number"
113
+ min="1"
114
+ max="200"
115
+ step="1"
116
+ value="15"
117
+ required
118
+ /></label>
119
+ <label
120
+ >Minimum similarity<input
121
+ id="search-score"
122
+ type="number"
123
+ min="-1"
124
+ max="1"
125
+ step="any"
126
+ value="0.12"
127
+ required
128
+ /></label>
129
+ </div>
130
+ <p class="muted">
131
+ Status applies only to tasks. Computer scope applies when All projects is selected.
132
+ </p>
133
+ </details>
134
+ <div id="search-results"></div>
135
+ </dialog>
136
+ </body>
137
+ </html>
@@ -0,0 +1,33 @@
1
+ .markdown pre code.hljs {
2
+ padding: 0;
3
+ background: transparent;
4
+ }
5
+ backlog-diagram {
6
+ display: block;
7
+ min-width: 0;
8
+ margin: 1.25rem 0;
9
+ }
10
+ .diagram-output {
11
+ overflow: auto;
12
+ padding: 16px;
13
+ border: 1px solid var(--line);
14
+ border-radius: 8px;
15
+ background: var(--surface);
16
+ }
17
+ .markdown .diagram-image {
18
+ display: block;
19
+ max-width: none;
20
+ height: auto;
21
+ margin: auto;
22
+ }
23
+ .diagram-source {
24
+ margin-top: 8px;
25
+ color: var(--muted);
26
+ font-size: 12px;
27
+ }
28
+ .diagram-source summary {
29
+ cursor: pointer;
30
+ }
31
+ .diagram-source pre {
32
+ color: var(--text);
33
+ }
@@ -0,0 +1,72 @@
1
+ import { taskRoute, noteRoute } from "./dom.js";
2
+ import { marked } from "./vendor/marked.esm.js";
3
+ import DOMPurify from "./vendor/purify.es.mjs";
4
+ import "./diagrams.js";
5
+
6
+ export function markdown(source = "") {
7
+ const fragment = DOMPurify.sanitize(marked.parse(source, { gfm: true, async: false }), {
8
+ RETURN_DOM_FRAGMENT: true,
9
+ USE_PROFILES: { html: true },
10
+ FORBID_TAGS: ["style", "form", "iframe", "object"],
11
+ FORBID_ATTR: ["style"],
12
+ });
13
+ const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT);
14
+ const nodes = [];
15
+ while (walker.nextNode()) {
16
+ if (!walker.currentNode.parentElement?.closest("a,code,pre")) nodes.push(walker.currentNode);
17
+ }
18
+ for (const node of nodes) {
19
+ const pattern = /\B#(note)?([1-9]\d*)\b/g;
20
+ let last = 0,
21
+ match;
22
+ const replacement = document.createDocumentFragment();
23
+ while ((match = pattern.exec(node.textContent))) {
24
+ replacement.append(node.textContent.slice(last, match.index));
25
+ const link = document.createElement("a");
26
+ link.href = match[1] ? noteRoute(match[2]) : taskRoute(match[2]);
27
+ link.textContent = match[0];
28
+ replacement.append(link);
29
+ last = match.index + match[0].length;
30
+ }
31
+ if (last) {
32
+ replacement.append(node.textContent.slice(last));
33
+ node.replaceWith(replacement);
34
+ }
35
+ }
36
+ for (const link of fragment.querySelectorAll("a[href]")) {
37
+ const href = link.getAttribute("href");
38
+ const task = href.match(/^#(?:\/t\/)?([1-9]\d*)$/);
39
+ const note = href.match(/^#note([1-9]\d*)$/);
40
+ if (note) link.href = noteRoute(note[1]);
41
+ else if (task) link.href = taskRoute(task[1]);
42
+ else if (!href.startsWith("/ui/#")) {
43
+ link.target = "_blank";
44
+ link.rel = "noopener noreferrer";
45
+ }
46
+ }
47
+ for (const code of fragment.querySelectorAll("pre > code")) {
48
+ const language = [...code.classList]
49
+ .find((name) => name.startsWith("language-"))
50
+ ?.slice(9)
51
+ .toLowerCase();
52
+ if (language === "mermaid") {
53
+ const diagram = document.createElement("backlog-diagram");
54
+ diagram.source = code.textContent;
55
+ code.parentElement.replaceWith(diagram);
56
+ } else if (language && globalThis.hljs?.getLanguage(language)) {
57
+ const html = globalThis.hljs.highlight(code.textContent, {
58
+ language,
59
+ ignoreIllegals: true,
60
+ }).value;
61
+ code.replaceChildren(
62
+ DOMPurify.sanitize(html, {
63
+ RETURN_DOM_FRAGMENT: true,
64
+ ALLOWED_TAGS: ["span"],
65
+ ALLOWED_ATTR: ["class"],
66
+ }),
67
+ );
68
+ code.classList.add("hljs");
69
+ }
70
+ }
71
+ return fragment;
72
+ }