@ureck/alexandria 1.6.0 → 1.6.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.
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/docs/xlsx.ts
4
+ import JSZip from "jszip";
5
+ var ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
6
+ function decodeXml(s) {
7
+ return s.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (m, e) => {
8
+ if (e[0] === "#") {
9
+ const code = e[1] === "x" || e[1] === "X" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
10
+ return Number.isFinite(code) ? String.fromCodePoint(code) : m;
11
+ }
12
+ return ENTITIES[e] ?? m;
13
+ });
14
+ }
15
+ var attr = (tag, name) => tag.match(new RegExp(`\\b${name}="([^"]*)"`))?.[1];
16
+ function textOf(xml) {
17
+ let out = "";
18
+ for (const m of xml.matchAll(/<t(?:\s[^>]*)?>([\s\S]*?)<\/t>/g)) out += m[1];
19
+ return decodeXml(out);
20
+ }
21
+ function colIndex(ref) {
22
+ const letters = ref.match(/^[A-Z]+/i)?.[0].toUpperCase() ?? "A";
23
+ let n = 0;
24
+ for (const ch of letters) n = n * 26 + (ch.charCodeAt(0) - 64);
25
+ return n - 1;
26
+ }
27
+ var DATE_IDS = /* @__PURE__ */ new Set([14, 15, 16, 17, 18, 19, 20, 21, 22, 27, 30, 36, 45, 46, 47, 50, 57]);
28
+ function isDateFormat(code) {
29
+ const c = code.replace(/"[^"]*"|\[[^\]]*\]|\\./g, "");
30
+ return /[dmyh]/i.test(c) && !/^[#0.,%\s]+$/.test(c);
31
+ }
32
+ function excelDate(serial, date1904 = false) {
33
+ const ms = Math.round((serial + (date1904 ? 1462 : 0) - 25569) * 864e5);
34
+ const d = new Date(ms);
35
+ return isNaN(d.getTime()) ? String(serial) : d.toISOString().slice(0, 10);
36
+ }
37
+ function numText(v) {
38
+ const n = Number(v);
39
+ return Number.isFinite(n) ? String(n) : v;
40
+ }
41
+ async function readXlsx(buf) {
42
+ const zip = await JSZip.loadAsync(buf);
43
+ const read = async (p) => await zip.file(p)?.async("string") ?? "";
44
+ const workbook = await read("xl/workbook.xml");
45
+ const date1904 = /<workbookPr\b[^>]*\bdate1904="(1|true)"/.test(workbook);
46
+ const rels = await read("xl/_rels/workbook.xml.rels");
47
+ const target = /* @__PURE__ */ new Map();
48
+ for (const m of rels.matchAll(/<Relationship\b[^>]*>/g)) {
49
+ const id = attr(m[0], "Id");
50
+ const t = attr(m[0], "Target");
51
+ if (id && t) target.set(id, t.startsWith("/") ? t.slice(1) : t.startsWith("xl/") ? t : `xl/${t}`);
52
+ }
53
+ const shared = [];
54
+ for (const m of (await read("xl/sharedStrings.xml")).matchAll(/<si>([\s\S]*?)<\/si>/g)) shared.push(textOf(m[1]));
55
+ const styles = await read("xl/styles.xml");
56
+ const customFmt = /* @__PURE__ */ new Map();
57
+ for (const m of styles.matchAll(/<numFmt\b[^>]*>/g)) {
58
+ const id = Number(attr(m[0], "numFmtId"));
59
+ const code = attr(m[0], "formatCode");
60
+ if (code !== void 0) customFmt.set(id, decodeXml(code));
61
+ }
62
+ const dateStyle = [];
63
+ const xfs = styles.match(/<cellXfs\b[^>]*>([\s\S]*?)<\/cellXfs>/)?.[1] ?? "";
64
+ for (const m of xfs.matchAll(/<xf\b[^>]*>/g)) {
65
+ const id = Number(attr(m[0], "numFmtId") ?? 0);
66
+ dateStyle.push(DATE_IDS.has(id) || customFmt.has(id) && isDateFormat(customFmt.get(id)));
67
+ }
68
+ const sheets = [];
69
+ for (const m of workbook.matchAll(/<sheet\b[^>]*>/g)) {
70
+ const name = decodeXml(attr(m[0], "name") ?? `Hoja${sheets.length + 1}`);
71
+ const rid = attr(m[0], "r:id") ?? attr(m[0], "id");
72
+ const file = rid && target.get(rid) || `xl/worksheets/sheet${sheets.length + 1}.xml`;
73
+ const xml = await read(file);
74
+ const rows = [];
75
+ const rowsByNum = /* @__PURE__ */ new Map();
76
+ for (const r of xml.matchAll(/<row\b[^>]*?(?:\/>|>([\s\S]*?)<\/row>)/g)) {
77
+ const row = [];
78
+ let next = 0;
79
+ for (const c of (r[1] ?? "").matchAll(/<c\b([^>]*?)(?:\/>|>([\s\S]*?)<\/c>)/g)) {
80
+ const a = c[1];
81
+ const inner = c[2] ?? "";
82
+ const ref = attr(a, "r");
83
+ const col = ref ? colIndex(ref) : next;
84
+ next = col + 1;
85
+ const t = attr(a, "t");
86
+ const v = inner.match(/<v>([\s\S]*?)<\/v>/)?.[1];
87
+ let val = "";
88
+ if (t === "s") val = v !== void 0 ? shared[Number(v)] ?? "" : "";
89
+ else if (t === "inlineStr") val = textOf(inner.match(/<is>([\s\S]*?)<\/is>/)?.[1] ?? "");
90
+ else if (t === "b") val = v === "1" ? "true" : v === "0" ? "false" : "";
91
+ else if (t === "str" || t === "e") val = v !== void 0 ? decodeXml(v) : "";
92
+ else if (v !== void 0) {
93
+ const s = Number(attr(a, "s") ?? 0);
94
+ val = dateStyle[s] && Number.isFinite(Number(v)) ? excelDate(Number(v), date1904) : numText(v);
95
+ }
96
+ if (val === "") continue;
97
+ while (row.length < col) row.push("");
98
+ row[col] = val;
99
+ }
100
+ rowsByNum.set(Number(attr(r[0], "r") ?? rowsByNum.size + 1), row);
101
+ }
102
+ for (const mc of xml.matchAll(/<mergeCell\b[^>]*\bref="([A-Z]+)(\d+):([A-Z]+)(\d+)"/gi)) {
103
+ const [c1, r1, c2, r2] = [colIndex(mc[1]), Number(mc[2]), colIndex(mc[3]), Number(mc[4])];
104
+ const master = rowsByNum.get(r1)?.[c1] ?? "";
105
+ if (!master) continue;
106
+ for (let rn = r1; rn <= r2; rn++) {
107
+ const row = rowsByNum.get(rn) ?? [];
108
+ for (let cn = c1; cn <= c2; cn++) {
109
+ while (row.length < cn) row.push("");
110
+ row[cn] = master;
111
+ }
112
+ rowsByNum.set(rn, row);
113
+ }
114
+ }
115
+ for (const n of [...rowsByNum.keys()].sort((a, b) => a - b)) {
116
+ const row = rowsByNum.get(n);
117
+ if (row.some((x) => x !== "")) rows.push(row);
118
+ }
119
+ sheets.push({ name, rows });
120
+ }
121
+ return sheets;
122
+ }
123
+ export {
124
+ colIndex,
125
+ decodeXml,
126
+ excelDate,
127
+ readXlsx
128
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ureck/alexandria",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "Alexandria — bóveda de conocimiento local para agentes de IA (Claude Code, Codex, Gemini CLI, Cursor, OpenCode y más): captura automática por hooks, búsqueda semántica offline y grafo estilo Obsidian. Ahorra tokens inyectando solo el contexto relevante.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -48,8 +48,8 @@
48
48
  "@huggingface/transformers": "^4.3.2",
49
49
  "@modelcontextprotocol/sdk": "^1.32.2",
50
50
  "commander": "^15.0.0",
51
- "exceljs": "^4.4.0",
52
51
  "gray-matter": "^4.0.3",
52
+ "jszip": "^3.10.2",
53
53
  "mammoth": "^1.13.0",
54
54
  "minisearch": "^7.1.2",
55
55
  "picocolors": "^1.1.1",
@@ -64,7 +64,7 @@
64
64
  "devDependencies": {
65
65
  "@types/node": "^22.20.5",
66
66
  "@types/turndown": "^5.0.6",
67
- "jszip": "^3.10.2",
67
+ "exceljs": "^4.4.0",
68
68
  "tsup": "^9.0.0",
69
69
  "typescript": "^5.7.3",
70
70
  "vitest": "^5.0.5"