yamlover 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +214 -0
  3. package/bin/yamlover.js +202 -0
  4. package/dist/server.js +4681 -0
  5. package/index.html +12 -0
  6. package/package.json +72 -0
  7. package/src/client/App.tsx +372 -0
  8. package/src/client/NodeView.tsx +422 -0
  9. package/src/client/TaskStrip.tsx +34 -0
  10. package/src/client/Tree.tsx +97 -0
  11. package/src/client/api.ts +186 -0
  12. package/src/client/icons.ts +91 -0
  13. package/src/client/links.tsx +108 -0
  14. package/src/client/live.ts +42 -0
  15. package/src/client/main.tsx +10 -0
  16. package/src/client/paste-html.ts +228 -0
  17. package/src/client/paste-links.ts +42 -0
  18. package/src/client/paths.ts +109 -0
  19. package/src/client/render.tsx +326 -0
  20. package/src/client/renderers/annotate.tsx +507 -0
  21. package/src/client/renderers/asciidoc.tsx +35 -0
  22. package/src/client/renderers/chapter.tsx +138 -0
  23. package/src/client/renderers/csv.tsx +233 -0
  24. package/src/client/renderers/decoded.tsx +72 -0
  25. package/src/client/renderers/djvu.tsx +97 -0
  26. package/src/client/renderers/doc.tsx +40 -0
  27. package/src/client/renderers/docx.tsx +49 -0
  28. package/src/client/renderers/epub.tsx +147 -0
  29. package/src/client/renderers/explorer.tsx +209 -0
  30. package/src/client/renderers/fb2.tsx +149 -0
  31. package/src/client/renderers/headings.ts +69 -0
  32. package/src/client/renderers/heic.tsx +23 -0
  33. package/src/client/renderers/imagemap.tsx +157 -0
  34. package/src/client/renderers/kml.ts +46 -0
  35. package/src/client/renderers/latex.tsx +36 -0
  36. package/src/client/renderers/map.tsx +205 -0
  37. package/src/client/renderers/marklower.tsx +119 -0
  38. package/src/client/renderers/markup.tsx +64 -0
  39. package/src/client/renderers/media.tsx +19 -0
  40. package/src/client/renderers/panzoom.ts +101 -0
  41. package/src/client/renderers/pdf.tsx +176 -0
  42. package/src/client/renderers/plaintext.tsx +120 -0
  43. package/src/client/renderers/plantuml.tsx +82 -0
  44. package/src/client/renderers/psd.tsx +25 -0
  45. package/src/client/renderers/registry.tsx +389 -0
  46. package/src/client/renderers/rtf.tsx +210 -0
  47. package/src/client/renderers/spreadsheet.tsx +105 -0
  48. package/src/client/renderers/tag.tsx +113 -0
  49. package/src/client/renderers/text.tsx +41 -0
  50. package/src/client/renderers/tiff.tsx +33 -0
  51. package/src/client/styles.css +1115 -0
  52. package/src/client/vendor/README.md +30 -0
  53. package/src/client/vendor/djvu.js +15535 -0
  54. package/src/client/vite-env.d.ts +31 -0
  55. package/src/server/api.ts +147 -0
  56. package/src/server/engine-api.ts +1442 -0
  57. package/src/server/gitignore.ts +81 -0
  58. package/src/server/node-kind.ts +48 -0
  59. package/src/server/tasks.ts +83 -0
  60. package/src/server/yamlover.ts +1133 -0
@@ -0,0 +1,31 @@
1
+ /// <reference types="vite/client" />
2
+
3
+ // UTIF.js ships no types. Declare the minimal surface our TIFF renderer uses:
4
+ // decode the container into IFDs, decode each IFD's pixels in place, then read it
5
+ // back as RGBA. See https://github.com/photopea/UTIF.js.
6
+ declare module "utif" {
7
+ interface IFD {
8
+ width?: number;
9
+ height?: number;
10
+ [key: string]: unknown;
11
+ }
12
+ const UTIF: {
13
+ decode(buf: ArrayBuffer | Uint8Array): IFD[];
14
+ decodeImage(buf: ArrayBuffer | Uint8Array, ifd: IFD): void;
15
+ toRGBA8(ifd: IFD): Uint8Array;
16
+ };
17
+ export default UTIF;
18
+ }
19
+
20
+ // mammoth's browser build ships no types. Declare the one call our docx renderer
21
+ // makes: convert a .docx ArrayBuffer to an HTML string. See https://github.com/mwilliamson/mammoth.js.
22
+ declare module "mammoth/mammoth.browser" {
23
+ interface ConvertResult {
24
+ value: string;
25
+ messages: { type: string; message: string }[];
26
+ }
27
+ const mammoth: {
28
+ convertToHtml(input: { arrayBuffer: ArrayBuffer }): Promise<ConvertResult>;
29
+ };
30
+ export default mammoth;
31
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * api.ts — the JSON API the SPA calls.
3
+ *
4
+ * Endpoints, all keyed by a JSON-space `path` (`/key[0]/sub`, no "properties"):
5
+ *
6
+ * GET /api/tree the table of contents (containers only)
7
+ * GET /api/json?path&depth the node's value as JSON
8
+ * GET /api/schema?path&depth&format the node's instance schema (json | yaml)
9
+ * GET /api/blob?path a file-backed node's raw bytes (for the
10
+ * image / pdf / html / djvu / markup renderers)
11
+ *
12
+ * The materialized tree is cached per server for a short window (leaf bytes load
13
+ * lazily within it), so a burst of clicks does not re-read the filesystem; edits
14
+ * show up after the cache window on reload.
15
+ */
16
+
17
+ import path from "node:path";
18
+ import fs from "node:fs";
19
+ import type { IncomingMessage, ServerResponse } from "node:http";
20
+ import {
21
+ loadEntity,
22
+ getNode,
23
+ strToSegs,
24
+ buildTree,
25
+ labelForSeg,
26
+ toPlain,
27
+ toSchema,
28
+ buildRelations,
29
+ binaryContent,
30
+ displayTypeLabel,
31
+ documentPath,
32
+ formatFromExt,
33
+ setIgnoreFilter,
34
+ YNode,
35
+ } from "./yamlover.js";
36
+ import { buildGitIgnore } from "./gitignore.js";
37
+
38
+ type Handler = (req: IncomingMessage, res: ServerResponse, url: URL) => void;
39
+
40
+ interface Options {
41
+ gitignore?: boolean; // honor .gitignore for stray files (default: true)
42
+ }
43
+
44
+ // The materialized tree is cached this long. With lazy directory loading the
45
+ // cached tree *accumulates* the levels you browse (each read once and memoized),
46
+ // so a longer window keeps navigation warm; edits show after it on reload.
47
+ const CACHE_TTL_MS = 15000;
48
+
49
+ export function createHandlers(dataRoot: string, opts: Options = {}): Handler {
50
+ // The root label (breadcrumb head and TOC root): the yamlover title if set,
51
+ // else the served directory's name — always non-empty.
52
+ const rootName = path.basename(path.resolve(dataRoot)) || "/";
53
+ setIgnoreFilter(opts.gitignore === false ? () => false : buildGitIgnore(dataRoot));
54
+
55
+ let cache: { root: YNode; time: number } | null = null;
56
+ const root = (): YNode => {
57
+ const now = Date.now();
58
+ if (!cache || now - cache.time > CACHE_TTL_MS) cache = { root: loadEntity(dataRoot), time: now };
59
+ return cache.root;
60
+ };
61
+
62
+ return (_req, res, url) => {
63
+ try {
64
+ const segs = strToSegs(url.searchParams.get("path") || "/");
65
+ const depth = parseDepth(url.searchParams.get("depth"));
66
+
67
+ if (url.pathname === "/api/info") {
68
+ sendJson(res, 200, { root: root().title || rootName }); // breadcrumb head
69
+ return;
70
+ }
71
+
72
+ if (url.pathname === "/api/tree") {
73
+ // The TOC loads lazily: a subtree rooted at `path`, `depth` levels deep
74
+ // (default 3). Past the boundary, nodes carry hasChildren for the client
75
+ // to fetch on expand.
76
+ const start = getNode(root(), segs);
77
+ const label = segs.length === 0 ? start.title || rootName : labelForSeg(start, segs[segs.length - 1]);
78
+ sendJson(res, 200, buildTree(start, segs, label, depth ?? 3));
79
+ return;
80
+ }
81
+
82
+ if (url.pathname === "/api/blob") {
83
+ // Raw file bytes, served with the node's (inferred) format as the
84
+ // Content-Type, so a renderer can point an <img>/<iframe>/pdf loader at
85
+ // the URL — or fetch the text of a markdown/asciidoc file — directly.
86
+ const n = getNode(root(), segs);
87
+ if (!n.path || !fs.existsSync(n.path) || fs.statSync(n.path).isDirectory()) {
88
+ sendJson(res, 404, { error: `not a file-backed node: ${url.searchParams.get("path")}` });
89
+ return;
90
+ }
91
+ const data = fs.readFileSync(n.path);
92
+ res.statusCode = 200;
93
+ res.setHeader("Content-Type", n.format ?? formatFromExt(n.path) ?? "application/octet-stream");
94
+ res.setHeader("Content-Length", String(data.length));
95
+ res.end(data);
96
+ return;
97
+ }
98
+
99
+ const node = getNode(root(), segs);
100
+ // display type: a virtual-children overlay reads as `object` (matches the TOC)
101
+ const type = displayTypeLabel(node);
102
+ // Every representation is one level deep with nested containers as links;
103
+ // the client picks the YAML/JSON syntax. `depth` defaults to 1.
104
+ const viewDepth = depth ?? 1;
105
+
106
+ if (url.pathname === "/api/json") {
107
+ // A binary leaf's bytes load only on explicit request (?binary=1) — the
108
+ // selection's header fetch shows its cheap `repr` (size) via toPlain.
109
+ const wantBytes = type === "binary" && url.searchParams.get("binary") === "1";
110
+ sendJson(res, 200, {
111
+ path: url.searchParams.get("path") || "/",
112
+ type,
113
+ format: node.format ?? null, // with `type`, the (type, format) renderer key
114
+ concrete: node.concrete,
115
+ // the document (nearest yamlover entity) this node belongs to — the anchor
116
+ // a document-relative (`/…`) marklower link resolves against
117
+ documentPath: documentPath(root(), segs),
118
+ title: node.title ?? null,
119
+ description: node.description ?? null,
120
+ // virtual children are merged into the value; named up-edges (+ `..`)
121
+ // ride alongside as `relations` for the panel above the divider
122
+ value: wantBytes ? binaryContent(node) : toPlain(node, viewDepth, segs, true, root()),
123
+ relations: buildRelations(node, segs, root()),
124
+ });
125
+ } else if (url.pathname === "/api/schema") {
126
+ // pass the real root so a node's rel pointers (esp. absolute /…) resolve
127
+ sendJson(res, 200, toSchema(node, viewDepth, segs, true, root()));
128
+ } else {
129
+ sendJson(res, 404, { error: `no such endpoint: ${url.pathname}` });
130
+ }
131
+ } catch (exc) {
132
+ sendJson(res, 400, { error: (exc as Error).message || String(exc) });
133
+ }
134
+ };
135
+ }
136
+
137
+ function parseDepth(raw: string | null): number | null {
138
+ if (raw == null || raw === "") return null;
139
+ const n = Number(raw);
140
+ return Number.isInteger(n) && n >= 0 ? n : null;
141
+ }
142
+
143
+ function sendJson(res: ServerResponse, status: number, body: unknown): void {
144
+ res.statusCode = status;
145
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
146
+ res.end(JSON.stringify(body, null, 2));
147
+ }