football-docs 0.16.1 → 0.16.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/data/docs.db CHANGED
Binary file
@@ -15,6 +15,13 @@ import { type Env, FetchError, type PaperContext } from "./core.js";
15
15
  import type { PaperRecord } from "./records.js";
16
16
  import { type Section } from "./text.js";
17
17
  export declare const FORMAT = "football-docs/paper-text/v1";
18
+ /**
19
+ * The version of the text extraction. Raise it when extraction changes in a
20
+ * way that makes stored text wrong; open copies saved under an older version
21
+ * are then fetched again. (2: MathML formulas kept once, not twice.) Papers
22
+ * the user supplied are kept, since only they can supply them again.
23
+ */
24
+ export declare const TEXT_VERSION = 2;
18
25
  export type Access = "open" | "user";
19
26
  export type StoredPaper = {
20
27
  format: typeof FORMAT;
@@ -31,6 +38,8 @@ export type StoredPaper = {
31
38
  zotero?: string;
32
39
  sha256?: string;
33
40
  savedAt: string;
41
+ /** TEXT_VERSION when saved; absent on entries saved before versions existed. */
42
+ textVersion?: number;
34
43
  sections: Section[];
35
44
  };
36
45
  /** Passage length for user papers: FOOTBALL_DOCS_PAPERS_PASSAGE_CHARS, 50 to 1000, default 200. */
@@ -19,6 +19,13 @@ import { fileURLToPath } from "node:url";
19
19
  import { apiKey, FetchError, readCacheJson, readCapped, reason, USER_AGENT, writeCacheJson } from "./core.js";
20
20
  import { isPdf, pdfSections, readPdf, splitSections } from "./text.js";
21
21
  export const FORMAT = "football-docs/paper-text/v1";
22
+ /**
23
+ * The version of the text extraction. Raise it when extraction changes in a
24
+ * way that makes stored text wrong; open copies saved under an older version
25
+ * are then fetched again. (2: MathML formulas kept once, not twice.) Papers
26
+ * the user supplied are kept, since only they can supply them again.
27
+ */
28
+ export const TEXT_VERSION = 2;
22
29
  const TEXT_DIR = "text";
23
30
  const INDEX_FILE = "library.json";
24
31
  const MAX_LOCAL_PDF_BYTES = 100 * 1024 * 1024;
@@ -62,19 +69,29 @@ export function canonicalKey(key) {
62
69
  }
63
70
  export function saveEntry(ctx, entry) {
64
71
  const file = fileFor(entry.key);
65
- writeCacheJson(textDir(ctx), file, entry);
72
+ writeCacheJson(textDir(ctx), file, { ...entry, textVersion: TEXT_VERSION });
66
73
  const index = readIndex(ctx);
67
74
  for (const alias of aliasesOf(entry))
68
75
  index.aliases[canonicalKey(alias)] = file;
69
76
  writeCacheJson(ctx.cacheDir, INDEX_FILE, index);
70
77
  }
71
- export function loadEntry(ctx, key) {
78
+ /** The entry filed under a key, whatever its text version. */
79
+ function loadAnyEntry(ctx, key) {
72
80
  const file = readIndex(ctx).aliases[canonicalKey(key)];
73
81
  if (!file)
74
82
  return null;
75
83
  const entry = readCacheJson(textDir(ctx), file);
76
84
  return entry?.format === FORMAT ? entry : null;
77
85
  }
86
+ export function loadEntry(ctx, key) {
87
+ const entry = loadAnyEntry(ctx, key);
88
+ if (!entry)
89
+ return null;
90
+ // An open copy read by an older extractor is read again.
91
+ if (entry.access === "open" && (entry.textVersion ?? 1) < TEXT_VERSION)
92
+ return null;
93
+ return entry;
94
+ }
78
95
  /** User papers whose metadata names this DOI. */
79
96
  export function userEntriesFor(ctx, doi) {
80
97
  const dir = textDir(ctx);
@@ -96,7 +113,7 @@ export function listEntries(ctx) {
96
113
  }
97
114
  /** Remove one entry and its aliases. Returns the removed entry, if any. */
98
115
  export function forgetEntry(ctx, key) {
99
- const entry = loadEntry(ctx, key);
116
+ const entry = loadAnyEntry(ctx, key);
100
117
  if (!entry)
101
118
  return null;
102
119
  const file = fileFor(entry.key);
@@ -12,6 +12,7 @@
12
12
  * - It does not ask the Wayback Machine to save a page; it only reads what is
13
13
  * there and gives the save link.
14
14
  */
15
+ import { parseHTML } from "linkedom";
15
16
  import { type PaperContext, type ServiceLog } from "./core.js";
16
17
  import { type Section } from "./text.js";
17
18
  export type Lookup = (host: string) => Promise<string[]>;
@@ -39,6 +40,7 @@ export type Snapshots = {
39
40
  export declare function snapshotDate(timestamp: string): string;
40
41
  /** The earliest and latest Wayback snapshots of a URL. */
41
42
  export declare function waybackSnapshots(ctx: PaperContext, url: string): Promise<Snapshots>;
43
+ type Doc = ReturnType<typeof parseHTML>["document"];
42
44
  export type WebPage = {
43
45
  title?: string;
44
46
  author?: string;
@@ -51,6 +53,15 @@ export type WebPage = {
51
53
  sections: Section[];
52
54
  text: string;
53
55
  };
56
+ /**
57
+ * Replace each MathML formula with one text form. LaTeXML pages (arXiv HTML)
58
+ * carry every formula twice, as rendered MathML and as a LaTeX annotation, so
59
+ * their text read "13×1013\times 10" and a quote with maths in it could not
60
+ * match. A simple formula becomes its rendered text ("13×10", "M=192"), which
61
+ * is what a reader quotes; one with subscripts, fractions and the like
62
+ * becomes its LaTeX source as inline code, which keeps its structure.
63
+ */
64
+ export declare function flattenMath(document: Doc): void;
54
65
  export declare function extractPage(html: string, pageUrl: string): WebPage;
55
66
  /** The text of a fetched HTML page, PDF or plain-text file. */
56
67
  export declare function pageText(page: FetchedPage): Promise<WebPage & {
@@ -83,3 +94,4 @@ export type WebSourceResult = {
83
94
  isError?: boolean;
84
95
  };
85
96
  export declare function readWebSource(ctx: PaperContext, args: WebSourceArgs, log: ServiceLog, lookup?: Lookup): Promise<WebSourceResult>;
97
+ export {};
@@ -233,9 +233,39 @@ function licenceLink(document) {
233
233
  const cc = Array.from(document.querySelectorAll("a[href*='creativecommons.org/licenses/'], a[href*='creativecommons.org/publicdomain/']"))[0];
234
234
  return cc?.getAttribute("href") ?? undefined;
235
235
  }
236
+ /** MathML elements that give a formula structure plain text cannot show. */
237
+ const STRUCTURED_MATH = "msub, msup, msubsup, mfrac, msqrt, mroot, munder, mover, munderover, mtable, mmultiscripts";
238
+ /**
239
+ * Replace each MathML formula with one text form. LaTeXML pages (arXiv HTML)
240
+ * carry every formula twice, as rendered MathML and as a LaTeX annotation, so
241
+ * their text read "13×1013\times 10" and a quote with maths in it could not
242
+ * match. A simple formula becomes its rendered text ("13×10", "M=192"), which
243
+ * is what a reader quotes; one with subscripts, fractions and the like
244
+ * becomes its LaTeX source as inline code, which keeps its structure.
245
+ */
246
+ export function flattenMath(document) {
247
+ for (const math of Array.from(document.querySelectorAll("math"))) {
248
+ const latex = (math.querySelector('annotation[encoding="application/x-tex"]')?.textContent ??
249
+ math.getAttribute("alttext") ??
250
+ "").trim();
251
+ for (const annotation of Array.from(math.querySelectorAll("annotation, annotation-xml")))
252
+ annotation.remove();
253
+ const rendered = (math.textContent ?? "").replace(/\s+/g, "");
254
+ const structured = latex && math.querySelector(STRUCTURED_MATH);
255
+ if (structured || !rendered) {
256
+ const code = document.createElement("code");
257
+ code.textContent = latex || rendered;
258
+ math.replaceWith(code);
259
+ }
260
+ else {
261
+ math.replaceWith(document.createTextNode(rendered));
262
+ }
263
+ }
264
+ }
236
265
  export function extractPage(html, pageUrl) {
237
266
  const withBase = html.includes("<base ") ? html : html.replace(/(<head[^>]*>)/i, `$1<base href="${pageUrl}">`);
238
267
  const { document } = parseHTML(withBase);
268
+ flattenMath(document);
239
269
  const facts = {
240
270
  title: meta(document, "og:title", "twitter:title", "citation_title") ?? plainText(document.querySelector("title")?.textContent),
241
271
  author: meta(document, "citation_author", "author", "article:author") ?? jsonLdField(document, "author"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "football-docs",
3
- "version": "0.16.1",
3
+ "version": "0.16.2",
4
4
  "description": "Searchable football data provider documentation for AI coding agents. Like Context7 for football data.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@10.34.4",