@abraca/wiki 2.88.0 → 2.91.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/package.json +5 -11
- package/src/connect.ts +0 -66
- package/src/index.ts +0 -508
- package/src/parser.ts +0 -62
- package/src/render.ts +0 -91
- package/src/snapshot.ts +0 -210
- package/src/types.ts +0 -45
- package/src/wikipedia.ts +0 -154
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abraca/wiki",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.91.0",
|
|
4
4
|
"description": "Wikipedia → Abracadabra importer — fetch articles into a graph of CRDT docs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,17 +14,11 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"exports": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"default": {
|
|
21
|
-
"import": "./dist/abracadabra-wiki.esm.js",
|
|
22
|
-
"require": "./dist/abracadabra-wiki.cjs",
|
|
23
|
-
"types": "./dist/index.d.ts"
|
|
24
|
-
}
|
|
17
|
+
"import": "./dist/abracadabra-wiki.esm.js",
|
|
18
|
+
"require": "./dist/abracadabra-wiki.cjs",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
25
20
|
},
|
|
26
21
|
"files": [
|
|
27
|
-
"src",
|
|
28
22
|
"dist"
|
|
29
23
|
],
|
|
30
24
|
"dependencies": {
|
|
@@ -37,6 +31,6 @@
|
|
|
37
31
|
"yjs": "^13.6.8"
|
|
38
32
|
},
|
|
39
33
|
"devDependencies": {
|
|
40
|
-
"@abraca/dabra": "2.
|
|
34
|
+
"@abraca/dabra": "2.91.0"
|
|
41
35
|
}
|
|
42
36
|
}
|
package/src/connect.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Open a DocumentManager session for the wiki command, mirroring the
|
|
3
|
-
* auth/register flow that CLIConnection uses but using the modern public API.
|
|
4
|
-
*
|
|
5
|
-
* Uses the shared `@abraca/dabra` Node identity helpers (loadOrCreateKeyFile,
|
|
6
|
-
* loginOrRegisterWithKey) against `~/.abracadabra/cli.key`, so the wiki command
|
|
7
|
-
* authenticates with the same identity as every other CLI subcommand.
|
|
8
|
-
*/
|
|
9
|
-
import { DocumentManager, loadOrCreateKeyFile, loginOrRegisterWithKey } from '@abraca/dabra'
|
|
10
|
-
|
|
11
|
-
export interface OpenSessionConfig {
|
|
12
|
-
url: string
|
|
13
|
-
name?: string
|
|
14
|
-
color?: string
|
|
15
|
-
inviteCode?: string
|
|
16
|
-
keyFile?: string
|
|
17
|
-
/** Suppress informational stderr logging. */
|
|
18
|
-
quiet?: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface OpenSessionResult {
|
|
22
|
-
dm: DocumentManager
|
|
23
|
-
/** Active root doc id (the entry-point space). */
|
|
24
|
-
rootDocId: string
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function openSession(config: OpenSessionConfig): Promise<OpenSessionResult> {
|
|
28
|
-
const keypair = await loadOrCreateKeyFile(config.keyFile, {
|
|
29
|
-
defaultFileName: 'cli.key',
|
|
30
|
-
onCreate: ({ path, publicKeyB64 }) => {
|
|
31
|
-
if (!config.quiet) {
|
|
32
|
-
console.error(`[abracadabra] Generated new keypair at ${path}`)
|
|
33
|
-
console.error(`[abracadabra] Public key: ${publicKeyB64}`)
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const dm = new DocumentManager({
|
|
39
|
-
url: config.url,
|
|
40
|
-
name: config.name ?? 'Wiki Extractor',
|
|
41
|
-
color: config.color,
|
|
42
|
-
quiet: config.quiet,
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
// Authenticate first; register on first run.
|
|
46
|
-
await loginOrRegisterWithKey(dm.client, keypair, {
|
|
47
|
-
username: (config.name ?? 'wiki-extractor').replace(/\s+/g, '-').toLowerCase(),
|
|
48
|
-
displayName: config.name ?? 'Wiki Extractor',
|
|
49
|
-
deviceName: 'CLI Wiki',
|
|
50
|
-
inviteCode: config.inviteCode,
|
|
51
|
-
onRegister: () => {
|
|
52
|
-
if (!config.quiet) {
|
|
53
|
-
console.error('[abracadabra] Key not registered, creating new account...')
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
await dm.connect()
|
|
59
|
-
|
|
60
|
-
const rootDocId = dm.rootDocId
|
|
61
|
-
if (!rootDocId) {
|
|
62
|
-
throw new Error('Connected but no rootDocId — server has no spaces.')
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return { dm, rootDocId }
|
|
66
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,508 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* `abracadabra-wiki <title>` — fetch Wikipedia articles and seed them into the
|
|
4
|
-
* active Abracadabra space as a graph of docs.
|
|
5
|
-
*
|
|
6
|
-
* Streaming flow (no buffering): every newly-discovered title becomes a shell
|
|
7
|
-
* doc immediately (visible in the dashboard right away), then bodies are filled
|
|
8
|
-
* in one fetch at a time. The user sees the tree skeleton appear before the
|
|
9
|
-
* first body is written.
|
|
10
|
-
*
|
|
11
|
-
* Authenticates with its own Ed25519 key (`ABRA_KEY_FILE`, default
|
|
12
|
-
* `~/.abracadabra/cli.key`) via the modern `DocumentManager` API — the same
|
|
13
|
-
* identity the `abracadabra` CLI uses, so both tools share one account.
|
|
14
|
-
*
|
|
15
|
-
* Usage:
|
|
16
|
-
* abracadabra-wiki "<Article Title>" user-agent="you (you@example.com)" [options]
|
|
17
|
-
*
|
|
18
|
-
* Environment:
|
|
19
|
-
* ABRA_URL Server URL (required unless --dry-run)
|
|
20
|
-
* ABRA_KEY_FILE Ed25519 key file (~/.abracadabra/cli.key)
|
|
21
|
-
* ABRA_NAME / ABRA_COLOR Presence identity
|
|
22
|
-
* ABRA_INVITE_CODE Invite code for first-run registration
|
|
23
|
-
* ABRA_WIKI_USER_AGENT Default Api-User-Agent (or pass user-agent=...)
|
|
24
|
-
*/
|
|
25
|
-
import type { DocumentManager } from '@abraca/dabra'
|
|
26
|
-
import { parseArgs, type ParsedArgs } from './parser.ts'
|
|
27
|
-
import { WikipediaClient } from './wikipedia.ts'
|
|
28
|
-
import { snapshotArticle, canonicalTitle, prettyCategoryLabel } from './snapshot.ts'
|
|
29
|
-
import {
|
|
30
|
-
ICONS,
|
|
31
|
-
pickSectionType,
|
|
32
|
-
renderArticleLead,
|
|
33
|
-
renderArticleSingleDoc,
|
|
34
|
-
renderInfoboxBody,
|
|
35
|
-
renderCategoryBody,
|
|
36
|
-
rewriteLinks,
|
|
37
|
-
} from './render.ts'
|
|
38
|
-
import { openSession } from './connect.ts'
|
|
39
|
-
import type { WikiOptions, ExtractMode, ExtractedArticle, ExtractedSection } from './types.ts'
|
|
40
|
-
|
|
41
|
-
const USAGE = [
|
|
42
|
-
'abracadabra-wiki "<Article Title>" user-agent="<name (email)>" [options]',
|
|
43
|
-
'',
|
|
44
|
-
'Options:',
|
|
45
|
-
' mode=single|split single doc per article OR split into sections+infobox [split]',
|
|
46
|
-
' depth=<n> follow internal links to depth N [1]',
|
|
47
|
-
' category-depth=<n> recurse into sub-categories [1]',
|
|
48
|
-
' lang=<code> wiki language [en]',
|
|
49
|
-
' domain=<host> 3rd-party MediaWiki host (overrides lang)',
|
|
50
|
-
' parent=<docId> parent doc for the new graph [active space root]',
|
|
51
|
-
' user-agent=<str> Api-User-Agent header (REQUIRED by Wikimedia etiquette)',
|
|
52
|
-
' rate=<rps> max wikipedia requests per second [3]',
|
|
53
|
-
' --include-categories expand each article\'s categories into nested graphs',
|
|
54
|
-
' --dry-run fetch only the entry article, print outline, no writes',
|
|
55
|
-
'',
|
|
56
|
-
'Environment: ABRA_URL (required unless --dry-run), ABRA_KEY_FILE, ABRA_NAME,',
|
|
57
|
-
' ABRA_COLOR, ABRA_INVITE_CODE, ABRA_WIKI_USER_AGENT.',
|
|
58
|
-
].join('\n')
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Run a Wikipedia import for already-parsed args. Returns a human-readable
|
|
62
|
-
* summary (or an error/usage string). Exported for programmatic use.
|
|
63
|
-
*/
|
|
64
|
-
export async function runWiki(args: ParsedArgs): Promise<string> {
|
|
65
|
-
const opts = parseOptions(args)
|
|
66
|
-
if (typeof opts === 'string') return opts
|
|
67
|
-
|
|
68
|
-
const log = (msg: string) => {
|
|
69
|
-
if (!args.flags.has('quiet') && !args.flags.has('q')) {
|
|
70
|
-
console.error(`[wiki] ${msg}`)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const wp = new WikipediaClient({
|
|
75
|
-
lang: opts.lang,
|
|
76
|
-
domain: opts.domain,
|
|
77
|
-
userAgent: opts.userAgent,
|
|
78
|
-
rate: opts.rate,
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
if (opts.dryRun) {
|
|
82
|
-
// Dry-run: fetch only the entry, print its outline, no server.
|
|
83
|
-
log(`fetch ${opts.title}`)
|
|
84
|
-
const doc = await wp.fetchArticle(opts.title)
|
|
85
|
-
if (!doc) return `Article not found: "${opts.title}"`
|
|
86
|
-
const snap = snapshotArticle(doc, canonicalTitle(doc.title?.() ?? opts.title))
|
|
87
|
-
return [
|
|
88
|
-
`Entry: ${snap.title}`,
|
|
89
|
-
`URL: ${snap.url ?? '(none)'}`,
|
|
90
|
-
`Internal links: ${snap.linkTitles.length}`,
|
|
91
|
-
`Categories: ${snap.categories.length}`,
|
|
92
|
-
`Sections: ${snap.sections.length}`,
|
|
93
|
-
`Has infobox: ${snap.infobox && snap.infobox.length > 0 ? 'yes' : 'no'}`,
|
|
94
|
-
'',
|
|
95
|
-
'── Sections ──',
|
|
96
|
-
printSections(snap.sections, ''),
|
|
97
|
-
].join('\n')
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ── Connect ──────────────────────────────────────────────────────────
|
|
101
|
-
// process.env access uses bracket notation to satisfy noUncheckedIndexedAccess.
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
-
const env = (globalThis as any).process?.env ?? {}
|
|
104
|
-
const url = env['ABRA_URL']
|
|
105
|
-
if (!url) {
|
|
106
|
-
return 'ABRA_URL is required to write to the server. Set it or pass --dry-run.'
|
|
107
|
-
}
|
|
108
|
-
const { dm } = await openSession({
|
|
109
|
-
url,
|
|
110
|
-
name: env['ABRA_NAME'],
|
|
111
|
-
color: env['ABRA_COLOR'],
|
|
112
|
-
inviteCode: env['ABRA_INVITE_CODE'],
|
|
113
|
-
keyFile: env['ABRA_KEY_FILE'],
|
|
114
|
-
quiet: args.flags.has('quiet') || args.flags.has('q'),
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
const result = await runStreaming(dm, wp, opts, log)
|
|
119
|
-
return [
|
|
120
|
-
`Done. Created ${result.articleCount} articles${
|
|
121
|
-
result.categoryCount > 0 ? ` + ${result.categoryCount} categories` : ''
|
|
122
|
-
}.`,
|
|
123
|
-
`Root: ${result.rootDocId}`,
|
|
124
|
-
].join('\n')
|
|
125
|
-
} finally {
|
|
126
|
-
await dm.destroy().catch(() => {})
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
131
|
-
// Streaming orchestrator
|
|
132
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
133
|
-
|
|
134
|
-
interface StreamResult {
|
|
135
|
-
rootDocId: string
|
|
136
|
-
articleCount: number
|
|
137
|
-
categoryCount: number
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
async function runStreaming(
|
|
141
|
-
dm: DocumentManager,
|
|
142
|
-
wp: WikipediaClient,
|
|
143
|
-
opts: WikiOptions,
|
|
144
|
-
log: (msg: string) => void,
|
|
145
|
-
): Promise<StreamResult> {
|
|
146
|
-
// Title → docId map. Drives [[Title]] → [[docId|label]] rewriting at write time.
|
|
147
|
-
const titleToDocId = new Map<string, string>()
|
|
148
|
-
// Snapshots of articles we've already fetched (avoid re-fetching).
|
|
149
|
-
const fetched = new Map<string, ExtractedArticle>()
|
|
150
|
-
// Articles whose section/infobox children have been created (split mode).
|
|
151
|
-
const childrenCreated = new Set<string>()
|
|
152
|
-
// Categories whose shells have been created.
|
|
153
|
-
const categoryToDocId = new Map<string, string>()
|
|
154
|
-
let categoriesContainerId: string | null = null
|
|
155
|
-
|
|
156
|
-
// ── Fetch entry first; we need its title to label the root graph ─────
|
|
157
|
-
log(`fetch ${opts.title}`)
|
|
158
|
-
const entryDoc = await wp.fetchArticle(opts.title)
|
|
159
|
-
if (!entryDoc) {
|
|
160
|
-
throw new Error(`Article not found: "${opts.title}"`)
|
|
161
|
-
}
|
|
162
|
-
const entryTitle = canonicalTitle(entryDoc.title?.() ?? opts.title)
|
|
163
|
-
const entrySnap = snapshotArticle(entryDoc, entryTitle)
|
|
164
|
-
fetched.set(entryTitle, entrySnap)
|
|
165
|
-
|
|
166
|
-
// ── Step 1: create root graph doc (visible immediately) ──────────────
|
|
167
|
-
const rootEntry = dm.tree.create({
|
|
168
|
-
parentId: opts.parentDocId ?? null,
|
|
169
|
-
label: entryTitle,
|
|
170
|
-
type: 'graph',
|
|
171
|
-
meta: { icon: ICONS.graph },
|
|
172
|
-
})
|
|
173
|
-
log(`+ ${rootEntry.id.slice(0, 8)}… ${entryTitle} (graph)`)
|
|
174
|
-
|
|
175
|
-
// ── Step 2: create the entry article shell ───────────────────────────
|
|
176
|
-
const entryArticleId = createArticleShell(dm, entrySnap, rootEntry.id, log)
|
|
177
|
-
titleToDocId.set(entryTitle, entryArticleId)
|
|
178
|
-
|
|
179
|
-
// Queue of (title, depth) to process. Each entry is guaranteed to have
|
|
180
|
-
// a shell doc already in titleToDocId.
|
|
181
|
-
const queue: Array<{ title: string; depth: number }> = [{ title: entryTitle, depth: 0 }]
|
|
182
|
-
let articleCount = 0
|
|
183
|
-
|
|
184
|
-
// ── Step 3: streaming process ───────────────────────────────────────
|
|
185
|
-
while (queue.length > 0) {
|
|
186
|
-
const { title, depth } = queue.shift()!
|
|
187
|
-
const articleDocId = titleToDocId.get(title)!
|
|
188
|
-
|
|
189
|
-
// Ensure we've fetched this article.
|
|
190
|
-
let snap = fetched.get(title)
|
|
191
|
-
if (!snap) {
|
|
192
|
-
log(`fetch [d${depth}] ${title}`)
|
|
193
|
-
try {
|
|
194
|
-
const doc = await wp.fetchArticle(title)
|
|
195
|
-
if (!doc) {
|
|
196
|
-
log(` not found — leaving stub`)
|
|
197
|
-
continue
|
|
198
|
-
}
|
|
199
|
-
snap = snapshotArticle(doc, canonicalTitle(doc.title?.() ?? title))
|
|
200
|
-
fetched.set(title, snap)
|
|
201
|
-
} catch (err: any) {
|
|
202
|
-
log(`! fetch failed: ${err?.message ?? err}`)
|
|
203
|
-
continue
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// Create this article's section/infobox children (split mode only,
|
|
208
|
-
// and only once per article).
|
|
209
|
-
if (opts.mode === 'split' && !childrenCreated.has(title)) {
|
|
210
|
-
createArticleChildren(dm, snap, articleDocId, log)
|
|
211
|
-
childrenCreated.add(title)
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Discover new linked titles and pre-allocate shells immediately.
|
|
215
|
-
if (depth < opts.depth) {
|
|
216
|
-
for (const linkTitle of snap.linkTitles) {
|
|
217
|
-
if (titleToDocId.has(linkTitle)) continue
|
|
218
|
-
const shell = dm.tree.create({
|
|
219
|
-
parentId: rootEntry.id,
|
|
220
|
-
label: linkTitle,
|
|
221
|
-
type: 'doc',
|
|
222
|
-
meta: { icon: ICONS.article },
|
|
223
|
-
})
|
|
224
|
-
titleToDocId.set(linkTitle, shell.id)
|
|
225
|
-
queue.push({ title: linkTitle, depth: depth + 1 })
|
|
226
|
-
log(`+ ${shell.id.slice(0, 8)}… ${linkTitle} (doc, shell)`)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Pre-allocate category shells when first discovered.
|
|
231
|
-
if (opts.includeCategories && snap.categories.length > 0) {
|
|
232
|
-
if (!categoriesContainerId) {
|
|
233
|
-
const c = dm.tree.create({
|
|
234
|
-
parentId: rootEntry.id,
|
|
235
|
-
label: 'Categories',
|
|
236
|
-
type: 'graph',
|
|
237
|
-
meta: { icon: ICONS.categories },
|
|
238
|
-
})
|
|
239
|
-
categoriesContainerId = c.id
|
|
240
|
-
log(`+ ${c.id.slice(0, 8)}… Categories (graph)`)
|
|
241
|
-
}
|
|
242
|
-
for (const catTitle of snap.categories) {
|
|
243
|
-
if (categoryToDocId.has(catTitle)) continue
|
|
244
|
-
const cat = dm.tree.create({
|
|
245
|
-
parentId: categoriesContainerId,
|
|
246
|
-
label: prettyCategoryLabel(catTitle),
|
|
247
|
-
type: 'graph',
|
|
248
|
-
meta: { icon: ICONS.category },
|
|
249
|
-
})
|
|
250
|
-
categoryToDocId.set(catTitle, cat.id)
|
|
251
|
-
log(`+ ${cat.id.slice(0, 8)}… ${prettyCategoryLabel(catTitle)} (graph, cat)`)
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Write this article's body NOW (links resolve to whatever shells we
|
|
256
|
-
// have allocated so far — that's all of this article's links since we
|
|
257
|
-
// just allocated them above).
|
|
258
|
-
const body =
|
|
259
|
-
opts.mode === 'split' ? renderArticleLead(snap) : renderArticleSingleDoc(snap)
|
|
260
|
-
if (body.trim().length > 0) {
|
|
261
|
-
const rewritten = rewriteLinks(body, titleToDocId)
|
|
262
|
-
try {
|
|
263
|
-
await dm.content.write(articleDocId, rewritten)
|
|
264
|
-
log(`✓ body ${title}`)
|
|
265
|
-
} catch (err: any) {
|
|
266
|
-
log(`! body write failed for ${title}: ${err?.message ?? err}`)
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// In split mode, also write each section/infobox doc body.
|
|
271
|
-
if (opts.mode === 'split') {
|
|
272
|
-
await writeChildrenBodies(dm, snap, articleDocId, titleToDocId, log)
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
articleCount++
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// ── Step 4: fill in category bodies ─────────────────────────────────
|
|
279
|
-
let categoryCount = 0
|
|
280
|
-
if (opts.includeCategories && categoryToDocId.size > 0) {
|
|
281
|
-
for (const [catTitle, catDocId] of categoryToDocId) {
|
|
282
|
-
log(`category ${catTitle}`)
|
|
283
|
-
try {
|
|
284
|
-
const members = await wp.fetchCategoryPages(
|
|
285
|
-
catTitle,
|
|
286
|
-
opts.categoryDepth > 0,
|
|
287
|
-
Math.max(0, opts.categoryDepth),
|
|
288
|
-
)
|
|
289
|
-
const memberArticles: string[] = []
|
|
290
|
-
const subcats: string[] = []
|
|
291
|
-
for (const m of members) {
|
|
292
|
-
if (m.type === 'subcat') subcats.push(prettyCategoryLabel(m.title))
|
|
293
|
-
else memberArticles.push(m.title)
|
|
294
|
-
}
|
|
295
|
-
const body = renderCategoryBody(memberArticles, subcats)
|
|
296
|
-
const rewritten = rewriteLinks(body, titleToDocId)
|
|
297
|
-
if (rewritten.trim().length > 0) {
|
|
298
|
-
await dm.content.write(catDocId, rewritten)
|
|
299
|
-
log(`✓ body category ${catTitle}`)
|
|
300
|
-
}
|
|
301
|
-
categoryCount++
|
|
302
|
-
} catch (err: any) {
|
|
303
|
-
log(`! category ${catTitle}: ${err?.message ?? err}`)
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return { rootDocId: rootEntry.id, articleCount, categoryCount }
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
312
|
-
// Shell + body helpers
|
|
313
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
314
|
-
|
|
315
|
-
function createArticleShell(
|
|
316
|
-
dm: DocumentManager,
|
|
317
|
-
article: ExtractedArticle,
|
|
318
|
-
parentId: string,
|
|
319
|
-
log: (msg: string) => void,
|
|
320
|
-
): string {
|
|
321
|
-
const meta: Record<string, unknown> = { icon: ICONS.article }
|
|
322
|
-
if (article.url) meta.url = article.url
|
|
323
|
-
const entry = dm.tree.create({
|
|
324
|
-
parentId,
|
|
325
|
-
label: article.title,
|
|
326
|
-
type: 'doc',
|
|
327
|
-
meta,
|
|
328
|
-
})
|
|
329
|
-
log(`+ ${entry.id.slice(0, 8)}… ${article.title} (doc)`)
|
|
330
|
-
return entry.id
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Create section + infobox child docs for a split-mode article. Returns nothing
|
|
335
|
-
* — children get bodies written later in writeChildrenBodies.
|
|
336
|
-
*/
|
|
337
|
-
function createArticleChildren(
|
|
338
|
-
dm: DocumentManager,
|
|
339
|
-
article: ExtractedArticle,
|
|
340
|
-
articleDocId: string,
|
|
341
|
-
log: (msg: string) => void,
|
|
342
|
-
): void {
|
|
343
|
-
if (article.infobox && article.infobox.length > 0) {
|
|
344
|
-
const ib = dm.tree.create({
|
|
345
|
-
parentId: articleDocId,
|
|
346
|
-
label: 'Infobox',
|
|
347
|
-
type: 'outline',
|
|
348
|
-
meta: { icon: ICONS.infobox },
|
|
349
|
-
})
|
|
350
|
-
log(` + ${ib.id.slice(0, 8)}… Infobox (outline)`)
|
|
351
|
-
// We attach the docId to the article object so writeChildrenBodies
|
|
352
|
-
// can find it without a second tree query.
|
|
353
|
-
;(article as any)._infoboxDocId = ib.id
|
|
354
|
-
}
|
|
355
|
-
for (const section of article.sections) {
|
|
356
|
-
createSectionShell(dm, section, articleDocId, log)
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
function createSectionShell(
|
|
361
|
-
dm: DocumentManager,
|
|
362
|
-
section: ExtractedSection,
|
|
363
|
-
parentDocId: string,
|
|
364
|
-
log: (msg: string) => void,
|
|
365
|
-
): void {
|
|
366
|
-
const hasChildren = section.children.length > 0
|
|
367
|
-
if (!section.body.trim() && !hasChildren) return
|
|
368
|
-
const { type, icon } = pickSectionType(section)
|
|
369
|
-
const entry = dm.tree.create({
|
|
370
|
-
parentId: parentDocId,
|
|
371
|
-
label: section.title || 'Untitled section',
|
|
372
|
-
type,
|
|
373
|
-
meta: { icon },
|
|
374
|
-
})
|
|
375
|
-
log(` + ${entry.id.slice(0, 8)}… ${entry.label} (${type})`)
|
|
376
|
-
;(section as any)._docId = entry.id
|
|
377
|
-
for (const child of section.children) {
|
|
378
|
-
createSectionShell(dm, child, entry.id, log)
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
async function writeChildrenBodies(
|
|
383
|
-
dm: DocumentManager,
|
|
384
|
-
article: ExtractedArticle,
|
|
385
|
-
_articleDocId: string,
|
|
386
|
-
titleToDocId: Map<string, string>,
|
|
387
|
-
log: (msg: string) => void,
|
|
388
|
-
): Promise<void> {
|
|
389
|
-
const infoboxDocId = (article as any)._infoboxDocId as string | undefined
|
|
390
|
-
if (infoboxDocId && article.infobox && article.infobox.length > 0) {
|
|
391
|
-
try {
|
|
392
|
-
await dm.content.write(infoboxDocId, renderInfoboxBody(article.infobox))
|
|
393
|
-
} catch (err: any) {
|
|
394
|
-
log(`! infobox body write failed: ${err?.message ?? err}`)
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
for (const section of article.sections) {
|
|
398
|
-
await writeSectionBody(dm, section, titleToDocId, log)
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
async function writeSectionBody(
|
|
403
|
-
dm: DocumentManager,
|
|
404
|
-
section: ExtractedSection,
|
|
405
|
-
titleToDocId: Map<string, string>,
|
|
406
|
-
log: (msg: string) => void,
|
|
407
|
-
): Promise<void> {
|
|
408
|
-
const docId = (section as any)._docId as string | undefined
|
|
409
|
-
if (docId && section.body.trim().length > 0) {
|
|
410
|
-
try {
|
|
411
|
-
await dm.content.write(docId, rewriteLinks(section.body, titleToDocId))
|
|
412
|
-
} catch (err: any) {
|
|
413
|
-
log(`! section body write failed for ${section.title}: ${err?.message ?? err}`)
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
for (const child of section.children) {
|
|
417
|
-
await writeSectionBody(dm, child, titleToDocId, log)
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
422
|
-
// Argument parsing + dry-run printing
|
|
423
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
424
|
-
|
|
425
|
-
function parseOptions(args: ParsedArgs): WikiOptions | string {
|
|
426
|
-
const title = args.positional[0]?.trim() || args.params['title']
|
|
427
|
-
if (!title) return 'Missing required positional argument: <title>. Example: abracadabra-wiki "Toronto Raptors"'
|
|
428
|
-
|
|
429
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
430
|
-
const env = (globalThis as any).process?.env ?? {}
|
|
431
|
-
const userAgent = args.params['user-agent'] || args.params['userAgent'] || env['ABRA_WIKI_USER_AGENT']
|
|
432
|
-
if (!userAgent) {
|
|
433
|
-
return [
|
|
434
|
-
'Missing required parameter: user-agent="your-name (you@example.com)"',
|
|
435
|
-
'(Wikimedia etiquette requires an Api-User-Agent header. Pass user-agent=... or set ABRA_WIKI_USER_AGENT.)',
|
|
436
|
-
].join('\n')
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
const mode = (args.params['mode'] ?? 'split') as ExtractMode
|
|
440
|
-
if (mode !== 'single' && mode !== 'split') {
|
|
441
|
-
return `Invalid mode "${mode}". Use mode=single or mode=split.`
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
const depth = parseIntOr(args.params['depth'], 1)
|
|
445
|
-
const categoryDepth = parseIntOr(args.params['category-depth'] ?? args.params['categoryDepth'], 1)
|
|
446
|
-
const rate = parseFloatOr(args.params['rate'], 3)
|
|
447
|
-
|
|
448
|
-
return {
|
|
449
|
-
title,
|
|
450
|
-
mode,
|
|
451
|
-
depth,
|
|
452
|
-
categoryDepth,
|
|
453
|
-
includeCategories: args.flags.has('include-categories') || args.flags.has('includeCategories'),
|
|
454
|
-
lang: args.params['lang'] ?? 'en',
|
|
455
|
-
domain: args.params['domain'],
|
|
456
|
-
parentDocId: args.params['parent'],
|
|
457
|
-
userAgent,
|
|
458
|
-
rate,
|
|
459
|
-
dryRun: args.flags.has('dry-run') || args.flags.has('dryRun'),
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function parseIntOr(s: string | undefined, fallback: number): number {
|
|
464
|
-
if (!s) return fallback
|
|
465
|
-
const n = Number.parseInt(s, 10)
|
|
466
|
-
return Number.isFinite(n) && n >= 0 ? n : fallback
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function parseFloatOr(s: string | undefined, fallback: number): number {
|
|
470
|
-
if (!s) return fallback
|
|
471
|
-
const n = Number.parseFloat(s)
|
|
472
|
-
return Number.isFinite(n) && n > 0 ? n : fallback
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function printSections(sections: ExtractedSection[], indent: string): string {
|
|
476
|
-
const lines: string[] = []
|
|
477
|
-
for (const s of sections) {
|
|
478
|
-
const hint = s.body ? ` (${s.body.length}b)` : ''
|
|
479
|
-
lines.push(`${indent}- ${s.title}${hint}${s.children.length > 0 ? ` [${s.children.length} sub]` : ''}`)
|
|
480
|
-
if (s.children.length > 0) {
|
|
481
|
-
lines.push(printSections(s.children, indent + ' '))
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
return lines.join('\n')
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
488
|
-
// Bin entry
|
|
489
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
490
|
-
|
|
491
|
-
async function main(): Promise<void> {
|
|
492
|
-
const args = parseArgs(process.argv)
|
|
493
|
-
if (
|
|
494
|
-
args.flags.has('help') ||
|
|
495
|
-
args.flags.has('h') ||
|
|
496
|
-
(!args.positional[0]?.trim() && !args.params['title'])
|
|
497
|
-
) {
|
|
498
|
-
console.log(USAGE)
|
|
499
|
-
return
|
|
500
|
-
}
|
|
501
|
-
const output = await runWiki(args)
|
|
502
|
-
if (output) console.log(output)
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
main().catch((err) => {
|
|
506
|
-
console.error(`Fatal: ${err?.message ?? err}`)
|
|
507
|
-
process.exit(1)
|
|
508
|
-
})
|
package/src/parser.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal argument parser for the `abracadabra-wiki` bin.
|
|
3
|
-
*
|
|
4
|
-
* Single-purpose: there is no subcommand — the first bare word is the article
|
|
5
|
-
* title (`positional[0]`). Supports `key=value`, `key="value with spaces"`,
|
|
6
|
-
* and `--flag` / `--key=value`.
|
|
7
|
-
*
|
|
8
|
-
* abracadabra-wiki "<Article Title>" [key=value ...] [--flags]
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export interface ParsedArgs {
|
|
12
|
-
/** Positional arguments — `positional[0]` is the article title. */
|
|
13
|
-
positional: string[]
|
|
14
|
-
/** Key-value parameters, e.g. `{ lang: "en", "user-agent": "..." }`. */
|
|
15
|
-
params: Record<string, string>
|
|
16
|
-
/** Boolean flags, e.g. `dry-run`, `include-categories`, `quiet`. */
|
|
17
|
-
flags: Set<string>
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Parse CLI arguments into a structured object.
|
|
22
|
-
* @param argv Raw `process.argv` (includes node path and script path).
|
|
23
|
-
*/
|
|
24
|
-
export function parseArgs(argv: string[]): ParsedArgs {
|
|
25
|
-
const args = argv.slice(2) // skip node + script
|
|
26
|
-
const result: ParsedArgs = { positional: [], params: {}, flags: new Set() }
|
|
27
|
-
|
|
28
|
-
for (let i = 0; i < args.length; i++) {
|
|
29
|
-
const arg = args[i]
|
|
30
|
-
|
|
31
|
-
// --flag or --key=value
|
|
32
|
-
if (arg.startsWith('--')) {
|
|
33
|
-
const stripped = arg.slice(2)
|
|
34
|
-
const eqIdx = stripped.indexOf('=')
|
|
35
|
-
if (eqIdx !== -1) {
|
|
36
|
-
result.params[stripped.slice(0, eqIdx)] = stripped.slice(eqIdx + 1)
|
|
37
|
-
} else {
|
|
38
|
-
result.flags.add(stripped)
|
|
39
|
-
}
|
|
40
|
-
continue
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// key=value pair
|
|
44
|
-
const eqIdx = arg.indexOf('=')
|
|
45
|
-
if (eqIdx > 0) {
|
|
46
|
-
const key = arg.slice(0, eqIdx)
|
|
47
|
-
let value = arg.slice(eqIdx + 1)
|
|
48
|
-
// Strip surrounding quotes if present
|
|
49
|
-
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
50
|
-
(value.startsWith("'") && value.endsWith("'"))) {
|
|
51
|
-
value = value.slice(1, -1)
|
|
52
|
-
}
|
|
53
|
-
result.params[key] = value
|
|
54
|
-
continue
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Every bare word is positional (no subcommand for this single-purpose bin).
|
|
58
|
-
result.positional.push(arg)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result
|
|
62
|
-
}
|
package/src/render.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Body rendering + page-type decisions for the streaming orchestrator.
|
|
3
|
-
*
|
|
4
|
-
* All rendering is title-driven: bodies are rendered with `[[Title]]`
|
|
5
|
-
* placeholders, and `rewriteLinks` rewrites them to `[[docId|label]]`
|
|
6
|
-
* using the live title→docId map at write time.
|
|
7
|
-
*/
|
|
8
|
-
import type { ExtractedArticle, ExtractedSection } from './types.ts'
|
|
9
|
-
|
|
10
|
-
export const ICONS = {
|
|
11
|
-
graph: 'git-fork',
|
|
12
|
-
article: 'book-open',
|
|
13
|
-
category: 'tag',
|
|
14
|
-
infobox: 'info',
|
|
15
|
-
outline: 'list',
|
|
16
|
-
gallery: 'images',
|
|
17
|
-
section: 'pilcrow',
|
|
18
|
-
categories: 'tags',
|
|
19
|
-
} as const
|
|
20
|
-
|
|
21
|
-
/** Decide a page type for a section based on its shape. */
|
|
22
|
-
export function pickSectionType(section: ExtractedSection): { type: string; icon: string } {
|
|
23
|
-
if (section.children.length > 0) return { type: 'outline', icon: ICONS.outline }
|
|
24
|
-
if (section.isList && section.listLength >= 5) return { type: 'outline', icon: ICONS.outline }
|
|
25
|
-
return { type: 'doc', icon: ICONS.section }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Render the lead paragraph as the article-doc body. */
|
|
29
|
-
export function renderArticleLead(article: ExtractedArticle): string {
|
|
30
|
-
return article.lead ?? ''
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Render the article as a single doc, sections + infobox inlined. */
|
|
34
|
-
export function renderArticleSingleDoc(article: ExtractedArticle): string {
|
|
35
|
-
const parts: string[] = []
|
|
36
|
-
if (article.lead) parts.push(article.lead)
|
|
37
|
-
if (article.infobox && article.infobox.length > 0) {
|
|
38
|
-
parts.push('## Infobox', renderInfoboxBody(article.infobox))
|
|
39
|
-
}
|
|
40
|
-
for (const section of article.sections) {
|
|
41
|
-
parts.push(...renderSectionInline(section, 2))
|
|
42
|
-
}
|
|
43
|
-
return parts.join('\n\n')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function renderSectionInline(section: ExtractedSection, level: number): string[] {
|
|
47
|
-
const out: string[] = []
|
|
48
|
-
const prefix = '#'.repeat(Math.min(6, level))
|
|
49
|
-
if (section.title) out.push(`${prefix} ${section.title}`)
|
|
50
|
-
if (section.body.trim()) out.push(section.body)
|
|
51
|
-
for (const child of section.children) {
|
|
52
|
-
out.push(...renderSectionInline(child, level + 1))
|
|
53
|
-
}
|
|
54
|
-
return out
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function renderInfoboxBody(rows: Array<{ key: string; value: string }>): string {
|
|
58
|
-
return rows.map((r) => `- **${r.key}:** ${r.value}`).join('\n')
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function renderCategoryBody(members: string[], subcategories: string[]): string {
|
|
62
|
-
const parts: string[] = []
|
|
63
|
-
if (members.length > 0) {
|
|
64
|
-
parts.push('## Pages')
|
|
65
|
-
parts.push(members.map((m) => `- [[${m}]]`).join('\n'))
|
|
66
|
-
}
|
|
67
|
-
if (subcategories.length > 0) {
|
|
68
|
-
parts.push('## Sub-categories')
|
|
69
|
-
parts.push(subcategories.map((s) => `- ${s}`).join('\n'))
|
|
70
|
-
}
|
|
71
|
-
return parts.join('\n\n')
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Replace `[[Title]]` / `[[Title|Alias]]` in markdown with
|
|
76
|
-
* `[[docId|label]]` using the title→docId map. Unresolved titles fall
|
|
77
|
-
* back to plain text (their alias or original title).
|
|
78
|
-
*/
|
|
79
|
-
export function rewriteLinks(
|
|
80
|
-
markdown: string,
|
|
81
|
-
titleToDocId: Map<string, string>,
|
|
82
|
-
): string {
|
|
83
|
-
const re = /\[\[([^\]|]+?)(?:\|([^\]]+?))?\]\]/g
|
|
84
|
-
return markdown.replace(re, (_match, target: string, alias?: string) => {
|
|
85
|
-
const title = target.trim()
|
|
86
|
-
const docId = titleToDocId.get(title)
|
|
87
|
-
const display = (alias && alias.trim().length > 0 ? alias : title).trim()
|
|
88
|
-
if (!docId) return display
|
|
89
|
-
return `[[${docId}|${display}]]`
|
|
90
|
-
})
|
|
91
|
-
}
|
package/src/snapshot.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Snapshot a wtf_wikipedia Document into a plain-data shape that's easy to
|
|
3
|
-
* work with downstream. No BFS / no plan-building here — just a pure read
|
|
4
|
-
* of one parsed page.
|
|
5
|
-
*/
|
|
6
|
-
import type { ExtractedArticle, ExtractedSection } from './types.ts'
|
|
7
|
-
import { canonicalTitle, isCategoryTitle, stripCategoryPrefix } from './wikipedia.ts'
|
|
8
|
-
|
|
9
|
-
export { canonicalTitle, isCategoryTitle, stripCategoryPrefix }
|
|
10
|
-
|
|
11
|
-
export function snapshotArticle(doc: any, title: string): ExtractedArticle {
|
|
12
|
-
return {
|
|
13
|
-
title,
|
|
14
|
-
linkTitles: collectLinkTitles(doc),
|
|
15
|
-
categories: collectCategories(doc),
|
|
16
|
-
sections: snapshotSections(doc.sections?.() ?? []),
|
|
17
|
-
infobox: snapshotInfobox(doc.infobox?.()),
|
|
18
|
-
lead: leadParagraph(doc),
|
|
19
|
-
url: typeof doc.url === 'function' ? doc.url() : null,
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function prettyCategoryLabel(catTitle: string): string {
|
|
24
|
-
return stripCategoryPrefix(catTitle)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
28
|
-
// Link / category extraction
|
|
29
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
30
|
-
|
|
31
|
-
function collectLinkTitles(doc: any): string[] {
|
|
32
|
-
const links = doc.links?.() ?? []
|
|
33
|
-
const out = new Set<string>()
|
|
34
|
-
for (const l of links) {
|
|
35
|
-
if (!l) continue
|
|
36
|
-
const page = typeof l.page === 'function' ? l.page() : null
|
|
37
|
-
if (typeof page !== 'string' || page.length === 0) continue
|
|
38
|
-
if (isCategoryTitle(page)) continue
|
|
39
|
-
out.add(canonicalTitle(page))
|
|
40
|
-
}
|
|
41
|
-
return [...out]
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function collectCategories(doc: any): string[] {
|
|
45
|
-
const out: string[] = []
|
|
46
|
-
for (const c of (doc.categories?.() as string[] | undefined) ?? []) {
|
|
47
|
-
const norm = canonicalTitle(c)
|
|
48
|
-
if (norm) out.push(norm)
|
|
49
|
-
}
|
|
50
|
-
return out
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
54
|
-
// Sections — flatten wtf's parent-child references into a real tree
|
|
55
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
56
|
-
|
|
57
|
-
function snapshotSections(rawSections: any[]): ExtractedSection[] {
|
|
58
|
-
const all = rawSections.map((s) => ({
|
|
59
|
-
raw: s,
|
|
60
|
-
title: s.title?.() || '',
|
|
61
|
-
parentRef: typeof s.parent === 'function' ? s.parent() : null,
|
|
62
|
-
children: [] as ExtractedSection[],
|
|
63
|
-
}))
|
|
64
|
-
|
|
65
|
-
const byRaw = new Map<any, (typeof all)[number]>()
|
|
66
|
-
for (const s of all) byRaw.set(s.raw, s)
|
|
67
|
-
|
|
68
|
-
const roots: (typeof all)[number][] = []
|
|
69
|
-
for (const s of all) {
|
|
70
|
-
if (s.parentRef && byRaw.has(s.parentRef)) {
|
|
71
|
-
byRaw.get(s.parentRef)!.children.push(materialize(s))
|
|
72
|
-
} else {
|
|
73
|
-
roots.push(s)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return roots.map(materialize)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function materialize(node: {
|
|
80
|
-
raw: any
|
|
81
|
-
title: string
|
|
82
|
-
children: ExtractedSection[]
|
|
83
|
-
}): ExtractedSection {
|
|
84
|
-
const lists = node.raw.lists?.() ?? []
|
|
85
|
-
const paragraphs = node.raw.paragraphs?.() ?? []
|
|
86
|
-
|
|
87
|
-
let listLength = 0
|
|
88
|
-
for (const l of lists) {
|
|
89
|
-
const lines = l.lines?.() ?? []
|
|
90
|
-
listLength += lines.length
|
|
91
|
-
}
|
|
92
|
-
const isList =
|
|
93
|
-
lists.length > 0 && (paragraphs.length === 0 || listLength >= paragraphs.length * 2)
|
|
94
|
-
|
|
95
|
-
const bodyParts: string[] = []
|
|
96
|
-
for (const p of paragraphs) {
|
|
97
|
-
const md = paragraphMarkdown(p)
|
|
98
|
-
if (md) bodyParts.push(md)
|
|
99
|
-
}
|
|
100
|
-
for (const l of lists) {
|
|
101
|
-
const lines = (l.lines?.() ?? []) as any[]
|
|
102
|
-
for (const line of lines) {
|
|
103
|
-
const text = lineText(line)
|
|
104
|
-
if (text) bodyParts.push(`- ${text}`)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
title: node.title,
|
|
110
|
-
body: bodyParts.join('\n\n'),
|
|
111
|
-
isList,
|
|
112
|
-
listLength,
|
|
113
|
-
children: node.children,
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
118
|
-
// Infobox
|
|
119
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
120
|
-
|
|
121
|
-
function snapshotInfobox(box: any | null | undefined): Array<{ key: string; value: string }> | undefined {
|
|
122
|
-
if (!box) return undefined
|
|
123
|
-
const data = typeof box.json === 'function' ? box.json() : null
|
|
124
|
-
if (!data || typeof data !== 'object') return undefined
|
|
125
|
-
const rows: Array<{ key: string; value: string }> = []
|
|
126
|
-
for (const [key, val] of Object.entries(data)) {
|
|
127
|
-
const value = stringifyInfoboxValue(val)
|
|
128
|
-
if (!value) continue
|
|
129
|
-
rows.push({ key: humanKey(key), value })
|
|
130
|
-
}
|
|
131
|
-
return rows.length > 0 ? rows : undefined
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function stringifyInfoboxValue(val: unknown): string {
|
|
135
|
-
if (val == null) return ''
|
|
136
|
-
if (typeof val === 'string') return val
|
|
137
|
-
if (typeof val === 'number' || typeof val === 'boolean') return String(val)
|
|
138
|
-
if (Array.isArray(val)) {
|
|
139
|
-
return val.map(stringifyInfoboxValue).filter(Boolean).join(', ')
|
|
140
|
-
}
|
|
141
|
-
if (typeof val === 'object') {
|
|
142
|
-
const o = val as Record<string, unknown>
|
|
143
|
-
if (typeof o.text === 'string') return o.text
|
|
144
|
-
if (typeof o.number === 'number') return String(o.number)
|
|
145
|
-
}
|
|
146
|
-
return ''
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function humanKey(k: string): string {
|
|
150
|
-
return k.replace(/_/g, ' ').replace(/^./, (m) => m.toUpperCase())
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
154
|
-
// Markdown rendering
|
|
155
|
-
// ─────────────────────────────────────────────────────────────────────────
|
|
156
|
-
|
|
157
|
-
function leadParagraph(doc: any): string {
|
|
158
|
-
const paras = doc.paragraphs?.() ?? []
|
|
159
|
-
const first = paras[0]
|
|
160
|
-
if (!first) return ''
|
|
161
|
-
return paragraphMarkdown(first)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Render a paragraph as markdown, replacing internal links with `[[Title]]`.
|
|
166
|
-
* The streaming orchestrator's link rewriter later swaps `[[Title]]` →
|
|
167
|
-
* `[[docId|label]]` once IDs are known.
|
|
168
|
-
*/
|
|
169
|
-
function paragraphMarkdown(paragraph: any): string {
|
|
170
|
-
const sentences = paragraph.sentences?.() ?? []
|
|
171
|
-
const out: string[] = []
|
|
172
|
-
for (const s of sentences) {
|
|
173
|
-
out.push(sentenceWithWikilinks(s))
|
|
174
|
-
}
|
|
175
|
-
return out.join(' ').trim()
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function sentenceWithWikilinks(sentence: any): string {
|
|
179
|
-
const text: string = (sentence.text?.() ?? '').toString()
|
|
180
|
-
const links = sentence.links?.() ?? []
|
|
181
|
-
if (links.length === 0) return text
|
|
182
|
-
|
|
183
|
-
let result = text
|
|
184
|
-
const replacements = links
|
|
185
|
-
.map((l: any) => {
|
|
186
|
-
const page = typeof l.page === 'function' ? l.page() : null
|
|
187
|
-
const display = typeof l.text === 'function' ? l.text() : null
|
|
188
|
-
if (typeof page !== 'string' || page.length === 0) return null
|
|
189
|
-
if (isCategoryTitle(page)) return null
|
|
190
|
-
const shown = (display && display.length > 0 ? display : page) as string
|
|
191
|
-
return { page: canonicalTitle(page), shown }
|
|
192
|
-
})
|
|
193
|
-
.filter((x: any): x is { page: string; shown: string } => x !== null)
|
|
194
|
-
.sort((a: any, b: any) => b.shown.length - a.shown.length)
|
|
195
|
-
|
|
196
|
-
for (const { page, shown } of replacements) {
|
|
197
|
-
if (!result.includes(shown)) continue
|
|
198
|
-
const replacement = shown === page ? `[[${page}]]` : `[[${page}|${shown}]]`
|
|
199
|
-
result = result.replace(shown, replacement)
|
|
200
|
-
}
|
|
201
|
-
return result
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function lineText(line: any): string {
|
|
205
|
-
if (!line) return ''
|
|
206
|
-
if (typeof line === 'string') return line
|
|
207
|
-
if (typeof line.text === 'string') return line.text
|
|
208
|
-
if (typeof line.text === 'function') return line.text()
|
|
209
|
-
return ''
|
|
210
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal types for the Wikipedia extractor command.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export type ExtractMode = 'single' | 'split'
|
|
6
|
-
|
|
7
|
-
export interface WikiOptions {
|
|
8
|
-
title: string
|
|
9
|
-
mode: ExtractMode
|
|
10
|
-
depth: number
|
|
11
|
-
includeCategories: boolean
|
|
12
|
-
categoryDepth: number
|
|
13
|
-
lang: string
|
|
14
|
-
domain?: string
|
|
15
|
-
parentDocId?: string
|
|
16
|
-
userAgent: string
|
|
17
|
-
rate: number
|
|
18
|
-
/** Print plan-tree to stderr without writing (no server connection). */
|
|
19
|
-
dryRun: boolean
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** A simplified article snapshot extracted from a wtf_wikipedia Document. */
|
|
23
|
-
export interface ExtractedArticle {
|
|
24
|
-
title: string
|
|
25
|
-
/** Wikipedia internal links (page titles) referenced by this article. */
|
|
26
|
-
linkTitles: string[]
|
|
27
|
-
/** Categories this article belongs to. */
|
|
28
|
-
categories: string[]
|
|
29
|
-
/** Top-level sections; each section has nested subsections. */
|
|
30
|
-
sections: ExtractedSection[]
|
|
31
|
-
/** Infobox key-value rows (first infobox only). */
|
|
32
|
-
infobox?: Array<{ key: string; value: string }>
|
|
33
|
-
/** Plaintext lead paragraph(s) for the single-doc body. */
|
|
34
|
-
lead: string
|
|
35
|
-
/** Source URL on Wikipedia. */
|
|
36
|
-
url: string | null
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface ExtractedSection {
|
|
40
|
-
title: string
|
|
41
|
-
body: string
|
|
42
|
-
isList: boolean
|
|
43
|
-
listLength: number
|
|
44
|
-
children: ExtractedSection[]
|
|
45
|
-
}
|
package/src/wikipedia.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rate-limited wrapper around wtf_wikipedia + wtf-plugin-api.
|
|
3
|
-
*
|
|
4
|
-
* Responsibilities:
|
|
5
|
-
* - Throttle requests to respect Wikimedia API etiquette
|
|
6
|
-
* - Cache parsed Documents by canonical title
|
|
7
|
-
* - Resolve redirects so callers always see the redirect target
|
|
8
|
-
* - Expose getCategoryPages via wtf-plugin-api
|
|
9
|
-
*/
|
|
10
|
-
// @ts-ignore — wtf_wikipedia ships its own types but they are imprecise; we
|
|
11
|
-
// cast Link/Section APIs as needed below.
|
|
12
|
-
import wtf from 'wtf_wikipedia'
|
|
13
|
-
// @ts-ignore — wtf-plugin-api is JS-only with no types; treat as opaque.
|
|
14
|
-
import wtfApiPlugin from 'wtf-plugin-api'
|
|
15
|
-
|
|
16
|
-
// Augment wtf at module load so getCategoryPages becomes available.
|
|
17
|
-
let pluginExtended = false
|
|
18
|
-
function ensurePlugin(): void {
|
|
19
|
-
if (pluginExtended) return
|
|
20
|
-
// @ts-ignore — extend is dynamically attached to the wtf default export.
|
|
21
|
-
wtf.extend(wtfApiPlugin)
|
|
22
|
-
pluginExtended = true
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface WikipediaClientConfig {
|
|
26
|
-
lang: string
|
|
27
|
-
domain?: string
|
|
28
|
-
userAgent: string
|
|
29
|
-
/** Max requests per second. */
|
|
30
|
-
rate: number
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface FetchOpts {
|
|
34
|
-
lang?: string
|
|
35
|
-
domain?: string
|
|
36
|
-
'Api-User-Agent'?: string
|
|
37
|
-
follow_redirects?: boolean
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** A token-bucket-ish throttle: at most `rate` calls per second, FIFO. */
|
|
41
|
-
class RateLimiter {
|
|
42
|
-
private lastTickMs = 0
|
|
43
|
-
constructor(private intervalMs: number) {}
|
|
44
|
-
|
|
45
|
-
async wait(): Promise<void> {
|
|
46
|
-
const now = Date.now()
|
|
47
|
-
const earliest = this.lastTickMs + this.intervalMs
|
|
48
|
-
if (now < earliest) {
|
|
49
|
-
await new Promise((r) => setTimeout(r, earliest - now))
|
|
50
|
-
}
|
|
51
|
-
this.lastTickMs = Math.max(now, earliest)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export class WikipediaClient {
|
|
56
|
-
private cache = new Map<string, any>()
|
|
57
|
-
private redirects = new Map<string, string>()
|
|
58
|
-
private limiter: RateLimiter
|
|
59
|
-
private fetchOpts: FetchOpts
|
|
60
|
-
|
|
61
|
-
constructor(private config: WikipediaClientConfig) {
|
|
62
|
-
ensurePlugin()
|
|
63
|
-
this.limiter = new RateLimiter(Math.max(50, Math.floor(1000 / Math.max(0.1, config.rate))))
|
|
64
|
-
this.fetchOpts = {
|
|
65
|
-
lang: config.lang,
|
|
66
|
-
'Api-User-Agent': config.userAgent,
|
|
67
|
-
follow_redirects: true,
|
|
68
|
-
}
|
|
69
|
-
if (config.domain) this.fetchOpts.domain = config.domain
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Fetch and parse a Wikipedia article.
|
|
74
|
-
* - Returns the cached Document if we've seen this title before.
|
|
75
|
-
* - Follows redirects and caches under both source and target titles.
|
|
76
|
-
* - Returns null when the page does not exist.
|
|
77
|
-
*/
|
|
78
|
-
async fetchArticle(rawTitle: string): Promise<any | null> {
|
|
79
|
-
const title = canonicalTitle(rawTitle)
|
|
80
|
-
if (this.cache.has(title)) return this.cache.get(title)
|
|
81
|
-
if (this.redirects.has(title)) {
|
|
82
|
-
const target = this.redirects.get(title)!
|
|
83
|
-
return this.cache.get(target) ?? null
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
await this.limiter.wait()
|
|
87
|
-
let doc: any
|
|
88
|
-
try {
|
|
89
|
-
doc = await (wtf as any).fetch(title, this.fetchOpts)
|
|
90
|
-
} catch (err: any) {
|
|
91
|
-
throw new Error(`Wikipedia fetch failed for "${title}": ${err?.message ?? err}`)
|
|
92
|
-
}
|
|
93
|
-
if (!doc) return null
|
|
94
|
-
|
|
95
|
-
// wtf usually follows redirects automatically when follow_redirects=true,
|
|
96
|
-
// but defensively handle the case where it surfaces a redirect doc.
|
|
97
|
-
if (typeof doc.isRedirect === 'function' && doc.isRedirect()) {
|
|
98
|
-
const target = doc.redirectTo?.()?.page
|
|
99
|
-
if (typeof target === 'string') {
|
|
100
|
-
this.redirects.set(title, canonicalTitle(target))
|
|
101
|
-
const inner = await this.fetchArticle(target)
|
|
102
|
-
return inner
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const resolvedTitle = canonicalTitle(doc.title?.() ?? title)
|
|
107
|
-
this.cache.set(resolvedTitle, doc)
|
|
108
|
-
if (resolvedTitle !== title) this.redirects.set(title, resolvedTitle)
|
|
109
|
-
return doc
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Fetch the member pages of a category (and optionally sub-categories).
|
|
114
|
-
* @param category Category title (with or without "Category:" prefix).
|
|
115
|
-
* @param recursive Whether to traverse sub-categories.
|
|
116
|
-
* @param maxDepth Recursion depth when recursive=true.
|
|
117
|
-
*/
|
|
118
|
-
async fetchCategoryPages(
|
|
119
|
-
category: string,
|
|
120
|
-
recursive: boolean,
|
|
121
|
-
maxDepth: number,
|
|
122
|
-
): Promise<Array<{ title: string; type: 'page' | 'subcat' }>> {
|
|
123
|
-
await this.limiter.wait()
|
|
124
|
-
const opts: Record<string, unknown> = {
|
|
125
|
-
lang: this.config.lang,
|
|
126
|
-
'Api-User-Agent': this.config.userAgent,
|
|
127
|
-
recursive,
|
|
128
|
-
maxDepth,
|
|
129
|
-
}
|
|
130
|
-
if (this.config.domain) opts.domain = this.config.domain
|
|
131
|
-
// @ts-ignore — getCategoryPages is attached at runtime via wtf.extend.
|
|
132
|
-
const list: any[] = await wtf.getCategoryPages(category, opts)
|
|
133
|
-
return (list ?? []).map((m) => ({
|
|
134
|
-
title: canonicalTitle(m.title),
|
|
135
|
-
type: m.type === 'subcat' ? 'subcat' : 'page',
|
|
136
|
-
}))
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** Normalize a Wikipedia title — trim, collapse spaces, strip leading/trailing colons. */
|
|
141
|
-
export function canonicalTitle(s: string): string {
|
|
142
|
-
return (s ?? '').toString().replace(/_/g, ' ').replace(/\s+/g, ' ').trim()
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Detect a category-namespaced title. */
|
|
146
|
-
const CATEGORY_PREFIX = /^(Category|Catégorie|Kategorie|Categoría|Categoria|Categorie|Kategoria):/i
|
|
147
|
-
export function isCategoryTitle(title: string): boolean {
|
|
148
|
-
return CATEGORY_PREFIX.test(title)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Strip the "Category:" prefix for display. */
|
|
152
|
-
export function stripCategoryPrefix(title: string): string {
|
|
153
|
-
return title.replace(CATEGORY_PREFIX, '').trim()
|
|
154
|
-
}
|