@openmoca/moca-convert 0.0.0-stage → 0.6.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.
@@ -0,0 +1,194 @@
1
+ // `youtube` adapter: a capture folder written by `moca-youtube` (see
2
+ // lib/youtube/capture.js) becomes one content node per video. The captions
3
+ // travel under media/ and the description under sources/, so every citation
4
+ // can be checked without a network; a playlist also gets a structure.ttl that
5
+ // keeps the videos in playlist order.
6
+ import { existsSync, readFileSync, statSync } from 'node:fs';
7
+ import { join } from 'node:path';
8
+ import { joinFrontmatter } from '@openmoca/moca-core';
9
+ import { buildManifest } from '../manifest.js';
10
+ import { slugify } from '../slug.js';
11
+ import { UsageError } from '../target.js';
12
+ import { escapeInline, escapeText, firstLine, summary, turtleString, uniqueSlug } from '../text.js';
13
+ import { CAPTURE_FILE } from '../youtube/capture.js';
14
+ import { parseVtt } from '../youtube/json3-to-vtt.js';
15
+
16
+ export const name = 'youtube';
17
+ export const DEFAULT_TYPE = 'Video';
18
+
19
+ const MEDIA_FRAGS = 'http://www.w3.org/TR/media-frags/';
20
+ const PARAGRAPH_MS = 60_000;
21
+
22
+ /** @param {string} inputPath */
23
+ export function detect(inputPath) {
24
+ return existsSync(inputPath) && statSync(inputPath).isDirectory() && existsSync(join(inputPath, CAPTURE_FILE));
25
+ }
26
+
27
+ /**
28
+ * @param {{ inputPath: string, options: object }} ctx
29
+ * @returns {import('./index.js').PackageDraft}
30
+ */
31
+ export function convert({ inputPath, options }) {
32
+ if (!detect(inputPath)) throw new UsageError(`${inputPath} is not a YouTube capture folder (no ${CAPTURE_FILE}); create one with moca-youtube.`);
33
+ const capture = readJson(join(inputPath, CAPTURE_FILE));
34
+ if (capture.videos.length === 0) throw new UsageError(`${inputPath} holds no videos.`);
35
+ const playlist = capture.kind === 'playlist';
36
+ const manifest = buildManifest({
37
+ ...options,
38
+ title: options.title ?? capture.title,
39
+ description: options.description ?? summary(capture.description),
40
+ });
41
+ const type = options.type ?? DEFAULT_TYPE;
42
+ const lang = options.language;
43
+ const iri = (fragment) => `${manifest.id}#${fragment}`;
44
+
45
+ const warnings = [];
46
+ const contentNodes = [];
47
+ const files = [];
48
+ const taken = new Set(playlist ? ['index'] : []);
49
+ const videos = capture.videos.map((id) => {
50
+ const dir = join(inputPath, 'videos', id);
51
+ const info = readJson(join(dir, 'info.json'));
52
+ const vttPath = join(dir, 'captions.vtt');
53
+ const vtt = existsSync(vttPath) ? readFileSync(vttPath, 'utf8').replace(/\r\n?/g, '\n') : undefined;
54
+ return { info, vtt, slug: uniqueSlug(slugify(info.title), taken) };
55
+ });
56
+
57
+ for (const { info, vtt, slug } of videos) {
58
+ const captionLang = info.captions?.language ?? lang ?? 'und';
59
+ const captionsPath = `media/${slug}.${captionLang}.vtt`;
60
+ const descriptionPath = `sources/${slug}.description.txt`;
61
+ const description = info.description.replace(/\r\n?/g, '\n').trim();
62
+
63
+ const sources = [{ id: 'video', resource: info.webpage_url, title: `${info.title} (YouTube${info.channel ? `, ${info.channel}` : ''})` }];
64
+ const evidence = [];
65
+ if (description) {
66
+ files.push({ path: descriptionPath, body: `${description}\n` });
67
+ sources.push({ id: 'description', resource: `../${descriptionPath}`, title: 'Video description' });
68
+ evidence.push({ source: 'description', selector: { type: 'TextQuoteSelector', exact: firstLine(description) } });
69
+ }
70
+ const cues = vtt ? parseVtt(vtt) : [];
71
+ if (vtt) {
72
+ files.push({ path: captionsPath, body: vtt });
73
+ sources.push({ id: 'captions', resource: `../${captionsPath}`, title: `${info.captions?.automatic ? 'Automatic captions' : 'Captions'} (${captionLang})` });
74
+ for (const chapter of sections(info, cues)) {
75
+ evidence.push({
76
+ source: 'captions',
77
+ selector: { type: 'FragmentSelector', conformsTo: MEDIA_FRAGS, value: `t=${chapter.start},${chapter.end}` },
78
+ ...(chapter.title ? { note: chapter.title } : {}),
79
+ });
80
+ }
81
+ } else {
82
+ warnings.push({ code: 'YT_NO_CAPTIONS', message: `${info.title} has no captions; its node has no transcript.`, file: `videos/${info.id}` });
83
+ }
84
+
85
+ const data = {
86
+ type,
87
+ title: info.title,
88
+ ...(description ? { description: summary(description) } : {}),
89
+ resource: info.webpage_url,
90
+ sources,
91
+ moca: {
92
+ ...(playlist ? { concepts: [{ iri: iri(`video-${info.id}`), role: 'primary' }] } : {}),
93
+ ...(evidence.length > 0 ? { evidence } : {}),
94
+ },
95
+ };
96
+ if (Object.keys(data.moca).length === 0) delete data.moca;
97
+ contentNodes.push({ path: `content/${slug}.md`, body: joinFrontmatter(data, videoBody(info, description, cues)) });
98
+ }
99
+
100
+ if (playlist) {
101
+ contentNodes.push({ path: 'content/index.md', body: playlistIndex(capture, videos) });
102
+ files.push({ path: 'structure.ttl', body: playlistStructure(capture, videos, { iri, lang, title: manifest.title }) });
103
+ }
104
+ return { manifest, contentNodes, files, warnings };
105
+ }
106
+
107
+ function videoBody(info, description, cues) {
108
+ const facts = [`[Watch on YouTube](${info.webpage_url})`];
109
+ if (info.channel) facts.push(info.channel_url ? `[${escapeInline(info.channel)}](${info.channel_url})` : escapeInline(info.channel));
110
+ if (info.upload_date) facts.push(`published ${info.upload_date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')}`);
111
+ if (info.duration) facts.push(clock(info.duration));
112
+ const out = [`# ${escapeInline(info.title)}`, facts.join(' · ')];
113
+ if (description) out.push('## Description', escapeText(description));
114
+ if (cues.length > 0) {
115
+ out.push('## Transcript');
116
+ for (const section of sections(info, cues)) {
117
+ if (section.title) out.push(`### ${clock(section.start)} ${escapeInline(section.title)}`);
118
+ for (const para of paragraphs(section.cues)) {
119
+ out.push(`[${clock(para.start / 1000)}](https://youtu.be/${info.id}?t=${Math.floor(para.start / 1000)}) ${escapeInline(para.text)}`);
120
+ }
121
+ }
122
+ }
123
+ return `\n${out.join('\n\n')}\n`;
124
+ }
125
+
126
+ /**
127
+ * The video's chapters with the cues each holds (by cue start), or the whole
128
+ * video as one untitled section. Times are whole seconds.
129
+ */
130
+ function sections(info, cues) {
131
+ const lastEnd = Math.ceil((cues.at(-1)?.end ?? 0) / 1000);
132
+ const end = Math.max(info.duration ?? 0, lastEnd);
133
+ const chapters = info.chapters?.length > 0
134
+ ? info.chapters.map((c, i) => ({ start: Math.floor(c.start), end: Math.ceil(c.end ?? end), title: chapterTitle(c.title, i) }))
135
+ : [{ start: 0, end, title: null }];
136
+ return chapters
137
+ .map((c, i) => ({
138
+ ...c,
139
+ cues: cues.filter((cue) => cue.start >= c.start * 1000 && (i === chapters.length - 1 || cue.start < chapters[i + 1].start * 1000)),
140
+ }))
141
+ .filter((c) => c.cues.length > 0 && c.end > c.start);
142
+ }
143
+
144
+ // YouTube names a chapter it had to invent, before the first timestamp in
145
+ // the description, "<Untitled Chapter 1>".
146
+ function chapterTitle(title, index) {
147
+ if (title && !/^<Untitled Chapter \d+>$/.test(title)) return title;
148
+ return index === 0 ? 'Opening' : `Chapter ${index + 1}`;
149
+ }
150
+
151
+ function paragraphs(cues) {
152
+ const out = [];
153
+ for (const cue of cues) {
154
+ const last = out.at(-1);
155
+ if (last && cue.start - last.start < PARAGRAPH_MS) last.text += ` ${cue.text}`;
156
+ else out.push({ start: cue.start, text: cue.text });
157
+ }
158
+ return out;
159
+ }
160
+
161
+ function playlistIndex(capture, videos) {
162
+ const lines = [`# ${escapeInline(capture.title)}`, ''];
163
+ const description = (capture.description ?? '').trim();
164
+ if (description) lines.push(escapeText(description), '');
165
+ lines.push(`Videos in [playlist](${capture.url}) order:`, '');
166
+ videos.forEach(({ info, slug }, i) => lines.push(`${i + 1}. [${escapeInline(info.title)}](${slug}.md)`));
167
+ return `${lines.join('\n')}\n`;
168
+ }
169
+
170
+ function playlistStructure(capture, videos, { iri, lang, title }) {
171
+ const label = (text) => `${turtleString(text)}${lang ? `@${lang}` : ''}`;
172
+ const ref = (fragment) => `<${iri(fragment)}>`;
173
+ const members = videos.map(({ info }) => ref(`video-${info.id}`));
174
+ const blocks = [
175
+ '@prefix skos: <http://www.w3.org/2004/02/skos/core#> .\n@prefix dct: <http://purl.org/dc/terms/> .',
176
+ `${ref('playlist')} a skos:Concept ;\n skos:prefLabel ${label(capture.title ?? title)} ;\n dct:hasPart ${members.join(',\n ')} .`,
177
+ `${ref('playlist-order')} a skos:OrderedCollection ;\n skos:prefLabel ${label(`${capture.title ?? title} (playlist order)`)} ;\n skos:memberList (\n ${members.join('\n ')}\n ) .`,
178
+ ...videos.map(({ info }) => `${ref(`video-${info.id}`)} a skos:Concept ;\n skos:prefLabel ${label(info.title)} .`),
179
+ ];
180
+ return `${blocks.join('\n\n')}\n`;
181
+ }
182
+
183
+ /** Seconds as m:ss or h:mm:ss, as YouTube shows them. */
184
+ function clock(seconds) {
185
+ const s = Math.floor(seconds);
186
+ const h = Math.floor(s / 3600);
187
+ const m = Math.floor((s % 3600) / 60);
188
+ const ss = String(s % 60).padStart(2, '0');
189
+ return h > 0 ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`;
190
+ }
191
+
192
+ function readJson(path) {
193
+ return JSON.parse(readFileSync(path, 'utf8'));
194
+ }
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
@@ -0,0 +1,4 @@
1
+ // Public entry point for @openmoca/moca-convert.
2
+ export { resolveAdapterName } from './detect.js';
3
+ export { resolveInputTarget, isGlobPattern, UsageError } from './target.js';
4
+ export { writeDraft, ConversionFailedError } from './write.js';
@@ -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.6',
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,176 @@
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
+
7
+ /**
8
+ * @typedef {object} PdfLine
9
+ * @property {string} text
10
+ * @property {number} x left edge, PDF units
11
+ * @property {number} y baseline, PDF units from the bottom of the page
12
+ * @property {number} size font size, PDF units
13
+ * @property {string} font pdf.js font id of the line's first run
14
+ * @property {boolean} [bold] most of the line is set in a bold face
15
+ *
16
+ * @typedef {object} PdfPage
17
+ * @property {number} number 1-based physical page number (RFC 3778 `page=`)
18
+ * @property {string} [label] printed page label, e.g. "4-12"
19
+ * @property {number} height
20
+ * @property {PdfLine[]} lines in the order the page draws them
21
+ *
22
+ * @typedef {object} OutlineEntry
23
+ * @property {string} title
24
+ * @property {number} level 1 = top level
25
+ * @property {number} [page] 1-based page the entry points at
26
+ *
27
+ * @typedef {object} PdfText
28
+ * @property {PdfPage[]} pages
29
+ * @property {OutlineEntry[]} outline flattened, in document order
30
+ * @property {string} [title] the document's Title metadata
31
+ */
32
+
33
+ /**
34
+ * @param {Uint8Array} data
35
+ * @returns {Promise<PdfText>}
36
+ */
37
+ export async function extractPdf(data) {
38
+ const task = getDocument({
39
+ data,
40
+ isEvalSupported: false,
41
+ disableFontFace: true,
42
+ useSystemFonts: false,
43
+ verbosity: 0,
44
+ });
45
+ const doc = await task.promise;
46
+ try {
47
+ const labels = await doc.getPageLabels().catch(() => null);
48
+ const pages = [];
49
+ for (let n = 1; n <= doc.numPages; n++) {
50
+ const page = await doc.getPage(n);
51
+ const { items } = await page.getTextContent();
52
+ const [, , , height] = page.view;
53
+ const bold = await boldFonts(page, items);
54
+ pages.push({ number: n, ...(labels?.[n - 1] ? { label: labels[n - 1] } : {}), height, lines: toLines(items, bold) });
55
+ page.cleanup();
56
+ }
57
+ const meta = await doc.getMetadata().catch(() => null);
58
+ const title = typeof meta?.info?.Title === 'string' ? meta.info.Title.trim() : '';
59
+ return { pages, outline: await readOutline(doc), ...(title ? { title } : {}) };
60
+ } finally {
61
+ await task.destroy();
62
+ }
63
+ }
64
+
65
+ /**
66
+ * The ids of the page's bold fonts. pdf.js knows a font's PostScript name
67
+ * ("Helvetica,Bold", "ABCDEE+Arial-BoldMT") only once the page's operators
68
+ * have been loaded.
69
+ */
70
+ async function boldFonts(page, items) {
71
+ const bold = new Set();
72
+ try {
73
+ await page.getOperatorList();
74
+ } catch {
75
+ return bold;
76
+ }
77
+ for (const id of new Set(items.map((i) => i.fontName))) {
78
+ try {
79
+ const font = page.commonObjs.get(id);
80
+ if (font?.bold || font?.black || /bold|black|heavy|semibold|demi/i.test(font?.name ?? '')) bold.add(id);
81
+ } catch {
82
+ // Not loaded: treat as regular.
83
+ }
84
+ }
85
+ return bold;
86
+ }
87
+
88
+ /**
89
+ * Joins pdf.js text runs into lines. Runs are taken in drawing order, which
90
+ * follows the reading order of a column or text frame, so two columns are not
91
+ * interleaved. A line ends where pdf.js marks one, or where the baseline moves
92
+ * or the text goes back to the left.
93
+ * @param {object[]} items pdf.js text items
94
+ * @param {Set<string>} [boldIds] font ids set in a bold face
95
+ */
96
+ export function toLines(items, boldIds = new Set()) {
97
+ const lines = [];
98
+ let line = null;
99
+ for (const item of items) {
100
+ if (typeof item.str !== 'string') continue;
101
+ const [a, b, c, d, x, y] = item.transform;
102
+ const size = Math.hypot(c, d) || Math.hypot(a, b) || item.height || 0;
103
+ if (item.str.trim() !== '') {
104
+ const sameLine = line && Math.abs(y - line.y) < Math.max(line.size, size) * 0.4 && x >= line.end - line.size;
105
+ if (!sameLine) {
106
+ if (line) lines.push(line);
107
+ line = { text: '', x, y, size, font: item.fontName, end: x, chars: 0, boldChars: 0, sizes: new Map() };
108
+ }
109
+ const gap = x - line.end;
110
+ if (line.text && !/\s$/.test(line.text) && !/^\s/.test(item.str) && gap > size * 0.15) line.text += ' ';
111
+ line.text += item.str;
112
+ line.end = x + (item.width ?? 0);
113
+ const weight = item.str.trim().length;
114
+ const key = Math.round(size * 2) / 2;
115
+ line.sizes.set(key, (line.sizes.get(key) ?? 0) + weight);
116
+ line.chars += weight;
117
+ if (boldIds.has(item.fontName)) line.boldChars += weight;
118
+ }
119
+ if (item.hasEOL && line) {
120
+ lines.push(line);
121
+ line = null;
122
+ }
123
+ }
124
+ if (line) lines.push(line);
125
+ return lines
126
+ .map((l) => ({
127
+ text: l.text.replace(/\s+/g, ' ').trim(), x: round(l.x), y: round(l.y), size: dominant(l.sizes), font: l.font,
128
+ ...(l.boldChars > l.chars * 0.8 ? { bold: true } : {}),
129
+ }))
130
+ .filter((l) => l.text !== '');
131
+ }
132
+
133
+ /** The size that covers the most characters in a line. */
134
+ function dominant(sizes) {
135
+ let best = 0;
136
+ let most = -1;
137
+ for (const [size, n] of sizes) {
138
+ if (n > most || (n === most && size > best)) {
139
+ best = size;
140
+ most = n;
141
+ }
142
+ }
143
+ return best;
144
+ }
145
+
146
+ async function readOutline(doc) {
147
+ const tree = await doc.getOutline().catch(() => null);
148
+ if (!tree) return [];
149
+ const out = [];
150
+ const visit = async (entries, level) => {
151
+ for (const entry of entries) {
152
+ const title = String(entry.title ?? '').replace(/\s+/g, ' ').trim();
153
+ const page = await destinationPage(doc, entry.dest);
154
+ if (title) out.push({ title, level, ...(page ? { page } : {}) });
155
+ if (entry.items?.length) await visit(entry.items, level + 1);
156
+ }
157
+ };
158
+ await visit(tree, 1);
159
+ return out;
160
+ }
161
+
162
+ async function destinationPage(doc, dest) {
163
+ try {
164
+ const explicit = typeof dest === 'string' ? await doc.getDestination(dest) : dest;
165
+ if (!Array.isArray(explicit) || explicit.length === 0) return undefined;
166
+ const ref = explicit[0];
167
+ const index = typeof ref === 'number' ? ref : await doc.getPageIndex(ref);
168
+ return index + 1;
169
+ } catch {
170
+ return undefined;
171
+ }
172
+ }
173
+
174
+ function round(n) {
175
+ return Math.round(n * 100) / 100;
176
+ }