forked from mozilla/readability
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·439 lines (413 loc) · 13.6 KB
/
cli.js
File metadata and controls
executable file
·439 lines (413 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env node
import {
toHTML,
toMarkdown,
extract,
analyzePageStructure,
extractDocumentContent,
} from "./dist/index.js"; // Adjust path as needed, import PageType, remove toReadableAriaTree
import { parseArgs } from "node:util";
import path from "node:path";
import fs from "node:fs";
// TODO: add puppeteer and lightpanda
/**
*
* @param {string} urlOrPath
* @returns {Promise<string>}
*/
async function fetchLoader(urlOrPath) {
// Check if it's a local file path
if (!urlOrPath.startsWith("http://") && !urlOrPath.startsWith("https://")) {
try {
const fullPath = path.isAbsolute(urlOrPath) ? urlOrPath : path.join(process.cwd(), urlOrPath);
return fs.readFileSync(fullPath, "utf-8");
} catch (err) {
throw new Error(`Failed to read file ${urlOrPath}: ${err.message}`);
}
}
const res = await fetch(urlOrPath);
if (!res.ok) {
throw new Error(`Failed to fetch ${urlOrPath}: ${res.statusText}`);
}
return res.text();
}
const helpText = `
Usage: @mizchi/readability [options] <url-or-file>
Default behavior (extract main content):
readability <url> Extract main content as markdown
readability <url> -f html Extract main content as HTML
Progressive analysis options:
--analyze-structure Analyze page structure without content extraction
--extract-nav Extract navigation elements only
--extract-content --with-context Extract content with structural context
--full-analysis Complete analysis (structure + nav + content)
Output formats:
-f, --format <format> Output format:
- md (markdown, default)
- html
- json
- nav (navigation only)
- doc (document mode)
- ai-summary (AI-optimized summary)
- ai-structured (AI-optimized structure)
Filtering options:
--nav-type <type> Filter by navigation type
--nav-location <location> Filter by navigation location
--with-context Include structural context
Other options:
-t, --threshold <number> Character threshold (default: 250)
-o, --out <file> Output file path (default: stdout)
-h, --help Show this help message
--mcp Start MCP server
Legacy options (backward compatibility):
--nav-only Same as --extract-nav
--doc-mode Same as --format doc
`;
if (process.argv.includes("--help") || process.argv.includes("-h") || process.argv.length === 2) {
console.log(helpText);
process.exit(0);
}
async function main() {
// before parse
if (process.argv.includes("--mcp") || process.argv.includes("-v")) {
const { startMcpServer } = await import("./src/mcp-server.js");
await startMcpServer();
return;
}
const parsed = parseArgs({
args: process.argv.slice(2),
options: {
threshold: {
type: "string",
short: "t",
},
loader: {
type: "string",
},
format: {
type: "string",
short: "f",
},
out: {
type: "string",
short: "o",
},
mcp: {
type: "boolean",
default: false,
},
"nav-type": {
type: "string",
},
"nav-location": {
type: "string",
},
"nav-only": {
type: "boolean",
default: false,
},
"doc-mode": {
type: "boolean",
default: false,
},
"analyze-structure": {
type: "boolean",
default: false,
},
"extract-nav": {
type: "boolean",
default: false,
},
"extract-content": {
type: "boolean",
default: false,
},
"with-context": {
type: "boolean",
default: false,
},
"full-analysis": {
type: "boolean",
default: false,
},
},
allowPositionals: true,
});
if (parsed.values.mcp) {
const { startMcpServer } = await import("./src/mcp-server.js");
await startMcpServer();
return;
}
const url = parsed.positionals[0];
if (!url) {
console.error("Please provide a URL.");
process.exit(1);
}
// const format = parsed.values.format ?? "md";
/**
* @type {string}
*/
let format = "md";
/**
* @type {string | undefined}
*/
let output = undefined;
const threshold = parsed.values.threshold ? Number(parsed.values.threshold) : 250;
if (parsed.values.format) {
format = parsed.values.format;
} else if (parsed.values.out) {
if (path.isAbsolute(parsed.values.out)) {
output = parsed.values.out;
} else {
output = path.join(process.cwd(), parsed.values.out);
}
const ext = path.extname(url);
if (ext === ".html") {
format = "html";
} else if (ext === ".md") {
format = "md";
}
}
const html = await fetchLoader(url);
const result = extract(html, {
charThreshold: threshold,
});
/**
* @type {string}
*/
let content;
// Handle progressive analysis options
if (parsed.values["analyze-structure"]) {
// Structure analysis without content extraction
const structure = analyzePageStructure(html);
const analysis = {
url: url,
pageType: result.nodeCount > 0 ? "article" : "other", // Simple heuristic
hasMainContent: result.nodeCount > 0,
navigations: {
global: structure.navigations.some((n) => n.type === "global"),
breadcrumb: structure.navigations.some((n) => n.type === "breadcrumb"),
toc: structure.navigations.some((n) => n.type === "toc"),
sidebar: structure.navigations.some((n) => n.location === "sidebar"),
pagination: structure.navigations.some((n) => n.type === "pagination"),
},
contentAreas: {
header: structure.headers.length > 0,
mainContent: !!structure.mainContent,
sidebar: !!structure.sidebar,
footer: !!structure.footer,
},
stats: {
navigationCount: structure.navigations.length,
headerCount: structure.headers.length,
contentLength: result.nodeCount,
},
};
content = JSON.stringify(analysis, null, 2);
} else if (parsed.values["extract-nav"] || parsed.values["nav-only"]) {
// Navigation extraction (enhanced version of nav-only)
const pageStructure = analyzePageStructure(html);
let navigations = pageStructure.navigations;
// Filter by navigation type if specified
if (parsed.values["nav-type"]) {
navigations = navigations.filter((nav) => nav.type === parsed.values["nav-type"]);
}
// Filter by navigation location if specified
if (parsed.values["nav-location"]) {
navigations = navigations.filter((nav) => nav.location === parsed.values["nav-location"]);
}
// Create output object
const navOutput = {
url: url,
navigations: navigations,
summary: {
total: navigations.length,
byType: navigations.reduce((acc, nav) => {
acc[nav.type] = (acc[nav.type] || 0) + 1;
return acc;
}, {}),
mainNavigation: pageStructure.mainNavigation
? {
items: pageStructure.mainNavigation.items.map((item) => item.label),
}
: null,
breadcrumb: pageStructure.breadcrumb
? {
path: pageStructure.breadcrumb.items.map((item) => item.label).join(" > "),
}
: null,
toc: pageStructure.toc
? {
items: pageStructure.toc.items.map((item) => ({
label: item.label,
href: item.href,
})),
}
: null,
},
};
content = JSON.stringify(navOutput, null, 2);
} else if (parsed.values["extract-content"]) {
// Content extraction with optional context
const withContext = parsed.values["with-context"];
if (withContext) {
const structure = analyzePageStructure(html);
const contentWithContext = {
url: url,
title: result.metadata?.title || "",
content: toMarkdown(result.root),
context: {
breadcrumb: structure.breadcrumb?.items.map((i) => i.label).join(" > ") || null,
section: structure.mainContent ? "main" : "unknown",
surroundingNavigation: structure.navigations
.filter((n) => n.location === "inline" || n.type === "toc")
.map((n) => ({
type: n.type,
location: n.location,
itemCount: n.items.length,
})),
},
metadata: result.metadata,
};
content = JSON.stringify(contentWithContext, null, 2);
} else {
content = toMarkdown(result.root);
}
} else if (parsed.values["full-analysis"]) {
// Complete analysis
const structure = analyzePageStructure(html);
const docContent = extractDocumentContent(html);
const fullAnalysis = {
url: url,
structure: {
pageType: result.nodeCount > 0 ? "article" : "other",
navigations: structure.navigations.map((n) => ({
type: n.type,
location: n.location,
itemCount: n.items.length,
label: n.label,
})),
headers: structure.headers.map((h) => ({
type: h.type,
text: h.contains.siteTitle?.text || "",
})),
contentAreas: {
main: !!structure.mainContent,
sidebar: !!structure.sidebar,
footer: !!structure.footer,
},
},
navigation: {
breadcrumb: docContent.breadcrumb || null,
tableOfContents: docContent.toc || null,
sidebarNav: docContent.sidebarNav || null,
},
content: {
main: docContent.content,
outline: docContent.outline || null,
},
metadata: result.metadata,
};
content = JSON.stringify(fullAnalysis, null, 2);
} else if (format === "ai-summary") {
// AI-optimized summary format
const structure = analyzePageStructure(html);
const summary = {
url: url,
type: structure.navigations.some((n) => n.type === "toc" && n.location === "sidebar")
? "documentation"
: result.nodeCount > 500
? "article"
: "other",
title: result.metadata?.title || "",
summary: result.root ? toMarkdown(result.root).substring(0, 200) + "..." : "",
mainTopics: structure.sections?.map((s) => s.title).slice(0, 5) || [],
navigationSummary: {
breadcrumb: structure.breadcrumb?.items.map((i) => i.label).join(" > ") || null,
sections: structure.sections?.length || 0,
hasTableOfContents: structure.navigations.some((n) => n.type === "toc"),
hasSidebar: !!structure.sidebar,
},
contentStats: {
wordCount: result.root ? toMarkdown(result.root).split(/\s+/).length : 0,
hasCode: result.root ? toMarkdown(result.root).includes("```") : false,
},
};
content = JSON.stringify(summary, null, 2);
} else if (format === "ai-structured") {
// AI-optimized structured format
const structure = analyzePageStructure(html);
const structured = {
metadata: {
url: url,
...result.metadata,
},
structure: {
header: structure.mainHeader
? {
logo: structure.mainHeader.contains.logo ? "present" : "absent",
title: structure.mainHeader.contains.siteTitle?.text || null,
navigation: structure.mainHeader.contains.navigation ? "present" : "absent",
}
: null,
navigation: {
types: structure.navigations.map((n) => n.type),
main:
structure.mainNavigation?.items.map((i) => ({
label: i.label,
href: i.href,
})) || [],
breadcrumb: structure.breadcrumb?.items || [],
},
content: {
main: {
present: !!result.root,
markdown: result.root ? toMarkdown(result.root) : "",
},
sections:
structure.sections?.map((s) => ({
id: s.id,
title: s.title,
level: s.level,
children: s.children?.length || 0,
})) || [],
},
sidebar: structure.sidebar
? {
present: true,
navigation: structure.sidebarNavigation?.items.length || 0,
}
: null,
},
};
content = JSON.stringify(structured, null, 2);
} else if (parsed.values["doc-mode"] || format === "doc") {
// ドキュメントモード:本文とナビゲーション構造を統合
const docContent = extractDocumentContent(html);
// マークダウン形式で出力
content = "# Document Content\n\n";
if (docContent.breadcrumb) {
content += `**Breadcrumb:** ${docContent.breadcrumb}\n\n`;
}
if (docContent.toc) {
content += "## Table of Contents\n\n" + docContent.toc + "\n";
}
if (docContent.sidebarNav) {
content += "## Sidebar Navigation\n\n" + docContent.sidebarNav + "\n";
}
if (docContent.outline) {
content += "## Document Outline\n\n" + docContent.outline + "\n";
}
content += "## Main Content\n\n" + docContent.content;
} else if (format === "html") {
content = toHTML(result.root);
} else {
content = toMarkdown(result.root);
}
if (output) {
fs.writeFileSync(output, content);
} else {
console.log(content);
}
}
await main().catch(console.error);