@aiquants/markdown 1.18.0 → 2.0.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.
@@ -1,156 +1,56 @@
1
- import * as J from "cheerio";
2
- import { load as U } from "cheerio";
3
- import { L as h } from "./simple-logger-DIfMY3fH.js";
4
- import { visit as N } from "unist-util-visit";
5
- import I from "node:fs/promises";
6
- import * as M from "node:path";
7
- import w from "node:path";
8
- import L from "url-join";
9
- import { spawn as j } from "node:child_process";
10
- import { existsSync as A, unlinkSync as q } from "node:fs";
11
- import { createRequire as W } from "node:module";
12
- import * as B from "node:net";
13
- import { fileURLToPath as K } from "node:url";
14
- import { createClient as z } from "@connectrpc/connect";
15
- import { createConnectTransport as G } from "@connectrpc/connect-node";
16
- import { Message as E, proto3 as d, MethodKind as Z } from "@bufbuild/protobuf";
17
- const H = async (e) => {
1
+ import { load as C } from "cheerio";
2
+ import { L as R } from "./simple-logger-DIfMY3fH.js";
3
+ import { visit as D } from "unist-util-visit";
4
+ import { spawn as I } from "node:child_process";
5
+ import { existsSync as g, unlinkSync as E } from "node:fs";
6
+ import { createRequire as L } from "node:module";
7
+ import * as $ from "node:net";
8
+ import * as T from "node:path";
9
+ import { fileURLToPath as F } from "node:url";
10
+ import { createClient as q } from "@connectrpc/connect";
11
+ import { createConnectTransport as K } from "@connectrpc/connect-node";
12
+ import { Message as v, proto3 as i, MethodKind as j } from "@bufbuild/protobuf";
13
+ const U = async (r) => {
18
14
  try {
19
- const t = new AbortController(), r = setTimeout(() => t.abort(), 1e4), l = await fetch(e, { signal: t.signal });
20
- if (clearTimeout(r), !l.ok)
15
+ const t = new AbortController(), e = setTimeout(() => t.abort(), 1e4), l = await fetch(r, { signal: t.signal });
16
+ if (clearTimeout(e), !l.ok)
21
17
  return null;
22
- const s = await l.text(), o = { url: e }, n = U(s), a = n("title").text().trim();
23
- o.title = a;
24
- const c = n("meta");
25
- for (const i of c) {
26
- const m = i.attribs.property;
27
- m === "og:site_name" && (o.site_name = i.attribs.content || void 0), m === "og:title" && (o.title = i.attribs.content || a), m === "og:description" && (o.description = i.attribs.content || void 0), m === "og:image" && (o.image = i.attribs.content || void 0), m === "og:type" && (o.type = i.attribs.content || void 0), i.attribs.name === "description" && (o.description = i.attribs.content || void 0);
18
+ const a = await l.text(), n = { url: r }, s = C(a), o = s("title").text().trim();
19
+ n.title = o;
20
+ const y = s("meta");
21
+ for (const c of y) {
22
+ const m = c.attribs.property;
23
+ m === "og:site_name" && (n.site_name = c.attribs.content || void 0), m === "og:title" && (n.title = c.attribs.content || o), m === "og:description" && (n.description = c.attribs.content || void 0), m === "og:image" && (n.image = c.attribs.content || void 0), m === "og:type" && (n.type = c.attribs.content || void 0), c.attribs.name === "description" && (n.description = c.attribs.content || void 0);
28
24
  }
29
- return o;
25
+ return n;
30
26
  } catch (t) {
31
- return h.error("Error fetching site metadata:", t), null;
27
+ return R.error("Error fetching site metadata:", t), null;
32
28
  }
33
- }, v = /* @__PURE__ */ new Map(), V = (e, t) => t.fetchAll ? !0 : t.urlPatterns && t.urlPatterns.length > 0 ? t.urlPatterns.some((r) => r.test(e)) : /^https?:\/\//.test(e) && !e.includes("localhost") && !e.includes("127.0.0.1"), yt = (e = {}) => {
34
- const { enableCache: t = !0 } = e;
35
- return async (r) => {
29
+ }, h = /* @__PURE__ */ new Map(), G = (r, t) => t.fetchAll ? !0 : t.urlPatterns && t.urlPatterns.length > 0 ? t.urlPatterns.some((e) => e.test(r)) : /^https?:\/\//.test(r) && !r.includes("localhost") && !r.includes("127.0.0.1"), lt = (r = {}) => {
30
+ const { enableCache: t = !0 } = r;
31
+ return async (e) => {
36
32
  const l = [];
37
- N(r, "block-link", (o) => {
38
- V(o.url, e) && l.push(o);
33
+ D(e, "block-link", (n) => {
34
+ G(n.url, r) && l.push(n);
39
35
  });
40
- const s = l.map(async (o) => {
41
- const n = o.url;
42
- if (t && v.has(n)) {
43
- const a = v.get(n);
44
- a && (o.data = o.data || {}, o.data.siteMetadata = a);
36
+ const a = l.map(async (n) => {
37
+ const s = n.url;
38
+ if (t && h.has(s)) {
39
+ const o = h.get(s);
40
+ o && (n.data = n.data || {}, n.data.siteMetadata = o);
45
41
  return;
46
42
  }
47
43
  try {
48
- const a = await H(n);
49
- a ? (o.data = o.data || {}, o.data.siteMetadata = a, t && v.set(n, a)) : t && v.set(n, null);
50
- } catch (a) {
51
- h.warn(`Failed to fetch metadata for ${n}:`, a), t && v.set(n, null);
44
+ const o = await U(s);
45
+ o ? (n.data = n.data || {}, n.data.siteMetadata = o, t && h.set(s, o)) : t && h.set(s, null);
46
+ } catch (o) {
47
+ R.warn(`Failed to fetch metadata for ${s}:`, o), t && h.set(s, null);
52
48
  }
53
49
  });
54
- await Promise.all(s);
50
+ await Promise.all(a);
55
51
  };
56
- }, X = /<!--\s+@import\s+"(.+?)"\s+-->/;
57
- function gt(e) {
58
- return async (t) => {
59
- const { markdownFilePath: r } = e, l = w.dirname(r), s = [];
60
- if (N(t, "html", (n, a, c) => {
61
- if (!c || typeof a != "number")
62
- return;
63
- const i = n.value.match(X);
64
- if (!i)
65
- return;
66
- const m = i[1], u = w.resolve(l, m);
67
- Array.isArray(c.children) && s.push({ parent: c, index: a, absolutePath: u });
68
- }), s.length === 0)
69
- return;
70
- const o = await Promise.all(
71
- s.map(async ({ absolutePath: n }) => {
72
- try {
73
- const a = await I.readFile(n, "utf-8");
74
- return this.parse(a);
75
- } catch (a) {
76
- return a && a.code === "ENOENT" ? h.warn(`[remark-import-directive] Import file not found: ${n}`) : h.error(`[remark-import-directive] Error processing import: ${n}`, a), null;
77
- }
78
- })
79
- );
80
- for (let n = s.length - 1; n >= 0; n--) {
81
- const { parent: a, index: c } = s[n], i = o[n];
82
- i?.children && Array.isArray(i.children) && a.children.splice(c, 1, ...i.children);
83
- }
84
- };
85
- }
86
- const Y = /^[a-z][a-z0-9+.-]*:/, _ = async (e, t, r, l, s = !1) => {
87
- if (!e)
88
- return "";
89
- if (Y.test(e)) {
90
- const m = e.substring(0, e.indexOf(":")).toLowerCase();
91
- if (["http", "https", "mailto", "tel", "file"].includes(m))
92
- return e;
93
- }
94
- if (e.startsWith("#") || e.startsWith("/"))
95
- return e;
96
- const o = decodeURIComponent(e), n = w.dirname(r), a = w.normalize(w.join(n, o)), c = w.resolve(l, a);
97
- if (!c.startsWith(l) || a.startsWith(".."))
98
- return h.error(`Security Alert: Attempt to access file outside project root: ${c}`), "";
99
- if (s)
100
- try {
101
- await I.access(c);
102
- } catch {
103
- return h.warn(`File not found: ${c}`), "";
104
- }
105
- const i = a.replace(/\\/g, "/");
106
- return t ? L("/assets/", i) : `?filePath=${encodeURIComponent(i)}`;
107
52
  };
108
- function Tt(e) {
109
- return async (t) => {
110
- if (!(e?.markdownFilePath && e?.workspaceRoot)) {
111
- h.warn("remark-resolve-uri-async: markdownFilePath and workspaceRoot are required. Skipping URI resolution.");
112
- return;
113
- }
114
- const r = [], l = [];
115
- N(t, ["image", "link", "html"], (n) => {
116
- if (n.type === "html")
117
- l.push(n);
118
- else {
119
- const a = n;
120
- a.url && r.push({ node: a, isImage: a.type === "image" });
121
- }
122
- });
123
- const s = r.map(async ({ node: n, isImage: a }) => {
124
- const c = await _(n.url, a, e.markdownFilePath, e.workspaceRoot, e.validateExistence);
125
- n.url = c;
126
- }), o = l.map(async (n) => {
127
- const a = J.load(n.value, null, !1);
128
- let c = !1;
129
- const i = a("img").toArray();
130
- for (const u of i) {
131
- const f = a(u).attr("src");
132
- if (f) {
133
- const p = await _(f, !0, e.markdownFilePath, e.workspaceRoot, e.validateExistence);
134
- p !== f && (a(u).attr("src", p), c = !0);
135
- }
136
- }
137
- const m = a("a").toArray();
138
- for (const u of m) {
139
- const f = a(u).attr("href");
140
- if (f) {
141
- const p = await _(f, !1, e.markdownFilePath, e.workspaceRoot, e.validateExistence);
142
- p !== f && (a(u).attr("href", p), c = !0);
143
- }
144
- }
145
- if (c) {
146
- const u = a.html();
147
- u && (n.value = u);
148
- }
149
- });
150
- await Promise.all([...s, ...o]);
151
- };
152
- }
153
- class b extends E {
53
+ class u extends v {
154
54
  /**
155
55
  * @generated from field: string markdown_text = 1;
156
56
  */
@@ -215,11 +115,11 @@ class b extends E {
215
115
  */
216
116
  enableZennDirectives;
217
117
  constructor(t) {
218
- super(), d.util.initPartial(t, this);
118
+ super(), i.util.initPartial(t, this);
219
119
  }
220
- static runtime = d;
120
+ static runtime = i;
221
121
  static typeName = "markdown.v1.ConvertRequest";
222
- static fields = d.util.newFieldList(() => [
122
+ static fields = i.util.newFieldList(() => [
223
123
  {
224
124
  no: 1,
225
125
  name: "markdown_text",
@@ -255,20 +155,20 @@ class b extends E {
255
155
  { no: 9, name: "enable_embeds", kind: "scalar", T: 8, opt: !0 },
256
156
  { no: 10, name: "enable_zenn_directives", kind: "scalar", T: 8, opt: !0 }
257
157
  ]);
258
- static fromBinary(t, r) {
259
- return new b().fromBinary(t, r);
158
+ static fromBinary(t, e) {
159
+ return new u().fromBinary(t, e);
260
160
  }
261
- static fromJson(t, r) {
262
- return new b().fromJson(t, r);
161
+ static fromJson(t, e) {
162
+ return new u().fromJson(t, e);
263
163
  }
264
- static fromJsonString(t, r) {
265
- return new b().fromJsonString(t, r);
164
+ static fromJsonString(t, e) {
165
+ return new u().fromJsonString(t, e);
266
166
  }
267
- static equals(t, r) {
268
- return d.util.equals(b, t, r);
167
+ static equals(t, e) {
168
+ return i.util.equals(u, t, e);
269
169
  }
270
170
  }
271
- class k extends E {
171
+ class d extends v {
272
172
  /**
273
173
  * @generated from field: string html_content = 1;
274
174
  */
@@ -278,11 +178,11 @@ class k extends E {
278
178
  */
279
179
  headings = [];
280
180
  constructor(t) {
281
- super(), d.util.initPartial(t, this);
181
+ super(), i.util.initPartial(t, this);
282
182
  }
283
- static runtime = d;
183
+ static runtime = i;
284
184
  static typeName = "markdown.v1.ConvertResponse";
285
- static fields = d.util.newFieldList(() => [
185
+ static fields = i.util.newFieldList(() => [
286
186
  {
287
187
  no: 1,
288
188
  name: "html_content",
@@ -290,22 +190,22 @@ class k extends E {
290
190
  T: 9
291
191
  /* ScalarType.STRING */
292
192
  },
293
- { no: 2, name: "headings", kind: "message", T: y, repeated: !0 }
193
+ { no: 2, name: "headings", kind: "message", T: f, repeated: !0 }
294
194
  ]);
295
- static fromBinary(t, r) {
296
- return new k().fromBinary(t, r);
195
+ static fromBinary(t, e) {
196
+ return new d().fromBinary(t, e);
297
197
  }
298
- static fromJson(t, r) {
299
- return new k().fromJson(t, r);
198
+ static fromJson(t, e) {
199
+ return new d().fromJson(t, e);
300
200
  }
301
- static fromJsonString(t, r) {
302
- return new k().fromJsonString(t, r);
201
+ static fromJsonString(t, e) {
202
+ return new d().fromJsonString(t, e);
303
203
  }
304
- static equals(t, r) {
305
- return d.util.equals(k, t, r);
204
+ static equals(t, e) {
205
+ return i.util.equals(d, t, e);
306
206
  }
307
207
  }
308
- class y extends E {
208
+ class f extends v {
309
209
  /**
310
210
  * @generated from field: string text = 1;
311
211
  */
@@ -319,11 +219,11 @@ class y extends E {
319
219
  */
320
220
  id = "";
321
221
  constructor(t) {
322
- super(), d.util.initPartial(t, this);
222
+ super(), i.util.initPartial(t, this);
323
223
  }
324
- static runtime = d;
224
+ static runtime = i;
325
225
  static typeName = "markdown.v1.Heading";
326
- static fields = d.util.newFieldList(() => [
226
+ static fields = i.util.newFieldList(() => [
327
227
  {
328
228
  no: 1,
329
229
  name: "text",
@@ -346,20 +246,20 @@ class y extends E {
346
246
  /* ScalarType.STRING */
347
247
  }
348
248
  ]);
349
- static fromBinary(t, r) {
350
- return new y().fromBinary(t, r);
249
+ static fromBinary(t, e) {
250
+ return new f().fromBinary(t, e);
351
251
  }
352
- static fromJson(t, r) {
353
- return new y().fromJson(t, r);
252
+ static fromJson(t, e) {
253
+ return new f().fromJson(t, e);
354
254
  }
355
- static fromJsonString(t, r) {
356
- return new y().fromJsonString(t, r);
255
+ static fromJsonString(t, e) {
256
+ return new f().fromJsonString(t, e);
357
257
  }
358
- static equals(t, r) {
359
- return d.util.equals(y, t, r);
258
+ static equals(t, e) {
259
+ return i.util.equals(f, t, e);
360
260
  }
361
261
  }
362
- const Q = {
262
+ const W = {
363
263
  typeName: "markdown.v1.MarkdownService",
364
264
  methods: {
365
265
  /**
@@ -367,129 +267,126 @@ const Q = {
367
267
  */
368
268
  convert: {
369
269
  name: "Convert",
370
- I: b,
371
- O: k,
372
- kind: Z.Unary
270
+ I: u,
271
+ O: d,
272
+ kind: j.Unary
373
273
  }
374
274
  }
375
- }, g = process.env.MARKDOWN_SOCKET_PATH || "/tmp/markdown.sock", O = G({
275
+ }, p = process.env.MARKDOWN_SOCKET_PATH || "/tmp/markdown.sock", Z = K({
376
276
  baseUrl: "http://localhost",
377
277
  httpVersion: "2",
378
278
  nodeOptions: {
379
- createConnection: () => B.connect(g)
279
+ createConnection: () => $.connect(p)
380
280
  }
381
- }), tt = z(Q, O), $ = () => {
281
+ }), z = q(W, Z), N = () => {
382
282
  try {
383
- return M.dirname(K(import.meta.url));
283
+ return T.dirname(F(import.meta.url));
384
284
  } catch {
385
285
  return __dirname;
386
286
  }
387
- }, C = () => `@aiquants/markdown-${process.platform}-${process.arch}`, x = () => process.platform === "win32" ? "bin/markdown-api.exe" : "bin/markdown-api", et = () => {
388
- const e = process.env.MARKDOWN_API_BINARY;
389
- if (e && A(e))
390
- return e;
287
+ }, x = () => `@aiquants/markdown-${process.platform}-${process.arch}`, A = () => process.platform === "win32" ? "bin/markdown-api.exe" : "bin/markdown-api", V = () => {
288
+ const r = process.env.MARKDOWN_API_BINARY;
289
+ if (r && g(r))
290
+ return r;
391
291
  try {
392
- const r = W(M.join($(), "index.js")).resolve(`${C()}/${x()}`);
393
- if (A(r))
394
- return r;
292
+ const e = L(T.join(N(), "index.js")).resolve(`${x()}/${A()}`);
293
+ if (g(e))
294
+ return e;
395
295
  } catch {
396
296
  }
397
- const t = M.join($(), "..", "..", "go", "markdown-api", "dist", M.basename(x()));
398
- return A(t) ? t : void 0;
399
- }, rt = () => [
297
+ const t = T.join(N(), "..", "..", "go", "markdown-api", "dist", T.basename(A()));
298
+ return g(t) ? t : void 0;
299
+ }, Y = () => [
400
300
  `markdown-api binary not found for ${process.platform}-${process.arch}.`,
401
- `Expected the optional dependency ${C()} to be installed.`,
301
+ `Expected the optional dependency ${x()} to be installed.`,
402
302
  "If your platform is unsupported, build the service yourself",
403
303
  "(https://github.com/fehde-k/aiquants/tree/main/packages/go/markdown-api)",
404
304
  "and point MARKDOWN_API_BINARY at it."
405
- ].join(" "), D = () => new Promise((e) => {
406
- const t = B.connect(g);
305
+ ].join(" "), J = () => new Promise((r) => {
306
+ const t = $.connect(p);
407
307
  t.on("connect", () => {
408
- t.destroy(), e(!0);
409
- }), t.on("error", () => e(!1));
410
- }), F = async (e) => {
308
+ t.destroy(), r(!0);
309
+ }), t.on("error", () => r(!1));
310
+ }), B = async (r) => {
411
311
  const t = Date.now();
412
- for (; Date.now() - t < e; ) {
413
- if (await D()) return !0;
414
- await new Promise((r) => setTimeout(r, 100));
312
+ for (; Date.now() - t < r; ) {
313
+ if (await J()) return !0;
314
+ await new Promise((e) => setTimeout(e, 100));
415
315
  }
416
316
  return !1;
417
317
  };
418
- let P = null, S = null;
419
- const at = 2e3, nt = () => {
420
- const e = Number(process.env.MARKDOWN_API_STARTUP_TIMEOUT_MS);
421
- return Number.isFinite(e) && e > 0 ? e : 1e4;
318
+ let w = null, k = null;
319
+ const X = 2e3, H = () => {
320
+ const r = Number(process.env.MARKDOWN_API_STARTUP_TIMEOUT_MS);
321
+ return Number.isFinite(r) && r > 0 ? r : 1e4;
422
322
  };
423
- let R = null;
424
- const ot = async () => {
425
- if (await D()) return !0;
426
- if (S) return S;
427
- S = (async () => {
428
- const t = et();
323
+ let M = null;
324
+ const Q = async () => {
325
+ if (await J()) return !0;
326
+ if (k) return k;
327
+ k = (async () => {
328
+ const t = V();
429
329
  if (!t)
430
- return await F(at) ? !0 : (R = rt(), !1);
431
- R = null;
330
+ return await B(X) ? !0 : (M = Y(), !1);
331
+ M = null;
432
332
  try {
433
- A(g) && q(g);
333
+ g(p) && E(p);
434
334
  } catch {
435
335
  }
436
336
  try {
437
- P = j(t, [], { stdio: "ignore" });
438
- const r = () => {
439
- if (P) {
337
+ w = I(t, [], { stdio: "ignore" });
338
+ const e = () => {
339
+ if (w) {
440
340
  try {
441
- P.kill("SIGTERM");
341
+ w.kill("SIGTERM");
442
342
  } catch {
443
343
  }
444
- P = null;
344
+ w = null;
445
345
  }
446
346
  };
447
- return process.once("exit", r), await F(nt()) ? !0 : (console.error("markdown-api Go server did not become ready on socket:", g), r(), process.removeListener("exit", r), !1);
448
- } catch (r) {
449
- return console.error("Failed to spawn markdown-api Go server:", r), P = null, !1;
347
+ return process.once("exit", e), await B(H()) ? !0 : (console.error("markdown-api Go server did not become ready on socket:", p), e(), process.removeListener("exit", e), !1);
348
+ } catch (e) {
349
+ return console.error("Failed to spawn markdown-api Go server:", e), w = null, !1;
450
350
  }
451
351
  })();
452
- const e = await S;
453
- return e || (S = null), e;
454
- }, vt = async (e, t, r, l, s = !0) => {
455
- if (!await ot()) {
456
- const T = R ? ` ${R}` : "";
457
- throw new Error(`markdown service is not reachable on ${g}.${T}`);
352
+ const r = await k;
353
+ return r || (k = null), r;
354
+ }, mt = async (r, t, e, l, a = !0) => {
355
+ if (!await Q()) {
356
+ const b = M ? ` ${M}` : "";
357
+ throw new Error(`markdown service is not reachable on ${p}.${b}`);
458
358
  }
459
- let o = !0, n, a, c, i, m, u;
460
- typeof s == "boolean" ? o = s : s && typeof s == "object" && (o = s.validateExistence ?? !0, n = s.enableSubscript, a = s.enableSuperscript, c = s.enableMark, i = s.enableTableMerge, m = s.enableEmbeds, u = s.enableZennDirectives);
461
- const f = {};
462
- l && (f.Authorization = `Bearer ${l}`);
463
- const p = await tt.convert(
359
+ let n = !0, s, o, y, c, m, S;
360
+ typeof a == "boolean" ? n = a : a && typeof a == "object" && (n = a.validateExistence ?? !0, s = a.enableSubscript, o = a.enableSuperscript, y = a.enableMark, c = a.enableTableMerge, m = a.enableEmbeds, S = a.enableZennDirectives);
361
+ const P = {};
362
+ l && (P.Authorization = `Bearer ${l}`);
363
+ const _ = await z.convert(
464
364
  {
465
- markdownText: e,
365
+ markdownText: r,
466
366
  filePath: t,
467
- workspaceRoot: r,
468
- validateExistence: o,
469
- enableSubscript: n,
470
- enableSuperscript: a,
471
- enableMark: c,
472
- enableTableMerge: i,
367
+ workspaceRoot: e,
368
+ validateExistence: n,
369
+ enableSubscript: s,
370
+ enableSuperscript: o,
371
+ enableMark: y,
372
+ enableTableMerge: c,
473
373
  enableEmbeds: m,
474
- enableZennDirectives: u
374
+ enableZennDirectives: S
475
375
  },
476
- { headers: f }
376
+ { headers: P }
477
377
  );
478
378
  return {
479
- htmlContent: p.htmlContent,
480
- headings: p.headings.map((T) => ({
481
- text: T.text,
482
- depth: T.depth,
483
- id: T.id
379
+ htmlContent: _.htmlContent,
380
+ headings: _.headings.map((b) => ({
381
+ text: b.text,
382
+ depth: b.depth,
383
+ id: b.id
484
384
  }))
485
385
  };
486
386
  };
487
387
  export {
488
- H as fetchSiteMetadata,
489
- vt as parseMarkdown,
490
- yt as remarkBlockLinkMetadata,
491
- gt as remarkImportDirective,
492
- Tt as remarkResolveUriAsync,
493
- _ as transformUriAsync
388
+ U as fetchSiteMetadata,
389
+ mt as parseMarkdown,
390
+ lt as remarkBlockLinkMetadata
494
391
  };
495
392
  //# sourceMappingURL=index.server.js.map