sideora 0.0.1-alpha.0 → 0.1.0-alpha.2
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/LICENSE.md +9 -0
- package/README.md +57 -6
- package/assets/sounds/freesound_community-mail-40275.mp3 +0 -0
- package/assets/sounds/mixkit-clear-announce-tones-2861.wav +0 -0
- package/assets/sounds/mixkit-happy-bells-notification-937.wav +0 -0
- package/assets/sounds/mixkit-musical-alert-notification-2309.wav +0 -0
- package/assets/sounds/nice-chime.mp3 +0 -0
- package/bin/sideora.js +2430 -12
- package/extension/dist/assets/background.ts-NW8YqAlO.js +1 -0
- package/extension/dist/assets/fillCode-Ci8IkBvY.js +1 -0
- package/extension/dist/assets/index.html-DrrPha1M.js +2 -0
- package/extension/dist/assets/modulepreload-polyfill-DcjICKwh.js +1 -0
- package/extension/dist/assets/shell-82MNMljp.css +1 -0
- package/extension/dist/assets/shell-QYBN1bx1.js +1 -0
- package/extension/dist/icons/icon-128.png +0 -0
- package/extension/dist/icons/icon-16.png +0 -0
- package/extension/dist/icons/icon-32.png +0 -0
- package/extension/dist/icons/icon-48.png +0 -0
- package/extension/dist/manifest.json +40 -0
- package/extension/dist/service-worker-loader.js +1 -0
- package/extension/dist/src/newtab/index.html +14 -0
- package/extension/dist/src/panel/index.html +15 -0
- package/extension/dist-web/assets/abnfDiagram-WEYOIA3C-CoXOc872.js +1 -0
- package/extension/dist-web/assets/apple-touch-icon-DSgRQASq.png +0 -0
- package/extension/dist-web/assets/architecture-WOLXFQ4H-BkJ4AerO.js +1 -0
- package/extension/dist-web/assets/architectureDiagram-ZHVYKT53-aPkSDlxA.js +36 -0
- package/extension/dist-web/assets/blockDiagram-RJAE7QA3-CH1G4NBZ.js +140 -0
- package/extension/dist-web/assets/c4Diagram-XDPPPE54-Baum1zDu.js +38 -0
- package/extension/dist-web/assets/channel-PsQg-mnK.js +1 -0
- package/extension/dist-web/assets/chunk-2E7WHVMD-CKvLrWRa.js +88 -0
- package/extension/dist-web/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/extension/dist-web/assets/chunk-4EMNKR2T-6fjnroCZ.js +1 -0
- package/extension/dist-web/assets/chunk-55XCU76K-Ygs3sNpi.js +1 -0
- package/extension/dist-web/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/extension/dist-web/assets/chunk-7S6XSTFO-iX5xYzfP.js +2 -0
- package/extension/dist-web/assets/chunk-AQDTSKZB-D9zwhFI8.js +72 -0
- package/extension/dist-web/assets/chunk-DSNLSHJZ-siGS-4qu.js +129 -0
- package/extension/dist-web/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/extension/dist-web/assets/chunk-HAGFOTED-_3dtnWXn.js +62 -0
- package/extension/dist-web/assets/chunk-HNE4Q36P-BPzRHi72.js +1 -0
- package/extension/dist-web/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/extension/dist-web/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/extension/dist-web/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/extension/dist-web/assets/chunk-NQOKRVIF-AXbmOBjs.js +213 -0
- package/extension/dist-web/assets/chunk-UHW7PFBP-B3xrZ8sD.js +1 -0
- package/extension/dist-web/assets/chunk-VQ5W62QI-D_k7Pa6b.js +1 -0
- package/extension/dist-web/assets/chunk-WQQL4CDK-BBIrWwoZ.js +10 -0
- package/extension/dist-web/assets/chunk-XXDRQBXY-i7MKP1NY.js +1 -0
- package/extension/dist-web/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/extension/dist-web/assets/chunk-YZO67HFG-Z7qp5QAI.js +1 -0
- package/extension/dist-web/assets/chunk-ZAUX2LTX-CSt3c0Rb.js +1 -0
- package/extension/dist-web/assets/chunk-ZCHMQSOR-g8XRABGK.js +2 -0
- package/extension/dist-web/assets/classDiagram-v2-4MYWR3UA-BL706wzc.js +217 -0
- package/extension/dist-web/assets/cose-bilkent-JH36ORCC-C6J2IoHg.js +1 -0
- package/extension/dist-web/assets/cynefin-EF2NZ3EQ-DLhQOiAy.js +1 -0
- package/extension/dist-web/assets/cynefinDiagram-WF24UDRP-Cv9Lx6_l.js +62 -0
- package/extension/dist-web/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/extension/dist-web/assets/d3-scale-CElPUUud.js +1 -0
- package/extension/dist-web/assets/d3-selection-COWvaQNI.js +1 -0
- package/extension/dist-web/assets/dagre-NK4GIZVX-ngM-w2EC.js +4 -0
- package/extension/dist-web/assets/diagram-CBK3MWTC-kp8sayNG.js +24 -0
- package/extension/dist-web/assets/diagram-I5XIIV2D-BZ_Dgs6K.js +24 -0
- package/extension/dist-web/assets/diagram-ODIL2UCW-DWhUyArG.js +30 -0
- package/extension/dist-web/assets/diagram-PFBEJUXE-CBAdEK7l.js +3 -0
- package/extension/dist-web/assets/diagram-RJA3I4VV-kOdIr_3K.js +41 -0
- package/extension/dist-web/assets/diagram-TYUNFSNR-BrWGa0x5.js +200 -0
- package/extension/dist-web/assets/dist-DdXDFcej.js +1 -0
- package/extension/dist-web/assets/docx-preview-D1zbEiLy.js +20 -0
- package/extension/dist-web/assets/earth-Ce3xs8Cb.webp +0 -0
- package/extension/dist-web/assets/ebnfDiagram-V4P3TTPT-0r3weWmj.js +1 -0
- package/extension/dist-web/assets/elk-QONTBCTQ-D3oElRZ3.js +27 -0
- package/extension/dist-web/assets/erDiagram-KYQIGQAA-B9oU7_j_.js +99 -0
- package/extension/dist-web/assets/eventmodeling-K75KTNOO-CMKgc1nP.js +1 -0
- package/extension/dist-web/assets/favicon-D_X6tUVf.svg +5 -0
- package/extension/dist-web/assets/flowDiagram-IUGLLWBU-yRwks0Mw.js +1 -0
- package/extension/dist-web/assets/ganttDiagram-R7GPNTJY-cJqD1qwQ.js +292 -0
- package/extension/dist-web/assets/gitGraph-VSP46ZUC-CuBQqkbr.js +1 -0
- package/extension/dist-web/assets/gitGraphDiagram-PDYL7XHY-D7bHnQXC.js +106 -0
- package/extension/dist-web/assets/graphlib-DS17s2tU.js +1 -0
- package/extension/dist-web/assets/home-CV7-8Trs.js +1 -0
- package/extension/dist-web/assets/index-CQvw7Wy_.js +7 -0
- package/extension/dist-web/assets/index-CVKnZ21i.css +1 -0
- package/extension/dist-web/assets/info-OHQRW6UA-B8kyzV4O.js +1 -0
- package/extension/dist-web/assets/infoDiagram-MVDRKAX5-CEm6lezX.js +2 -0
- package/extension/dist-web/assets/ishikawaDiagram-HPEHV2GE-Ce3uv2wr.js +70 -0
- package/extension/dist-web/assets/journeyDiagram-BOSHW7O7-QgdahHCP.js +139 -0
- package/extension/dist-web/assets/jszip.min-3XXygzvy.js +2 -0
- package/extension/dist-web/assets/kanban-definition-FNSA4HC5-DPuTtWgR.js +89 -0
- package/extension/dist-web/assets/katex-MEuSR36T.js +257 -0
- package/extension/dist-web/assets/mermaid-parser.core-h7U7YGn_.js +7 -0
- package/extension/dist-web/assets/mermaid.core-DkGhophj.js +44 -0
- package/extension/dist-web/assets/mindmap-definition-6OY57UG3-D7VQxbda.js +98 -0
- package/extension/dist-web/assets/mount-CBzzf6KQ.js +195 -0
- package/extension/dist-web/assets/mount-DtvYnQXt.css +1 -0
- package/extension/dist-web/assets/officeparser.browser.slim-kEjhTrQ1.js +1989 -0
- package/extension/dist-web/assets/packet-JDAUHWVQ-oafHgw6X.js +1 -0
- package/extension/dist-web/assets/pegDiagram-ECIUJNBX-C3zo1naj.js +1 -0
- package/extension/dist-web/assets/peoria-B5TBlKDG.jpg +0 -0
- package/extension/dist-web/assets/peoria-thumb-CN8upAdZ.jpg +0 -0
- package/extension/dist-web/assets/pie-XZMESJXO-DXzeBQqF.js +1 -0
- package/extension/dist-web/assets/pieDiagram-GNMPY74T-S5NzPthl.js +39 -0
- package/extension/dist-web/assets/quadrantDiagram-D57K4MC3-C9glHQmk.js +7 -0
- package/extension/dist-web/assets/radar-ABXABTNO-CWTNqtbu.js +1 -0
- package/extension/dist-web/assets/railroad-I3PHUGI6-BTpD5_Bu.js +1 -0
- package/extension/dist-web/assets/railroad-abnf-LNEFI6M7-bWyxoXQG.js +1 -0
- package/extension/dist-web/assets/railroad-ebnf-DWYD2UWJ-ri5733DQ.js +1 -0
- package/extension/dist-web/assets/railroad-peg-JR7BVNR7-CmGfTubP.js +1 -0
- package/extension/dist-web/assets/railroadDiagram-Y72NRBL7-DTfe6Dh1.js +1 -0
- package/extension/dist-web/assets/requirementDiagram-EQYSW5RA-DQDzcS3L.js +84 -0
- package/extension/dist-web/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/extension/dist-web/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/extension/dist-web/assets/sankeyDiagram-SY24JHO5-DyMKWxdB.js +40 -0
- package/extension/dist-web/assets/sequenceDiagram-BLEHNHQU-dIkB1mCn.js +169 -0
- package/extension/dist-web/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/extension/dist-web/assets/stateDiagram-v2-A6IESKWZ-Bj_vxEVF.js +291 -0
- package/extension/dist-web/assets/swimlanes-B3ENRKFQ-BSkWEwHR.js +1 -0
- package/extension/dist-web/assets/swimlanesDiagram-AYQRV4AK-BTzSDlSM.js +8 -0
- package/extension/dist-web/assets/timeline-definition-JXYKGLGB-TwfWS0pw.js +120 -0
- package/extension/dist-web/assets/treeView-D4JQ5SDB-BzA_8YYb.js +1 -0
- package/extension/dist-web/assets/treemap-SAJKECNS-BI9d6--2.js +1 -0
- package/extension/dist-web/assets/usecaseDiagram-WPOLBNNX-BOVbAUPk.js +328 -0
- package/extension/dist-web/assets/vennDiagram-Q3Y3IDCW-X2kYAWaj.js +34 -0
- package/extension/dist-web/assets/wardley-7MLQ67FV-CItxqJ8G.js +1 -0
- package/extension/dist-web/assets/wardleyDiagram-IVAIDKWA-BnUXvKD_.js +78 -0
- package/extension/dist-web/assets/xychartDiagram-4URNHDU4-ysvBXvyE.js +7 -0
- package/extension/dist-web/icon-192.png +0 -0
- package/extension/dist-web/icon-512.png +0 -0
- package/extension/dist-web/index.html +25 -0
- package/extension/dist-web/manifest.webmanifest +14 -0
- package/package.json +64 -12
- package/plugin/.claude-plugin/icon.png +0 -0
- package/plugin/.claude-plugin/plugin.json +6 -0
- package/plugin/.mcp.json +8 -0
- package/plugin/README.md +72 -0
- package/plugin/mcp-launcher.mjs +48 -0
- package/src/account-probe.js +45 -0
- package/src/accounts.js +687 -0
- package/src/ai.js +269 -0
- package/src/assistant-runs.js +32 -0
- package/src/attached-mail.js +76 -0
- package/src/auth-gate.js +70 -0
- package/src/backup-cli.js +158 -0
- package/src/backup.js +329 -0
- package/src/bimi.js +233 -0
- package/src/bridge.js +101 -0
- package/src/calendar-alerts.js +56 -0
- package/src/chats.js +27 -0
- package/src/compose-editor.js +58 -0
- package/src/config.js +420 -0
- package/src/connections.d.ts +24 -0
- package/src/connections.js +37 -0
- package/src/core/agent.js +92 -0
- package/src/core/assistant.d.ts +13 -0
- package/src/core/assistant.js +790 -0
- package/src/core/chats.js +323 -0
- package/src/core/markdown.d.ts +8 -0
- package/src/core/markdown.js +239 -0
- package/src/core/notes.js +445 -0
- package/src/core/otp.js +152 -0
- package/src/core/rules.js +196 -0
- package/src/core/runs.d.ts +9 -0
- package/src/core/runs.js +211 -0
- package/src/core/thread.js +526 -0
- package/src/core/write.js +247 -0
- package/src/core-db.js +131 -0
- package/src/core.js +174 -0
- package/src/credentials.js +320 -0
- package/src/daemon.js +564 -0
- package/src/events.js +213 -0
- package/src/extension-id.d.ts +2 -0
- package/src/extension-id.js +5 -0
- package/src/file-lock.js +27 -0
- package/src/files-index.js +154 -0
- package/src/google-contacts.js +315 -0
- package/src/google-signin.js +306 -0
- package/src/host/assistant-html.js +44 -0
- package/src/host/chats-files.js +137 -0
- package/src/host/file-prep.js +122 -0
- package/src/host/notes-files.js +158 -0
- package/src/host/office-text.js +224 -0
- package/src/host/office-worker.js +8 -0
- package/src/host/rules-file.js +20 -0
- package/src/host/secrets-file.js +91 -0
- package/src/host/systemd.js +90 -0
- package/src/hub.js +245 -0
- package/src/identity.js +316 -0
- package/src/labels.js +5 -0
- package/src/mail-port.d.ts +79 -0
- package/src/mail-port.js +148 -0
- package/src/mcp-server.js +747 -0
- package/src/microsoft-app.js +20 -0
- package/src/microsoft-signin.js +194 -0
- package/src/migrate.js +327 -0
- package/src/notes-html.js +450 -0
- package/src/notes.js +19 -0
- package/src/notify.js +105 -0
- package/src/onepassword.js +223 -0
- package/src/op-registry.d.ts +14 -0
- package/src/op-registry.js +150 -0
- package/src/ops.js +1251 -0
- package/src/otp.js +6 -0
- package/src/panel-ops.js +273 -0
- package/src/pictures.js +735 -0
- package/src/postinstall.js +25 -0
- package/src/relay-passkey.js +122 -0
- package/src/relay-request.js +155 -0
- package/src/relay-seal.js +56 -0
- package/src/relay-settings.js +139 -0
- package/src/relay.js +876 -0
- package/src/render.js +82 -0
- package/src/rules-store.js +8 -0
- package/src/safe-fetch.js +215 -0
- package/src/sender-auth.js +270 -0
- package/src/site-match.js +77 -0
- package/src/tui-agenda.js +266 -0
- package/src/tui.js +670 -0
- package/src/vmc.js +376 -0
- package/src/wake.js +96 -0
- package/src/web-access.js +185 -0
- package/src/web-server.js +241 -0
- package/src/web-service.js +152 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
// What the notes store needs to know of a note's text without an editor: its
|
|
2
|
+
// words, its to-dos, and the two changes made to it from outside the editor
|
|
3
|
+
// (a to-do added at its end, a to-do ticked). The text is the HTML the side
|
|
4
|
+
// panel's editor writes (well-formed: a browser wrote it), and a to-do in it is
|
|
5
|
+
//
|
|
6
|
+
// <ul data-type="taskList">
|
|
7
|
+
// <li data-type="taskItem" data-checked="false" data-id="k3f9a2c1"><label>…</label><div><p>Call Ann</p></div></li>
|
|
8
|
+
// </ul>
|
|
9
|
+
//
|
|
10
|
+
// Text written by hand or by a tool may be less regular: everything here then
|
|
11
|
+
// does less (a to-do it cannot find is not ticked, one it cannot place starts a
|
|
12
|
+
// list of its own), never something else. The text is read once, from its
|
|
13
|
+
// start to its end, whatever it holds: a quote never closed ends the reading
|
|
14
|
+
// there, it does not start it again.
|
|
15
|
+
|
|
16
|
+
const VOID = new Set(['br', 'hr', 'img', 'input', 'col', 'wbr', 'meta', 'link', 'area', 'base', 'embed', 'source', 'track']);
|
|
17
|
+
const BLOCK = /<\/(p|div|li|h[1-6]|tr|blockquote|pre)>|<br\b[^>]*>/gi;
|
|
18
|
+
const NAME = /[a-zA-Z]/;
|
|
19
|
+
const NAME_END = /[\s/>]/;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The tags of a text, in order, read as a browser reads them (a > inside a
|
|
23
|
+
* quoted value does not end one): { close, name, attrs, index, end }. A tag
|
|
24
|
+
* that never ends (a quote left open) is where the reading stops.
|
|
25
|
+
*/
|
|
26
|
+
function* tags(text) {
|
|
27
|
+
let at = 0;
|
|
28
|
+
for (;;) {
|
|
29
|
+
const lt = text.indexOf('<', at);
|
|
30
|
+
if (lt < 0) return;
|
|
31
|
+
let i = lt + 1;
|
|
32
|
+
const close = text[i] === '/';
|
|
33
|
+
if (close) i++;
|
|
34
|
+
if (!NAME.test(text[i] ?? '')) { at = lt + 1; continue; } // a < that opens nothing (a comparison, a comment)
|
|
35
|
+
const from = i;
|
|
36
|
+
while (i < text.length && !NAME_END.test(text[i])) i++;
|
|
37
|
+
const name = text.slice(from, i).toLowerCase();
|
|
38
|
+
const attrsFrom = i;
|
|
39
|
+
let quote = '';
|
|
40
|
+
for (; i < text.length; i++) {
|
|
41
|
+
const c = text[i];
|
|
42
|
+
if (quote) { if (c === quote) quote = ''; } else if (c === '"' || c === "'") quote = c;
|
|
43
|
+
else if (c === '>') break;
|
|
44
|
+
}
|
|
45
|
+
if (i >= text.length) return;
|
|
46
|
+
yield { close, name, attrs: text.slice(attrsFrom, i), index: lt, end: i + 1 };
|
|
47
|
+
at = i + 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
|
|
52
|
+
const decode = (s) => s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e) => {
|
|
53
|
+
if (e[0] !== '#') return ENTITIES[e.toLowerCase()] ?? m;
|
|
54
|
+
const n = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
|
55
|
+
try { return Number.isFinite(n) ? String.fromCodePoint(n) : m; } catch { return m; }
|
|
56
|
+
});
|
|
57
|
+
export const escapeHtml = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
58
|
+
|
|
59
|
+
/** The words of a piece of HTML, on one line. */
|
|
60
|
+
export function plainText(html) {
|
|
61
|
+
const text = String(html || '').replace(/<!--[^]*?(?:-->|$)/g, '').replace(BLOCK, ' ');
|
|
62
|
+
let out = '';
|
|
63
|
+
let at = 0;
|
|
64
|
+
for (const t of tags(text)) { out += text.slice(at, t.index); at = t.end; }
|
|
65
|
+
return decode(out + text.slice(at)).replace(/\s+/g, ' ').trim();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// the attributes of a tag, one at a time: what stands inside another's quoted value is not one
|
|
69
|
+
const ATTR = /\s+([^\s=>"'/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g;
|
|
70
|
+
const attrsOf = (attrs) => [...String(attrs).matchAll(ATTR)].map((m) => ({ name: m[1].toLowerCase(), value: m[2] ?? m[3] ?? m[4] ?? '', at: m.index, raw: m[0] }));
|
|
71
|
+
const attr = (attrs, name) => {
|
|
72
|
+
const found = attrsOf(attrs).find((a) => a.name === name);
|
|
73
|
+
return found ? decode(found.value) : undefined;
|
|
74
|
+
};
|
|
75
|
+
/** The attributes with one of them set (added at the end when it was not there); the others as they were written. */
|
|
76
|
+
function withAttr(attrs, name, value) {
|
|
77
|
+
const found = attrsOf(attrs).find((a) => a.name === name);
|
|
78
|
+
if (!found) return `${attrs} ${name}="${value}"`;
|
|
79
|
+
return `${attrs.slice(0, found.at)} ${name}="${value}"${attrs.slice(found.at + found.raw.length)}`;
|
|
80
|
+
}
|
|
81
|
+
const isTask = (attrs) => attr(attrs, 'data-type') === 'taskItem';
|
|
82
|
+
const isTaskList = (attrs) => attr(attrs, 'data-type') === 'taskList';
|
|
83
|
+
|
|
84
|
+
/** The to-dos of a note, in the order written: [{ id, checked, text }]. One without an id (written by hand) has ''. */
|
|
85
|
+
export function todoItems(html) {
|
|
86
|
+
const out = [];
|
|
87
|
+
const open = []; // the <li> we are inside of, outermost first
|
|
88
|
+
const text = String(html || '');
|
|
89
|
+
for (const t of tags(text)) {
|
|
90
|
+
if (t.name === 'li' && !t.close) {
|
|
91
|
+
const item = isTask(t.attrs) ? { id: attr(t.attrs, 'data-id') || '', checked: attr(t.attrs, 'data-checked') === 'true', text: '' } : null;
|
|
92
|
+
if (item) out.push(item);
|
|
93
|
+
open.push({ item, from: t.end, cut: undefined });
|
|
94
|
+
} else if (t.name === 'li' && t.close) {
|
|
95
|
+
const li = open.pop();
|
|
96
|
+
if (li?.item) li.item.text = plainText(text.slice(li.from, li.cut ?? t.index));
|
|
97
|
+
} else if ((t.name === 'ul' || t.name === 'ol') && !t.close && open.length) {
|
|
98
|
+
const li = open[open.length - 1];
|
|
99
|
+
if (li.cut === undefined) li.cut = t.index; // a list inside a to-do is not its words
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The note with one to-do ticked or cleared; null when it has no to-do of that id. */
|
|
106
|
+
export function setChecked(html, id, checked) {
|
|
107
|
+
if (!id) return null;
|
|
108
|
+
const text = String(html || '');
|
|
109
|
+
const all = [...tags(text)];
|
|
110
|
+
const at = all.findIndex((t) => !t.close && t.name === 'li' && isTask(t.attrs) && attr(t.attrs, 'data-id') === id);
|
|
111
|
+
if (at < 0) return null;
|
|
112
|
+
const li = all[at];
|
|
113
|
+
const tag = `<li${withAttr(li.attrs, 'data-checked', checked ? 'true' : 'false')}>`;
|
|
114
|
+
// the box the editor draws says the same (it reads data-checked; a browser shown the text reads the box): the
|
|
115
|
+
// <input> that follows, inside its <label>, when that is what follows
|
|
116
|
+
const label = all[at + 1];
|
|
117
|
+
const box = all[at + 2];
|
|
118
|
+
if (label && box && !label.close && label.name === 'label' && !box.close && box.name === 'input' && !text.slice(li.end, label.index).trim() && !text.slice(label.end, box.index).trim()) {
|
|
119
|
+
return text.slice(0, li.index) + tag + text.slice(li.end, box.index) + `<input type="checkbox"${checked ? ' checked="checked"' : ''}>` + text.slice(box.end);
|
|
120
|
+
}
|
|
121
|
+
return text.slice(0, li.index) + tag + text.slice(li.end);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A to-do, as the editor writes one. */
|
|
125
|
+
export const todoHtml = ({ id, text, checked = false }) =>
|
|
126
|
+
`<li data-type="taskItem" data-checked="${checked ? 'true' : 'false'}" data-id="${escapeHtml(id)}"><label><input type="checkbox"${checked ? ' checked="checked"' : ''}><span></span></label><div><p>${escapeHtml(text)}</p></div></li>`;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The text without the empty lines it ends with: the editor keeps one after a
|
|
130
|
+
* list or a table (to write on from), which is not writing. What is added to
|
|
131
|
+
* a note goes before it: the same rule in the editor (Editor.tsx, applyOp) and
|
|
132
|
+
* for a text no editor holds (panel/lib/noteHtml.ts).
|
|
133
|
+
*/
|
|
134
|
+
export const withoutEmptyEnd = (html) => String(html || '').replace(/(?:<p><\/p>)+$/, ''); // empty: nothing in it at all, as the editor counts it
|
|
135
|
+
|
|
136
|
+
/** The note with a to-do added at its end: in the list of to-dos it ends with, or in a new one. */
|
|
137
|
+
export function appendTodo(html, todo) {
|
|
138
|
+
const text = withoutEmptyEnd(html);
|
|
139
|
+
const item = todoHtml(todo);
|
|
140
|
+
let depth = 0;
|
|
141
|
+
let last = null; // the last element of the note itself: { list, close, end }
|
|
142
|
+
for (const t of tags(text)) {
|
|
143
|
+
if (VOID.has(t.name)) { if (depth === 0) last = null; continue; }
|
|
144
|
+
if (!t.close) {
|
|
145
|
+
if (depth === 0) last = { list: t.name === 'ul' && isTaskList(t.attrs), close: -1, end: -1 };
|
|
146
|
+
depth++;
|
|
147
|
+
} else {
|
|
148
|
+
depth--;
|
|
149
|
+
if (depth < 0) return `${text}<ul data-type="taskList">${item}</ul>`; // not the editor's writing: leave it be
|
|
150
|
+
if (depth === 0 && last) { last.close = t.index; last.end = t.end; }
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (depth === 0 && last?.list && last.close >= 0 && !text.slice(last.end).trim()) return text.slice(0, last.close) + item + text.slice(last.close);
|
|
154
|
+
return `${text}<ul data-type="taskList">${item}</ul>`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Plain text as paragraphs, one a line. */
|
|
158
|
+
export const textHtml = (text) => String(text).split(/\r?\n/).map((l) => `<p>${escapeHtml(l)}</p>`).join('');
|
|
159
|
+
|
|
160
|
+
// --- for tools (the MCP server): a note written as Markdown, and read back as Markdown ---------------------------
|
|
161
|
+
|
|
162
|
+
export const newItemId = () => Array.from(crypto.getRandomValues(new Uint8Array(4)), (b) => b.toString(16).padStart(2, '0')).join('');
|
|
163
|
+
|
|
164
|
+
/** To-dos as the editor writes them, each with an id of its own ('' for none). */
|
|
165
|
+
export const todoListHtml = (todos = []) => (todos.length ? `<ul data-type="taskList">${todos.map((t) => todoHtml({ id: newItemId(), text: t })).join('')}</ul>` : '');
|
|
166
|
+
|
|
167
|
+
// Marks inside a run of text that holds no link: bold, italic, strike. `kept` are the pieces already written
|
|
168
|
+
// (code, links), standing in the text as \u0000n\u0000: a mark may go round one, never through one.
|
|
169
|
+
function marks(escaped) {
|
|
170
|
+
let s = escaped;
|
|
171
|
+
s = s.replace(/(^|[^\w*])\*\*\*(?=\S)([^*]{0,2000}?\S)\*\*\*(?!\w)/g, '$1<strong><em>$2</em></strong>');
|
|
172
|
+
s = s.replace(/(^|[^\w*])\*\*(?=\S)([^*]{1,2000}?)\*\*(?!\w)/g, '$1<strong>$2</strong>');
|
|
173
|
+
s = s.replace(/(^|[^\w*])\*(?=\S)([^*]{0,2000}?\S)\*(?!\w)/g, '$1<em>$2</em>');
|
|
174
|
+
s = s.replace(/(^|[^\w_])_(?=\S)([^_]{0,2000}?\S)_(?!\w)/g, '$1<em>$2</em>');
|
|
175
|
+
return s.replace(/~~([^~]{1,2000})~~/g, '<s>$1</s>');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// An address: what follows the scheme, with at most one pair of brackets inside it (a wiki page's name)
|
|
179
|
+
const MD_URL = String.raw`(?:[^()\s\u0000-\u0002<>]|\([^()\s\u0000-\u0002<>]*\)){1,2000}`;
|
|
180
|
+
const MD_LINK = new RegExp(String.raw`!?\[([^\]\n]{0,500})\]\(((?:https?:\/\/|mailto:)${MD_URL})(?:\s+(?:"[^"\n]{0,300}"|'[^'\n]{0,300}'))?\)`, 'g');
|
|
181
|
+
const MD_ANGLE = new RegExp(String.raw`<(https?:\/\/${MD_URL})>`, 'g');
|
|
182
|
+
const MD_BARE = new RegExp(String.raw`(^|[\s("'])(https?:\/\/${MD_URL})`, 'g');
|
|
183
|
+
|
|
184
|
+
// What stands inside a line: code, links, bold, italic, strike. Everything else is text, written as it is. Each
|
|
185
|
+
// piece of code and each link is written whole and set aside (it stands in the text as \u0000n\u0000) before the
|
|
186
|
+
// marks are read, so that a mark goes round one or not at all, never half inside it.
|
|
187
|
+
function inlineMd(text) {
|
|
188
|
+
const kept = [];
|
|
189
|
+
const keep = (html) => `\u0000${kept.push(html) - 1}\u0000`;
|
|
190
|
+
let s = String(text).replace(/[\u0000-\u0002]/g, '');
|
|
191
|
+
// \* is a star, not a mark
|
|
192
|
+
s = s.replace(/\\([\\`*_{}[\]()#+\-.!~|<>])/g, (_, c) => keep(escapeHtml(c)));
|
|
193
|
+
s = s.replace(/`([^`\n]{1,2000})`/g, (_, c) => keep(`<code>${escapeHtml(c)}</code>`));
|
|
194
|
+
// [label](address "title"); a picture () is not held by what a tool writes: a link to it, named by what
|
|
195
|
+
// it shows. The label has its own marks, and no link of its own (an address in it is its words)
|
|
196
|
+
s = s.replace(MD_LINK, (_, label, href) => keep(`<a href="${escapeHtml(href)}">${label.trim() ? marks(escapeHtml(label.trim())) : escapeHtml(href)}</a>`));
|
|
197
|
+
// <address>, and a bare address (without the full stop or comma that ends its sentence)
|
|
198
|
+
s = s.replace(MD_ANGLE, (_, href) => keep(`<a href="${escapeHtml(href)}">${escapeHtml(href)}</a>`));
|
|
199
|
+
s = s.replace(MD_BARE, (_, before, found) => {
|
|
200
|
+
const end = /[.,;:!?'"]+$/.exec(found)?.[0] ?? '';
|
|
201
|
+
const href = end ? found.slice(0, -end.length) : found;
|
|
202
|
+
return `${before}${keep(`<a href="${escapeHtml(href)}">${escapeHtml(href)}</a>`)}${end}`;
|
|
203
|
+
});
|
|
204
|
+
let out = marks(escapeHtml(s));
|
|
205
|
+
// (a link's label may hold code set aside before it: put back until none is left)
|
|
206
|
+
for (let n = 0; n < 4 && /\u0000\d+\u0000/.test(out); n++) out = out.replace(/\u0000(\d+)\u0000/g, (_, k) => kept[Number(k)] ?? '');
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const MD_ITEM = /^([ \t]*)([-*+]|\d{1,9}[.)])[ \t]+(.*)$/;
|
|
211
|
+
// (read with its spaces taken out: a long line of spaces is then nothing to weigh)
|
|
212
|
+
const isTableRule = (line) => line.length < 2000 && /^\|?:?-{2,}:?(\|:?-{2,}:?)*\|?$/.test(line.replace(/\s+/g, ''));
|
|
213
|
+
const MD_QUOTE_DEPTH = 6; // quotes inside quotes: deeper than this, the marks are text
|
|
214
|
+
const mdCells = (row) => { const c = row.trim().replace(/^\|/, '').replace(/\|$/, '').split(/(?<!\\)\|/); return c.map((x) => x.trim().replace(/\\\|/g, '|')); };
|
|
215
|
+
|
|
216
|
+
/** A list's items, as their lines' indents say they stand, written out: each run of one kind (bullets, numbers, to-dos) a list of its own. */
|
|
217
|
+
function listHtml(items) {
|
|
218
|
+
let html = '';
|
|
219
|
+
for (let i = 0; i < items.length; ) {
|
|
220
|
+
const kind = items[i].kind;
|
|
221
|
+
let j = i;
|
|
222
|
+
while (j < items.length && items[j].kind === kind) j++;
|
|
223
|
+
// a numbered list keeps the number it starts at (a part of a procedure)
|
|
224
|
+
html += kind === 'task' ? '<ul data-type="taskList">' : kind === 'ol' && items[i].n > 1 ? `<ol start="${items[i].n}">` : `<${kind}>`;
|
|
225
|
+
for (const it of items.slice(i, j)) {
|
|
226
|
+
const inner = `${it.paras.map((p) => `<p>${p.split('\n').map(inlineMd).join('<br>')}</p>`).join('')}${listHtml(it.children)}`;
|
|
227
|
+
html += kind === 'task'
|
|
228
|
+
? `<li data-type="taskItem" data-checked="${it.checked ? 'true' : 'false'}" data-id="${newItemId()}"><label><input type="checkbox"${it.checked ? ' checked="checked"' : ''}><span></span></label><div>${inner}</div></li>`
|
|
229
|
+
: `<li>${inner}</li>`;
|
|
230
|
+
}
|
|
231
|
+
html += kind === 'ol' ? '</ol>' : '</ul>';
|
|
232
|
+
i = j;
|
|
233
|
+
}
|
|
234
|
+
return html;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Markdown, as a tool writes it, to a note's HTML: headings (three sizes),
|
|
239
|
+
* paragraphs, lists (nested by their indent), `- [ ]` as to-dos to tick (each
|
|
240
|
+
* with an id of its own), quotes, fenced code, tables, links, bold, italic.
|
|
241
|
+
* Nothing in it is HTML to the note: what looks like a tag is written as text.
|
|
242
|
+
*/
|
|
243
|
+
export function markdownToNoteHtml(md, depth = 0) {
|
|
244
|
+
const lines = String(md ?? '').replace(/\r\n?/g, '\n').split('\n');
|
|
245
|
+
const out = [];
|
|
246
|
+
const para = [];
|
|
247
|
+
const flush = () => { if (para.length) { out.push(`<p>${para.map((l) => inlineMd(l)).join('<br>')}</p>`); para.length = 0; } };
|
|
248
|
+
let i = 0;
|
|
249
|
+
while (i < lines.length) {
|
|
250
|
+
const line = lines[i];
|
|
251
|
+
if (!line.trim()) { flush(); i++; continue; }
|
|
252
|
+
const fence = /^\s*(`{3,})/.exec(line);
|
|
253
|
+
if (fence) {
|
|
254
|
+
flush();
|
|
255
|
+
const buf = [];
|
|
256
|
+
i++;
|
|
257
|
+
// (closed by a fence at least as long as the one that opened it: a shorter one inside is the code's own)
|
|
258
|
+
const closes = (l) => { const m = /^\s*(`{3,})\s*$/.exec(l); return !!m && m[1].length >= fence[1].length; };
|
|
259
|
+
while (i < lines.length && !closes(lines[i])) buf.push(lines[i++]);
|
|
260
|
+
i++;
|
|
261
|
+
out.push(`<pre><code>${escapeHtml(buf.join('\n'))}</code></pre>`);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const h = /^(#{1,6}) +(\S.*)$/.exec(line.trim());
|
|
265
|
+
if (h) { flush(); const n = Math.min(3, h[1].length); out.push(`<h${n}>${inlineMd(h[2].replace(/ +#+$/, ''))}</h${n}>`); i++; continue; }
|
|
266
|
+
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) { flush(); i++; continue; } // a rule: a break between paragraphs
|
|
267
|
+
if (line.includes('|') && i + 1 < lines.length && isTableRule(lines[i + 1])) {
|
|
268
|
+
flush();
|
|
269
|
+
const head = mdCells(line);
|
|
270
|
+
i += 2;
|
|
271
|
+
const rows = [];
|
|
272
|
+
while (i < lines.length && lines[i].includes('|') && lines[i].trim()) rows.push(mdCells(lines[i++]));
|
|
273
|
+
const row = (r, tag) => `<tr>${head.map((_, k) => `<${tag}><p>${inlineMd(r[k] ?? '')}</p></${tag}>`).join('')}</tr>`;
|
|
274
|
+
out.push(`<table><tbody>${row(head, 'th')}${rows.map((r) => row(r, 'td')).join('')}</tbody></table>`);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (MD_ITEM.test(line)) {
|
|
278
|
+
flush();
|
|
279
|
+
// the items as a tree first (an item under the last one less indented than it), then written out
|
|
280
|
+
const root = { indent: -1, children: [] };
|
|
281
|
+
const open = [root];
|
|
282
|
+
let last = null;
|
|
283
|
+
let gap = false; // a blank line was passed: what goes on the item after it is a paragraph of its own
|
|
284
|
+
while (i < lines.length) {
|
|
285
|
+
const l = lines[i];
|
|
286
|
+
if (!l.trim()) {
|
|
287
|
+
// a blank line does not end the list when an item follows, or more of the item above (set in under it)
|
|
288
|
+
let j = i;
|
|
289
|
+
while (j < lines.length && !lines[j].trim()) j++;
|
|
290
|
+
if (j < lines.length && (MD_ITEM.test(lines[j]) || (last && /^[ \t]{2,}\S/.test(lines[j]) && !/^\s*```/.test(lines[j])))) { i = j; gap = true; continue; }
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
const m = MD_ITEM.exec(l);
|
|
294
|
+
if (!m) {
|
|
295
|
+
// a line set in under an item goes on it: the same paragraph, or after a blank line another one
|
|
296
|
+
if (!last || !/^[ \t]{2,}\S/.test(l) || /^\s*```/.test(l)) break;
|
|
297
|
+
if (gap) last.paras.push(l.trim()); else last.paras[last.paras.length - 1] += `\n${l.trim()}`;
|
|
298
|
+
gap = false;
|
|
299
|
+
i++;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const indent = m[1].replace(/\t/g, ' ').length;
|
|
303
|
+
const task = /^\[( |x|X)\] +(.*)$/.exec(m[3]);
|
|
304
|
+
const item = { indent, kind: task ? 'task' : /\d/.test(m[2]) ? 'ol' : 'ul', n: parseInt(m[2], 10) || 1, checked: !!task && task[1] !== ' ', paras: [task ? task[2] : m[3]], children: [] };
|
|
305
|
+
while (open.length > 1 && open[open.length - 1].indent >= indent) open.pop();
|
|
306
|
+
open[open.length - 1].children.push(item);
|
|
307
|
+
open.push(item);
|
|
308
|
+
last = item;
|
|
309
|
+
gap = false;
|
|
310
|
+
i++;
|
|
311
|
+
}
|
|
312
|
+
out.push(listHtml(root.children));
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
if (/^\s*>/.test(line) && depth < MD_QUOTE_DEPTH) {
|
|
316
|
+
flush();
|
|
317
|
+
const buf = [];
|
|
318
|
+
while (i < lines.length && /^\s*>/.test(lines[i])) buf.push(lines[i++].replace(/^\s*> ?/, ''));
|
|
319
|
+
out.push(`<blockquote>${markdownToNoteHtml(buf.join('\n'), depth + 1)}</blockquote>`);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
para.push(line.trim());
|
|
323
|
+
i++;
|
|
324
|
+
}
|
|
325
|
+
flush();
|
|
326
|
+
return out.join('');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// --- a note read back as Markdown -----------------------------------------------------------------------------
|
|
330
|
+
|
|
331
|
+
/** The note's HTML as a tree: { name, attrs, children } and strings (text, as written: entities and all). Tags left open are closed where their parent ends. */
|
|
332
|
+
function treeOf(html) {
|
|
333
|
+
const root = { name: '', attrs: '', children: [] };
|
|
334
|
+
const open = [root];
|
|
335
|
+
let at = 0;
|
|
336
|
+
for (const t of tags(html)) {
|
|
337
|
+
if (t.index > at) open[open.length - 1].children.push(html.slice(at, t.index));
|
|
338
|
+
at = t.end;
|
|
339
|
+
if (!t.close) {
|
|
340
|
+
// as a browser reads it: a block that starts inside a paragraph ends the paragraph, an item that starts inside an item ends that one
|
|
341
|
+
const top = open[open.length - 1].name;
|
|
342
|
+
if ((top === 'p' && /^(p|div|h[1-6]|ul|ol|table|blockquote|pre)$/.test(t.name)) || (top === 'li' && t.name === 'li')) open.pop();
|
|
343
|
+
const node = { name: t.name, attrs: t.attrs, children: [] };
|
|
344
|
+
open[open.length - 1].children.push(node);
|
|
345
|
+
if (!VOID.has(t.name) && !/\/\s*$/.test(t.attrs)) open.push(node);
|
|
346
|
+
} else {
|
|
347
|
+
const k = open.map((n) => n.name).lastIndexOf(t.name);
|
|
348
|
+
if (k > 0) open.length = k; // (a closing tag with nothing open by that name is passed over)
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (at < html.length) open[open.length - 1].children.push(html.slice(at));
|
|
352
|
+
return root;
|
|
353
|
+
}
|
|
354
|
+
const rawText = (node) => (typeof node === 'string' ? decode(node) : node.children.map(rawText).join(''));
|
|
355
|
+
const BLOCKS = new Set(['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'blockquote', 'pre', 'table', 'tbody', 'thead', 'tr', 'td', 'th', 'label']);
|
|
356
|
+
/** Text that Markdown would read as a mark, said to be text. */
|
|
357
|
+
const mdText = (t) => t.replace(/\s+/g, ' ').replace(/([\\`*[\]<>])/g, '\\$1').replace(/(^|[^\w\\])_(?=\S)|(?<=\S)_(?!\w)/g, (m) => m.replace('_', '\\_'));
|
|
358
|
+
/** Code between backticks, as many as it takes for its own not to end it. */
|
|
359
|
+
const mdCode = (t) => { const run = Math.max(0, ...(t.match(/`+/g) ?? []).map((r) => r.length)); const f = '`'.repeat(run + 1); return /^`|`$/.test(t) || run ? `${f} ${t} ${f}` : `${f}${t}${f}`; };
|
|
360
|
+
|
|
361
|
+
/** What stands inside a line, as Markdown. A line break is a newline. */
|
|
362
|
+
function mdInline(nodes) {
|
|
363
|
+
let s = '';
|
|
364
|
+
for (const n of nodes) {
|
|
365
|
+
if (typeof n === 'string') { s += mdText(decode(n)); continue; }
|
|
366
|
+
const inner = () => mdInline(n.children);
|
|
367
|
+
if (n.name === 'br') s += '\n';
|
|
368
|
+
else if (n.name === 'strong' || n.name === 'b') { const t = inner(); s += t.trim() ? `**${t}**` : t; }
|
|
369
|
+
else if (n.name === 'em' || n.name === 'i') { const t = inner(); s += t.trim() ? `*${t}*` : t; }
|
|
370
|
+
else if (n.name === 's' || n.name === 'del' || n.name === 'strike') { const t = inner(); s += t.trim() ? `~~${t}~~` : t; }
|
|
371
|
+
else if (n.name === 'code') s += mdCode(rawText(n).replace(/\n/g, ' '));
|
|
372
|
+
else if (n.name === 'a') {
|
|
373
|
+
const href = attr(n.attrs, 'href') ?? '';
|
|
374
|
+
const label = inner();
|
|
375
|
+
// (an address with a bracket or a space in it is written between < and >, where it ends at the >)
|
|
376
|
+
s += href ? `[${label || mdText(href)}](${/[()\s<>]/.test(href) ? `<${href.replace(/[<>]/g, encodeURIComponent)}>` : href})` : label;
|
|
377
|
+
}
|
|
378
|
+
else if (n.name === 'img') { if (/^picture:/.test(attr(n.attrs, 'src') ?? '')) s += `[picture${attr(n.attrs, 'alt') ? `: ${mdText(attr(n.attrs, 'alt'))}` : ''}]`; }
|
|
379
|
+
else if (!BLOCKS.has(n.name)) s += inner(); // (a mark this does not know: its words)
|
|
380
|
+
}
|
|
381
|
+
return s;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The blocks of a note as lines of Markdown. What is inline between blocks is a paragraph of its own. */
|
|
385
|
+
function mdBlocks(nodes) {
|
|
386
|
+
const out = []; // lines; '' between blocks
|
|
387
|
+
const block = (lines) => { if (!lines.length) return; if (out.length) out.push(''); out.push(...lines); };
|
|
388
|
+
let loose = [];
|
|
389
|
+
const flush = () => { const t = mdInline(loose).trim(); loose = []; if (t) block(t.split('\n').map((l) => l.trim())); };
|
|
390
|
+
for (const n of nodes) {
|
|
391
|
+
if (typeof n === 'string' || !BLOCKS.has(n.name)) { loose.push(n); continue; }
|
|
392
|
+
flush();
|
|
393
|
+
if (/^h[1-6]$/.test(n.name)) block([`${'#'.repeat(Math.min(3, Number(n.name[1])))} ${mdInline(n.children).replace(/\n/g, ' ').trim()}`]);
|
|
394
|
+
else if (n.name === 'p') { const t = mdInline(n.children).replace(/^\n+|\n+$/g, ''); if (t.trim()) block(t.split('\n').map((l) => l.trim())); }
|
|
395
|
+
else if (n.name === 'ul' || n.name === 'ol') block(mdList(n));
|
|
396
|
+
else if (n.name === 'blockquote') block(mdBlocks(n.children).map((l) => (l ? `> ${l}` : '>')));
|
|
397
|
+
else if (n.name === 'pre') {
|
|
398
|
+
const code = rawText(n).replace(/\n$/, '');
|
|
399
|
+
const fence = '`'.repeat(Math.max(3, ...(code.match(/`+/g) ?? []).map((r) => r.length + 1)));
|
|
400
|
+
block([fence, ...code.split('\n'), fence]);
|
|
401
|
+
}
|
|
402
|
+
else if (n.name === 'table') block(mdTable(n));
|
|
403
|
+
else block(mdBlocks(n.children)); // (a box round other blocks: what it holds)
|
|
404
|
+
}
|
|
405
|
+
flush();
|
|
406
|
+
return out;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** A list: each item's first block after its marker, the rest of it under that, set in by the marker's width. */
|
|
410
|
+
function mdList(list) {
|
|
411
|
+
const lines = [];
|
|
412
|
+
let k = 0;
|
|
413
|
+
const first = Math.max(1, parseInt(attr(list.attrs, 'start') ?? '1', 10) || 1); // a numbered list keeps the number it starts at
|
|
414
|
+
for (const li of list.children) {
|
|
415
|
+
if (typeof li === 'string' || li.name !== 'li') continue;
|
|
416
|
+
k++;
|
|
417
|
+
const task = attr(li.attrs, 'data-type') === 'taskItem';
|
|
418
|
+
const marker = task ? `- [${attr(li.attrs, 'data-checked') === 'true' ? 'x' : ' '}] ` : list.name === 'ol' ? `${first + k - 1}. ` : '- ';
|
|
419
|
+
// a to-do's words are in its <div> (its <label> is the box)
|
|
420
|
+
const body = mdBlocks(task ? li.children.filter((c) => typeof c === 'string' || c.name !== 'label').flatMap((c) => (typeof c !== 'string' && c.name === 'div' ? c.children : [c])) : li.children);
|
|
421
|
+
const pad = ' '.repeat(task ? 2 : marker.length);
|
|
422
|
+
const own = body.length ? body : [''];
|
|
423
|
+
// (a list straight under the item's words stands close to them, as it is written)
|
|
424
|
+
const tight = own.filter((l, at) => !(l === '' && /^\s*(- |\d+\. )/.test(own[at + 1] ?? '')));
|
|
425
|
+
lines.push(`${marker}${tight[0]}`.trimEnd(), ...tight.slice(1).map((l) => (l ? pad + l : '')));
|
|
426
|
+
}
|
|
427
|
+
return lines;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** A table: a row a line, a rule under the first. A | in a cell is said to be text. */
|
|
431
|
+
function mdTable(table) {
|
|
432
|
+
const rows = [];
|
|
433
|
+
const walk = (n) => { for (const c of n.children) { if (typeof c === 'string') continue; if (c.name === 'tr') rows.push(c.children.filter((x) => typeof x !== 'string' && (x.name === 'td' || x.name === 'th'))); else walk(c); } };
|
|
434
|
+
walk(table);
|
|
435
|
+
if (!rows.length) return [];
|
|
436
|
+
const cell = (c) => mdBlocks(c.children).filter(Boolean).join(' ').replace(/\|/g, '\\|');
|
|
437
|
+
const lines = rows.map((r) => `| ${r.map(cell).join(' | ')} |`);
|
|
438
|
+
lines.splice(1, 0, `|${' --- |'.repeat(rows[0].length)}`);
|
|
439
|
+
return lines;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* A note's HTML read back as Markdown, for a tool: headings, paragraphs,
|
|
444
|
+
* lists, to-dos as `- [ ]` / `- [x]`, quotes, code, tables, links. A picture
|
|
445
|
+
* is said to be one ("[picture: what it shows]"): its bytes are not text.
|
|
446
|
+
* What Markdown would read as a mark and is only text is said to be text.
|
|
447
|
+
*/
|
|
448
|
+
export function noteMarkdown(html) {
|
|
449
|
+
return mdBlocks(treeOf(String(html || '')).children).join('\n').trim(); // (blank lines inside code are the code's own)
|
|
450
|
+
}
|
package/src/notes.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The notes on this Mac: the core's notes (src/core/notes.js, what a note is and how it changes) over this Mac's
|
|
2
|
+
// store (src/host/notes-files.js: one file a note in ~/.sideora/notes, a lock across processes), with pictures
|
|
3
|
+
// fetched by the host's safe fetch (src/safe-fetch.js). Everyone on this Mac uses these: the hub, the MCP server,
|
|
4
|
+
// the CLI.
|
|
5
|
+
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { HOME_DIR } from './config.js';
|
|
8
|
+
import { safeFetch } from './safe-fetch.js';
|
|
9
|
+
import { makeNotes } from './core/notes.js';
|
|
10
|
+
import { notesFiles } from './host/notes-files.js';
|
|
11
|
+
|
|
12
|
+
export { CHANGED, GONE, MAX_PICTURE } from './core/notes.js';
|
|
13
|
+
export const NOTES_DIR = path.join(HOME_DIR, 'notes');
|
|
14
|
+
|
|
15
|
+
const notes = makeNotes({ store: notesFiles(NOTES_DIR), fetch: safeFetch });
|
|
16
|
+
|
|
17
|
+
export const { listNotes, readNote, saveNote, addToNote, unlinkNote, tickNote, noteChanges, removeNote, restoreNote, addNotePicture, notePicture, watchNotes } = notes;
|
|
18
|
+
/** (`fetch`: another way to fetch, for tests) */
|
|
19
|
+
export const fetchNotePicture = (a, fetch) => notes.fetchNotePicture(a, fetch);
|
package/src/notify.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// macOS sound + notification output (afplay / osascript).
|
|
2
|
+
// The alert→sound mapping lives in config.json under "sounds", e.g.
|
|
3
|
+
// "sounds": { "alarm": "siren.wav:2", "chime": "my-chime.mp3", "ding": "Purr" }
|
|
4
|
+
// A value is resolved, in order, as: an absolute path; a file in
|
|
5
|
+
// ~/.sideora/sounds/ ($SIDEORA_HOME/sounds; any format afplay plays: wav, mp3, m4a, aiff —
|
|
6
|
+
// extension optional); a sound bundled in the repo's assets/sounds/; or a
|
|
7
|
+
// macOS system sound name from
|
|
8
|
+
// /System/Library/Sounds. Optional ":N" suffix repeats the sound N times.
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { CONFIG_PATH, SOUNDS_DIR } from './config.js';
|
|
15
|
+
|
|
16
|
+
export const SOUND_DIR = '/System/Library/Sounds';
|
|
17
|
+
export const CUSTOM_DIR = SOUNDS_DIR;
|
|
18
|
+
/**
|
|
19
|
+
* The program that plays a sound file here: the Mac's afplay; on Linux PipeWire's pw-play (Ubuntu's desktop has no
|
|
20
|
+
* paplay any more), else PulseAudio's paplay; null where there is neither (a server with no sound: nothing played).
|
|
21
|
+
*/
|
|
22
|
+
let player;
|
|
23
|
+
export function soundPlayer({ platform = process.platform, has = onPath } = {}) {
|
|
24
|
+
if (platform === 'darwin') return 'afplay';
|
|
25
|
+
if (player === undefined || has !== onPath) {
|
|
26
|
+
const found = ['pw-play', 'paplay'].find((p) => has(p)) ?? null;
|
|
27
|
+
if (has !== onPath) return found;
|
|
28
|
+
player = found;
|
|
29
|
+
}
|
|
30
|
+
return player;
|
|
31
|
+
}
|
|
32
|
+
export function onPath(name) {
|
|
33
|
+
return (process.env.PATH || '').split(path.delimiter).some((d) => { try { fs.accessSync(path.join(d, name), fs.constants.X_OK); return true; } catch { return false; } });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const BUNDLED_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'assets', 'sounds');
|
|
37
|
+
const EXTENSIONS = ['', '.wav', '.mp3', '.m4a', '.aiff', '.aif', '.caf'];
|
|
38
|
+
|
|
39
|
+
export const DEFAULT_SOUNDS = {
|
|
40
|
+
alarm: 'mixkit-happy-bells-notification-937.wav',
|
|
41
|
+
chime: 'mixkit-clear-announce-tones-2861.wav',
|
|
42
|
+
ding: 'mixkit-musical-alert-notification-2309.wav',
|
|
43
|
+
message: 'freesound_community-mail-40275.mp3'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export function availableSounds() {
|
|
47
|
+
const audio = /\.(wav|mp3|m4a|aiff?|caf)$/i;
|
|
48
|
+
let custom = [];
|
|
49
|
+
let bundled = [];
|
|
50
|
+
let system = [];
|
|
51
|
+
try { custom = fs.readdirSync(CUSTOM_DIR).filter((f) => audio.test(f)); } catch {}
|
|
52
|
+
try { bundled = fs.readdirSync(BUNDLED_DIR).filter((f) => audio.test(f) && !custom.includes(f)); } catch {}
|
|
53
|
+
try { system = fs.readdirSync(SOUND_DIR).filter((f) => f.endsWith('.aiff')).map((f) => f.replace(/\.aiff$/, '')); } catch {}
|
|
54
|
+
return { custom, bundled, system };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function resolveSound(alert, config) {
|
|
58
|
+
const map = { ...DEFAULT_SOUNDS, ...(config?.sounds || {}) };
|
|
59
|
+
const spec = String(map[alert] || map.chime);
|
|
60
|
+
const m = spec.match(/^(.*?)(?::(\d+))?$/);
|
|
61
|
+
const base = m[1];
|
|
62
|
+
const repeats = Math.min(5, Math.max(1, Number(m[2]) || 1));
|
|
63
|
+
|
|
64
|
+
const candidates = [];
|
|
65
|
+
if (path.isAbsolute(base)) candidates.push(base);
|
|
66
|
+
for (const dir of [CUSTOM_DIR, BUNDLED_DIR]) {
|
|
67
|
+
for (const ext of EXTENSIONS) candidates.push(path.join(dir, base + ext));
|
|
68
|
+
}
|
|
69
|
+
candidates.push(path.join(SOUND_DIR, base + '.aiff'));
|
|
70
|
+
|
|
71
|
+
const file = candidates.find((f) => fs.existsSync(f) && fs.statSync(f).isFile()) || path.join(SOUND_DIR, 'Glass.aiff');
|
|
72
|
+
return { file, repeats, name: base };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function playSound(alert = 'chime', config = null) {
|
|
76
|
+
if (!config) {
|
|
77
|
+
try {
|
|
78
|
+
// read here, raw and optional: keeps callers simple
|
|
79
|
+
config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
|
|
80
|
+
} catch { config = null; }
|
|
81
|
+
}
|
|
82
|
+
const { file, repeats } = resolveSound(alert, config);
|
|
83
|
+
let i = 0;
|
|
84
|
+
const playOnce = () => {
|
|
85
|
+
const prog = soundPlayer();
|
|
86
|
+
if (!prog) return;
|
|
87
|
+
const p = spawn(prog, [file], { stdio: 'ignore' });
|
|
88
|
+
p.on('exit', () => {
|
|
89
|
+
if (++i < repeats) playOnce();
|
|
90
|
+
});
|
|
91
|
+
p.on('error', () => {});
|
|
92
|
+
};
|
|
93
|
+
playOnce();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function notifyMac(title, message) {
|
|
97
|
+
// (Linux: the desktop's notifications, where there is a desktop; else nothing: the log and the app say it)
|
|
98
|
+
if (process.platform !== 'darwin') { spawn('notify-send', ['--', String(title).slice(0, 200), String(message).slice(0, 500)], { stdio: 'ignore' }).on('error', () => {}); return; }
|
|
99
|
+
const script = `display notification ${js(message)} with title ${js(title)}`;
|
|
100
|
+
spawn('osascript', ['-e', script], { stdio: 'ignore' }).on('error', () => {});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function js(s) {
|
|
104
|
+
return JSON.stringify(String(s).slice(0, 500));
|
|
105
|
+
}
|