@openmoca/moca-convert 0.0.0-stage → 0.7.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.
- package/LICENSE +201 -0
- package/README.md +191 -2
- package/bin/moca-convert.js +198 -0
- package/bin/moca-youtube.js +93 -0
- package/lib/adapters/directory.js +134 -0
- package/lib/adapters/index.js +60 -0
- package/lib/adapters/markdown.js +65 -0
- package/lib/adapters/obsidian.js +206 -0
- package/lib/adapters/openapi/parse.js +143 -0
- package/lib/adapters/openapi/render.js +149 -0
- package/lib/adapters/openapi/suitability.js +41 -0
- package/lib/adapters/openapi.js +79 -0
- package/lib/adapters/pdf.js +309 -0
- package/lib/adapters/youtube.js +259 -0
- package/lib/detect.js +128 -0
- package/lib/frontmatter.js +42 -0
- package/lib/glob.js +33 -0
- package/lib/index.js +4 -0
- package/lib/manifest.js +23 -0
- package/lib/pdf/extract.js +206 -0
- package/lib/pdf/layout.js +270 -0
- package/lib/slug.js +19 -0
- package/lib/target.js +35 -0
- package/lib/text.js +114 -0
- package/lib/walk.js +37 -0
- package/lib/write.js +123 -0
- package/lib/youtube/capture.js +150 -0
- package/lib/youtube/json3-to-vtt.js +109 -0
- package/lib/youtube/ytdlp.js +64 -0
- package/package.json +42 -4
package/lib/detect.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Auto-detection of --from when the caller doesn't pass it explicitly.
|
|
2
|
+
// Ambiguous input is always a refusal (UsageError, mapped to exit code 2 by
|
|
3
|
+
// the CLI) -- this module never guesses.
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { UsageError } from './target.js';
|
|
6
|
+
import { walkFiles } from './walk.js';
|
|
7
|
+
import { ADAPTER_NAMES, getAdapter } from './adapters/index.js';
|
|
8
|
+
import { parseOpenApiDocument } from './adapters/openapi/parse.js';
|
|
9
|
+
|
|
10
|
+
const OPENAPI_JSON_YAML_EXT = new Set(['.json', '.yaml', '.yml']);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {import('./target.js').UsageError | { inputPath: string, isGlob: boolean, isDirectory: boolean, isFile: boolean }} target
|
|
14
|
+
* @param {string} [explicitFrom]
|
|
15
|
+
* @returns {string} one of ADAPTER_NAMES
|
|
16
|
+
*/
|
|
17
|
+
export function resolveAdapterName(target, explicitFrom) {
|
|
18
|
+
if (explicitFrom) {
|
|
19
|
+
if (!ADAPTER_NAMES.includes(explicitFrom)) {
|
|
20
|
+
throw new UsageError(`Unknown --from "${explicitFrom}"; expected one of: ${ADAPTER_NAMES.join(', ')}.`);
|
|
21
|
+
}
|
|
22
|
+
return explicitFrom;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (target.isGlob) {
|
|
26
|
+
throw new UsageError(
|
|
27
|
+
`"${target.inputPath}" looks like a glob pattern; auto-detection only supports a single file or directory. Pass --from markdown explicitly.`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (target.isDirectory) {
|
|
32
|
+
return detectDirectory(target.inputPath);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (target.isFile) {
|
|
36
|
+
return detectFile(target.inputPath);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw new UsageError(`Cannot determine input format for "${target.inputPath}".`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function detectDirectory(inputPath) {
|
|
43
|
+
if (getAdapter('youtube').detect(inputPath)) {
|
|
44
|
+
return 'youtube';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (getAdapter('obsidian').detect(inputPath)) {
|
|
48
|
+
return 'obsidian';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (getAdapter('pdf').detect(inputPath)) {
|
|
52
|
+
return 'pdf';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// "Has at least one Markdown file anywhere" is the same test the
|
|
56
|
+
// directory adapter's own detect() runs; it's inlined here (rather than
|
|
57
|
+
// calling it separately) because the file list and count are also needed
|
|
58
|
+
// below for the ambiguity/refusal messages.
|
|
59
|
+
const allFiles = walkFiles(inputPath);
|
|
60
|
+
const markdownFiles = allFiles.filter((f) => f.endsWith('.md'));
|
|
61
|
+
const topLevelOpenApiCandidates = allFiles.filter(
|
|
62
|
+
(f) => !f.includes('/') && OPENAPI_JSON_YAML_EXT.has(extname(f)) && parsesAsOpenApi(join(inputPath, f))
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (markdownFiles.length > 0 && topLevelOpenApiCandidates.length > 0) {
|
|
66
|
+
throw new UsageError(
|
|
67
|
+
`Cannot determine input format for "${inputPath}": found ${markdownFiles.length} Markdown file(s) and an OpenAPI-shaped document ("${topLevelOpenApiCandidates[0]}"). Pass --from explicitly.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (topLevelOpenApiCandidates.length > 0 && markdownFiles.length === 0) {
|
|
72
|
+
return 'openapi';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (markdownFiles.length > 0) {
|
|
76
|
+
return 'directory';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw new UsageError(
|
|
80
|
+
`Cannot determine input format for "${inputPath}": no Markdown files and no recognizable OpenAPI document found. Pass --from explicitly.`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function detectFile(inputPath) {
|
|
85
|
+
if (getAdapter('markdown').detect(inputPath)) {
|
|
86
|
+
return 'markdown';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const ext = extname(inputPath);
|
|
90
|
+
if (ext === '.pdf') {
|
|
91
|
+
return 'pdf';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (OPENAPI_JSON_YAML_EXT.has(ext)) {
|
|
95
|
+
try {
|
|
96
|
+
parseOpenApiDocument(inputPath);
|
|
97
|
+
return 'openapi';
|
|
98
|
+
} catch (err) {
|
|
99
|
+
// A Swagger 2.0 document parses far enough to identify itself as such;
|
|
100
|
+
// surface that specific, actionable message rather than the generic
|
|
101
|
+
// "not recognizable" refusal below.
|
|
102
|
+
if (err instanceof UsageError && /Swagger/.test(err.message)) {
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
throw new UsageError(
|
|
106
|
+
`"${inputPath}" is JSON/YAML but not a recognizable OpenAPI document. Pass --from explicitly.`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
throw new UsageError(
|
|
112
|
+
`Cannot determine input format for "${inputPath}"; expected one of: ${ADAPTER_NAMES.join(', ')}. Pass --from explicitly.`
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function parsesAsOpenApi(absPath) {
|
|
117
|
+
try {
|
|
118
|
+
parseOpenApiDocument(absPath);
|
|
119
|
+
return true;
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function extname(p) {
|
|
126
|
+
const idx = p.lastIndexOf('.');
|
|
127
|
+
return idx === -1 ? '' : p.slice(idx).toLowerCase();
|
|
128
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Shared read helper for adapters that turn an existing Markdown file into a
|
|
2
|
+
// content node. OKF requires every concept document to carry frontmatter with
|
|
3
|
+
// a non-empty `type`; a file that already has one is kept byte for byte, and
|
|
4
|
+
// one that lacks it gets `type` (and a `title` when it has none) added at the
|
|
5
|
+
// top of its frontmatter, leaving the rest untouched.
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { splitFrontmatter, prependFrontmatterKeys } from '@openmoca/moca-core';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} absPath
|
|
11
|
+
* @returns {{ data: object, content: string, raw: string }}
|
|
12
|
+
*/
|
|
13
|
+
export function readContentFile(absPath) {
|
|
14
|
+
const raw = readFileSync(absPath, 'utf8').replace(/\r\n/g, '\n');
|
|
15
|
+
const split = splitFrontmatter(raw);
|
|
16
|
+
const data = split.present && !split.error ? split.data : {};
|
|
17
|
+
return { data, content: split.body, raw };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} raw full file text
|
|
22
|
+
* @param {object} data parsed frontmatter ({} when none)
|
|
23
|
+
* @param {{ type: string, title?: string }} defaults
|
|
24
|
+
* @param {object} [extra] further keys to add after type and title, such as a `moca` block
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
export function ensureOkf(raw, data, { type, title }, extra = {}) {
|
|
28
|
+
const additions = {};
|
|
29
|
+
if (typeof data.type !== 'string' || data.type.trim() === '') additions.type = type;
|
|
30
|
+
if (title && typeof data.title !== 'string') additions.title = title;
|
|
31
|
+
Object.assign(additions, extra);
|
|
32
|
+
return Object.keys(additions).length === 0 ? raw : prependFrontmatterKeys(raw, additions);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const H1 = /^#\s+(.+)$/m;
|
|
36
|
+
|
|
37
|
+
/** Title from frontmatter, first H1, or file name, in that order. */
|
|
38
|
+
export function deriveTitle(data, content, fallback) {
|
|
39
|
+
if (typeof data.title === 'string' && data.title.trim()) return data.title.trim();
|
|
40
|
+
const heading = content.match(H1);
|
|
41
|
+
return heading ? heading[1].trim() : fallback;
|
|
42
|
+
}
|
package/lib/glob.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Minimal glob-to-RegExp for matching already-known relative paths in
|
|
2
|
+
// memory (e.g. --exclude patterns against a vault's note list), as opposed
|
|
3
|
+
// to expanding a glob against the filesystem (see lib/adapters/markdown.js,
|
|
4
|
+
// which uses fs.globSync for that). Same small hand-rolled approach as
|
|
5
|
+
// moca-lint's lib/pack.js --exclude handling, but using a NUL-byte
|
|
6
|
+
// placeholder (rather than a space) for "**" so a pattern that legitimately
|
|
7
|
+
// contains a literal space isn't mis-expanded.
|
|
8
|
+
const DOUBLE_STAR_PLACEHOLDER = String.fromCharCode(0);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} pattern
|
|
12
|
+
* @returns {RegExp}
|
|
13
|
+
*/
|
|
14
|
+
export function globToRegExp(pattern) {
|
|
15
|
+
const escaped = pattern
|
|
16
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
17
|
+
.split('**')
|
|
18
|
+
.join(DOUBLE_STAR_PLACEHOLDER)
|
|
19
|
+
.replace(/\*/g, '[^/]*')
|
|
20
|
+
.split(DOUBLE_STAR_PLACEHOLDER)
|
|
21
|
+
.join('.*')
|
|
22
|
+
.replace(/\?/g, '.');
|
|
23
|
+
return new RegExp(`^${escaped}$`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} relPath
|
|
28
|
+
* @param {string[]} patterns
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
export function matchesAny(relPath, patterns) {
|
|
32
|
+
return patterns.some((pattern) => globToRegExp(pattern).test(relPath));
|
|
33
|
+
}
|
package/lib/index.js
ADDED
package/lib/manifest.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Builds the manifest every adapter writes.
|
|
2
|
+
import { UsageError } from './target.js';
|
|
3
|
+
|
|
4
|
+
const URI = /^[A-Za-z][A-Za-z0-9+.-]*:[^\s]+$/;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {{ id?: string, title?: string, version?: string, description?: string, language?: string, license?: string }} o
|
|
8
|
+
*/
|
|
9
|
+
export function buildManifest(o) {
|
|
10
|
+
if (!o.id) throw new UsageError('--id is required.');
|
|
11
|
+
if (!URI.test(o.id)) throw new UsageError(`--id must be an absolute URI (for example https://example.com/kb/support or tag:example.com,2026:kb), got "${o.id}".`);
|
|
12
|
+
if (!o.title) throw new UsageError('--title is required.');
|
|
13
|
+
return {
|
|
14
|
+
$schema: 'https://w3id.org/moca/schemas/v1/manifest.schema.json',
|
|
15
|
+
mocaVersion: '0.7',
|
|
16
|
+
id: o.id,
|
|
17
|
+
version: o.version ?? '1.0.0',
|
|
18
|
+
title: o.title,
|
|
19
|
+
...(o.description ? { description: o.description } : {}),
|
|
20
|
+
...(o.language ? { language: o.language } : {}),
|
|
21
|
+
...(o.license ? { license: o.license } : {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Reads a PDF with pdf.js and returns its text as positioned lines, page by
|
|
2
|
+
// page, plus the page labels and the outline (bookmarks). No layout decisions
|
|
3
|
+
// are made here; lib/pdf/layout.js turns the lines into headings and
|
|
4
|
+
// paragraphs.
|
|
5
|
+
import { getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
6
|
+
import { oneLine } from '../text.js';
|
|
7
|
+
|
|
8
|
+
/** Limits on one PDF: pages, and time to read it. */
|
|
9
|
+
export const PDF_LIMITS = Object.freeze({ maxPages: 2_000, timeoutMs: 10 * 60_000 });
|
|
10
|
+
|
|
11
|
+
export class PdfLimitError extends Error {}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {object} PdfLine
|
|
15
|
+
* @property {string} text
|
|
16
|
+
* @property {number} x left edge, PDF units
|
|
17
|
+
* @property {number} y baseline, PDF units from the bottom of the page
|
|
18
|
+
* @property {number} size font size, PDF units
|
|
19
|
+
* @property {string} font pdf.js font id of the line's first run
|
|
20
|
+
* @property {boolean} [bold] most of the line is set in a bold face
|
|
21
|
+
*
|
|
22
|
+
* @typedef {object} PdfPage
|
|
23
|
+
* @property {number} number 1-based physical page number (RFC 3778 `page=`)
|
|
24
|
+
* @property {string} [label] printed page label, e.g. "4-12"
|
|
25
|
+
* @property {number} height
|
|
26
|
+
* @property {PdfLine[]} lines in the order the page draws them
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} OutlineEntry
|
|
29
|
+
* @property {string} title
|
|
30
|
+
* @property {number} level 1 = top level
|
|
31
|
+
* @property {number} [page] 1-based page the entry points at
|
|
32
|
+
*
|
|
33
|
+
* @typedef {object} PdfText
|
|
34
|
+
* @property {PdfPage[]} pages
|
|
35
|
+
* @property {OutlineEntry[]} outline flattened, in document order
|
|
36
|
+
* @property {string} [title] the document's Title metadata
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {Uint8Array} data
|
|
41
|
+
* @param {{ maxPages?: number, timeoutMs?: number }} [limits] PDF_LIMITS by default
|
|
42
|
+
* @returns {Promise<PdfText>} rejects with PdfLimitError over a limit
|
|
43
|
+
*/
|
|
44
|
+
export async function extractPdf(data, limits = {}) {
|
|
45
|
+
const { maxPages, timeoutMs } = { ...PDF_LIMITS, ...limits };
|
|
46
|
+
let timer;
|
|
47
|
+
let task;
|
|
48
|
+
const timeout = new Promise((_, reject) => {
|
|
49
|
+
timer = setTimeout(() => {
|
|
50
|
+
reject(new PdfLimitError(`reading it took longer than ${Math.round(timeoutMs / 1000)} s`));
|
|
51
|
+
task?.destroy();
|
|
52
|
+
}, timeoutMs);
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
const deadline = Date.now() + timeoutMs;
|
|
56
|
+
return await Promise.race([read(data, { maxPages, deadline, timeoutMs }, (t) => { task = t; }), timeout]);
|
|
57
|
+
} finally {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function read(data, { maxPages, deadline, timeoutMs }, onTask) {
|
|
63
|
+
const task = getDocument({
|
|
64
|
+
data,
|
|
65
|
+
isEvalSupported: false,
|
|
66
|
+
disableFontFace: true,
|
|
67
|
+
useSystemFonts: false,
|
|
68
|
+
verbosity: 0,
|
|
69
|
+
});
|
|
70
|
+
onTask(task);
|
|
71
|
+
const doc = await task.promise;
|
|
72
|
+
try {
|
|
73
|
+
if (doc.numPages > maxPages) throw new PdfLimitError(`it has ${doc.numPages} pages, over the ${maxPages}-page limit`);
|
|
74
|
+
const labels = await doc.getPageLabels().catch(() => null);
|
|
75
|
+
const pages = [];
|
|
76
|
+
for (let n = 1; n <= doc.numPages; n++) {
|
|
77
|
+
if (Date.now() >= deadline) throw new PdfLimitError(`reading it took longer than ${Math.round(timeoutMs / 1000)} s`);
|
|
78
|
+
const page = await doc.getPage(n);
|
|
79
|
+
const { items } = await page.getTextContent();
|
|
80
|
+
const [, , , height] = page.view;
|
|
81
|
+
const bold = await boldFonts(page, items);
|
|
82
|
+
// A page label is the PDF's own text: kept short, on one line, with no control characters.
|
|
83
|
+
const label = oneLine(labels?.[n - 1]);
|
|
84
|
+
pages.push({ number: n, ...(label ? { label } : {}), height, lines: toLines(items, bold) });
|
|
85
|
+
page.cleanup();
|
|
86
|
+
}
|
|
87
|
+
const meta = await doc.getMetadata().catch(() => null);
|
|
88
|
+
const title = typeof meta?.info?.Title === 'string' ? meta.info.Title.trim() : '';
|
|
89
|
+
return { pages, outline: await readOutline(doc), ...(title ? { title } : {}) };
|
|
90
|
+
} finally {
|
|
91
|
+
await task.destroy();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The ids of the page's bold fonts. pdf.js knows a font's PostScript name
|
|
97
|
+
* ("Helvetica,Bold", "ABCDEE+Arial-BoldMT") only once the page's operators
|
|
98
|
+
* have been loaded.
|
|
99
|
+
*/
|
|
100
|
+
async function boldFonts(page, items) {
|
|
101
|
+
const bold = new Set();
|
|
102
|
+
try {
|
|
103
|
+
await page.getOperatorList();
|
|
104
|
+
} catch {
|
|
105
|
+
return bold;
|
|
106
|
+
}
|
|
107
|
+
for (const id of new Set(items.map((i) => i.fontName))) {
|
|
108
|
+
try {
|
|
109
|
+
const font = page.commonObjs.get(id);
|
|
110
|
+
if (font?.bold || font?.black || /bold|black|heavy|semibold|demi/i.test(font?.name ?? '')) bold.add(id);
|
|
111
|
+
} catch {
|
|
112
|
+
// Not loaded: treat as regular.
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return bold;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Joins pdf.js text runs into lines. Runs are taken in drawing order, which
|
|
120
|
+
* follows the reading order of a column or text frame, so two columns are not
|
|
121
|
+
* interleaved. A line ends where pdf.js marks one, or where the baseline moves
|
|
122
|
+
* or the text goes back to the left.
|
|
123
|
+
* @param {object[]} items pdf.js text items
|
|
124
|
+
* @param {Set<string>} [boldIds] font ids set in a bold face
|
|
125
|
+
*/
|
|
126
|
+
export function toLines(items, boldIds = new Set()) {
|
|
127
|
+
const lines = [];
|
|
128
|
+
let line = null;
|
|
129
|
+
for (const item of items) {
|
|
130
|
+
if (typeof item.str !== 'string') continue;
|
|
131
|
+
const [a, b, c, d, x, y] = item.transform;
|
|
132
|
+
const size = Math.hypot(c, d) || Math.hypot(a, b) || item.height || 0;
|
|
133
|
+
if (item.str.trim() !== '') {
|
|
134
|
+
const sameLine = line && Math.abs(y - line.y) < Math.max(line.size, size) * 0.4 && x >= line.end - line.size;
|
|
135
|
+
if (!sameLine) {
|
|
136
|
+
if (line) lines.push(line);
|
|
137
|
+
line = { text: '', x, y, size, font: item.fontName, end: x, chars: 0, boldChars: 0, sizes: new Map() };
|
|
138
|
+
}
|
|
139
|
+
const gap = x - line.end;
|
|
140
|
+
if (line.text && !/\s$/.test(line.text) && !/^\s/.test(item.str) && gap > size * 0.15) line.text += ' ';
|
|
141
|
+
line.text += item.str;
|
|
142
|
+
line.end = x + (item.width ?? 0);
|
|
143
|
+
const weight = item.str.trim().length;
|
|
144
|
+
const key = Math.round(size * 2) / 2;
|
|
145
|
+
line.sizes.set(key, (line.sizes.get(key) ?? 0) + weight);
|
|
146
|
+
line.chars += weight;
|
|
147
|
+
if (boldIds.has(item.fontName)) line.boldChars += weight;
|
|
148
|
+
}
|
|
149
|
+
if (item.hasEOL && line) {
|
|
150
|
+
lines.push(line);
|
|
151
|
+
line = null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (line) lines.push(line);
|
|
155
|
+
return lines
|
|
156
|
+
.map((l) => ({
|
|
157
|
+
text: l.text.replace(/\s+/g, ' ').trim(), x: round(l.x), y: round(l.y), size: dominant(l.sizes), font: l.font,
|
|
158
|
+
...(l.boldChars > l.chars * 0.8 ? { bold: true } : {}),
|
|
159
|
+
}))
|
|
160
|
+
.filter((l) => l.text !== '');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The size that covers the most characters in a line. */
|
|
164
|
+
function dominant(sizes) {
|
|
165
|
+
let best = 0;
|
|
166
|
+
let most = -1;
|
|
167
|
+
for (const [size, n] of sizes) {
|
|
168
|
+
if (n > most || (n === most && size > best)) {
|
|
169
|
+
best = size;
|
|
170
|
+
most = n;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return best;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function readOutline(doc) {
|
|
177
|
+
const tree = await doc.getOutline().catch(() => null);
|
|
178
|
+
if (!tree) return [];
|
|
179
|
+
const out = [];
|
|
180
|
+
const visit = async (entries, level) => {
|
|
181
|
+
for (const entry of entries) {
|
|
182
|
+
const title = String(entry.title ?? '').replace(/\s+/g, ' ').trim();
|
|
183
|
+
const page = await destinationPage(doc, entry.dest);
|
|
184
|
+
if (title) out.push({ title, level, ...(page ? { page } : {}) });
|
|
185
|
+
if (entry.items?.length) await visit(entry.items, level + 1);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
await visit(tree, 1);
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function destinationPage(doc, dest) {
|
|
193
|
+
try {
|
|
194
|
+
const explicit = typeof dest === 'string' ? await doc.getDestination(dest) : dest;
|
|
195
|
+
if (!Array.isArray(explicit) || explicit.length === 0) return undefined;
|
|
196
|
+
const ref = explicit[0];
|
|
197
|
+
const index = typeof ref === 'number' ? ref : await doc.getPageIndex(ref);
|
|
198
|
+
return index + 1;
|
|
199
|
+
} catch {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function round(n) {
|
|
205
|
+
return Math.round(n * 100) / 100;
|
|
206
|
+
}
|