Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·379 lines (337 loc) · 15.1 KB
/
Copy pathindex.js
File metadata and controls
executable file
·379 lines (337 loc) · 15.1 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
#!/usr/bin/env node
'use strict';
// ─────────────────────────── 布局常量 ────────────────────────────────
const JINA_PREFIX = 'https://r.jina.ai/';
const COL = Math.min(process.stdout.columns || 80, 96);
const PAD = 3;
const TW = COL - PAD; // 正文可用宽度
const SP = ' '.repeat(PAD); // 左边距
// ──────────────────────────── ANSI 颜色 ──────────────────────────────
const B = '\x1b[1m'; // bold
const D = '\x1b[2m'; // dim
const IT = '\x1b[3m'; // italic
const UN = '\x1b[4m'; // underline
const BL = '\x1b[94m'; // bright blue(accent)
const R = '\x1b[0m'; // reset
const REV = '\x1b[7m'; // reverse(inline code 背景)
// ─────────────────────────── 工具函数 ────────────────────────────────
// 全角字符(中日韩文字、全角标点、emoji 等)在终端里占两列
const WIDE = /[\u1100-\u115f\u2e80-\u303e\u3041-\u33ff\u3400-\u4dbf\u4e00-\u9fff\ua000-\ua4cf\uac00-\ud7a3\uf900-\ufaff\ufe30-\ufe4f\uff00-\uff60\uffe0-\uffe6]|[\u{1f300}-\u{1f64f}\u{1f900}-\u{1f9ff}\u{20000}-\u{3fffd}]/u;
const ANSI = /\x1b\[[0-9;]*m/g;
function cw(ch) { return WIDE.test(ch) ? 2 : 1; }
// 计算去除 ANSI 后在终端里占的列数
function vlen(s) { let n = 0; for (const ch of s.replace(ANSI, '')) n += cw(ch); return n; }
// 切成可断行的片段:ANSI 码单独一段;中日韩文字每个字是一段(字和字之间都能断行),其余按空格分词
function tokens(text) {
const out = [];
const re = /(\x1b\[[0-9;]*m)|(\s+)|([\u2e80-\u9fff\uac00-\ud7a3\uf900-\ufaff\uff00-\uffef\u{20000}-\u{3fffd}])|([^\s\x1b\u2e80-\u9fff\uac00-\ud7a3\uf900-\ufaff\uff00-\uffef\u{20000}-\u{3fffd}]+)/gu;
let m;
while ((m = re.exec(text)) !== null) {
if (m[1]) out.push({ t: m[1], w: 0, ansi: true });
else if (m[2]) out.push({ t: ' ', w: 1, space: true });
else out.push({ t: m[0], w: vlen(m[0]), cjk: !!m[3] });
}
return out;
}
// 行首不该出现的标点(避头尾)
const NO_START = /^[,。、;:?!)」』》〉】,.;:?!)\]}]/;
// 自动换行,保留左边距;中文按字断行,英文按词断行,超长的词(如 URL)硬切
function wrap(text, width, first = SP, rest = SP) {
const lines = [];
let cur = '', curW = 0, indent = first, pendingAnsi = '';
const avail = () => Math.max(10, width - vlen(indent));
const flush = () => { lines.push(indent + cur.replace(/\s+$/, '') + (pendingAnsi ? R : '')); cur = pendingAnsi; curW = 0; indent = rest; };
for (const tk of tokens(text)) {
if (tk.ansi) { cur += tk.t; pendingAnsi = tk.t === R ? '' : pendingAnsi + tk.t; continue; }
if (tk.space) { if (curW > 0) { cur += ' '; curW += 1; } continue; }
if (curW + tk.w > avail() && curW > 0 && !NO_START.test(tk.t)) flush();
if (tk.w > avail()) { // 超长片段硬切
for (const ch of tk.t) { if (curW + cw(ch) > avail()) flush(); cur += ch; curW += cw(ch); }
continue;
}
cur += tk.t; curW += tk.w;
}
if (curW > 0 || cur.trim()) lines.push(indent + cur.replace(/\s+$/, ''));
return lines.join('\n');
}
// 缩短 URL 用于展示
function shortenUrl(url) {
try {
const u = new URL(url);
const path = u.pathname.length > 28 ? u.pathname.slice(0, 25) + '…' : u.pathname;
return u.hostname + path;
} catch { return url.length > 40 ? url.slice(0, 37) + '…' : url; }
}
// ────────────────────────── 内联元素渲染 ──────────────────────────────
function inline(text) {
// 行内代码和链接里的 URL 先换成占位符,免得里面的 _ 和 * 被当成斜体/粗体
const keep = [];
const hold = (s) => `\u0000${keep.push(s) - 1}\u0000`;
const out = text
// 行内图片忽略
.replace(/!\[[^\]]*\]\([^)]*\)/g, '')
.replace(/`([^`\n]+)`/g, (_, c) => hold(`${REV} ${c} ${R}`))
// 链接:显示文字,URL 暗色附后
.replace(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g, (_, label, href) => {
if (label.startsWith('http') || label === href) return hold(`${D}${UN}${shortenUrl(href)}${R}`);
return `${UN}${label}${R}` + hold(`${D} (${shortenUrl(href)})${R}`);
})
.replace(/https?:\/\/[^\s)]+/g, (u) => hold(u))
.replace(/\*\*\*(.+?)\*\*\*/gs, `${B}${IT}$1${R}`)
.replace(/\*\*(.+?)\*\*/gs, `${B}$1${R}`)
.replace(/(^|[^\w*])\*(?!\s)(.+?)(?<!\s)\*(?![\w*])/gs, `$1${IT}$2${R}`)
.replace(/(^|[^\w])__(.+?)__(?!\w)/gs, `$1${B}$2${R}`)
.replace(/(^|[^\w])_(?!\s)(.+?)(?<!\s)_(?!\w)/gs, `$1${IT}$2${R}`)
.replace(/~~(.+?)~~/g, `${D}$1${R}`);
return out.replace(/\u0000(\d+)\u0000/g, (_, i) => keep[+i]);
}
// ──────────────────────── 块级元素渲染函数 ───────────────────────────
function renderH1(text) {
const t = text.replace(/[*#`]/g, '');
const upper = t.toUpperCase();
const line = '═'.repeat(Math.min(vlen(upper), TW - 2));
return `\n\n${SP}${BL}${B}${upper}${R}\n${SP}${BL}${D}${line}${R}\n\n`;
}
function renderH2(text) {
const t = inline(text.replace(/[*#`]/g, '').trim()).toUpperCase();
const fill = Math.max(0, TW - vlen(t) - 6);
return `\n${SP}${BL}${D}╌╌ ${R}${BL}${B}${t}${R}${BL}${D} ${'╌'.repeat(fill)}${R}\n\n`;
}
function renderH3(text) {
return `\n${SP}${B}${inline(text)}${R}\n\n`;
}
function renderH4(text) {
return `${SP}${D}${UN}${inline(text)}${R}\n\n`;
}
function renderParagraph(text) {
const t = inline(text.replace(/\n/g, ' ').trim());
return wrap(t, COL) + '\n\n';
}
function renderBulletItem(text, depth) {
const bullets = ['▸', '◦', '–'];
const bul = BL + bullets[Math.min(depth, 2)] + R;
const ind = SP + ' '.repeat(depth);
const restInd = ind + ' ';
const t = inline(text);
return wrap(t, COL - vlen(ind) - 3, `${ind}${bul} `, restInd) + '\n';
}
function renderOrderedItem(text, num, depth) {
const prefix = `${BL}${B}${num}.${R}`;
const ind = SP + ' '.repeat(depth);
const restInd = ind + ' ';
const t = inline(text);
return wrap(t, COL - vlen(ind) - 4, `${ind}${prefix} `, restInd) + '\n';
}
function renderList(tok, depth = 0) {
let out = '';
for (let i = 0; i < tok.items.length; i++) {
const item = tok.items[i];
// 提取主文本(过滤掉子列表 token)
const mainText = (item.tokens || [])
.filter(t => t.type === 'text' || t.type === 'paragraph')
.map(t => t.text || t.raw || '')
.join(' ');
if (tok.ordered) {
out += renderOrderedItem(mainText || item.text, i + 1, depth);
} else {
out += renderBulletItem(mainText || item.text, depth);
}
// 嵌套列表
for (const child of (item.tokens || [])) {
if (child.type === 'list') out += renderList(child, depth + 1);
}
}
return out + '\n';
}
function renderCode(code, lang) {
const label = lang ? ` ${lang} ` : '';
const barW = TW;
const top = '┌' + (label ? `─${label}` : '') + '─'.repeat(Math.max(0, barW - vlen(label) - 1)) + '┐';
const bot = '└' + '─'.repeat(barW) + '┘';
const body = code.replace(/\t/g, ' ').split('\n').map(l => {
const pad = ' '.repeat(Math.max(0, barW - vlen(l) - 1));
return `${SP}${D}│${R} ${l}${pad}${D}│${R}`;
}).join('\n');
return `\n${SP}${D}${top}${R}\n${body}\n${SP}${D}${bot}${R}\n\n`;
}
function renderHr() {
const dots = Array(Math.floor(TW / 2)).fill('·').join(' ');
return `\n${SP}${BL}${D}${dots}${R}\n\n`;
}
function renderBlockquote(tokens) {
const text = renderTokens(tokens).trim();
return text.split('\n').map(l => `${SP}${BL}│${R}${D} ${l.trimStart()}${R}`).join('\n') + '\n\n';
}
function renderTable(header, rows) {
// 简单渲染 table 为分隔行格式
const renderRow = (cells) =>
SP + D + '│' + R + ' ' + cells.map(c => inline(c.text || '')).join(` ${D}│${R} `) + '\n';
const sep = SP + D + '├' + '─'.repeat(TW - 1) + '┤' + R + '\n';
let out = '\n';
out += renderRow(header);
out += sep;
for (const row of rows) out += renderRow(row);
return out + '\n';
}
// ───────────────────────── Token 树渲染 ──────────────────────────────
function renderTokens(tokens) {
let out = '';
for (const tok of tokens) {
switch (tok.type) {
case 'heading':
if (tok.depth === 1) out += renderH1(tok.text);
else if (tok.depth === 2) out += renderH2(tok.text);
else if (tok.depth === 3) out += renderH3(tok.text);
else out += renderH4(tok.text);
break;
case 'paragraph':
out += renderParagraph(tok.text);
break;
case 'list':
out += renderList(tok, 0);
break;
case 'code':
out += renderCode(tok.text, tok.lang);
break;
case 'hr':
out += renderHr();
break;
case 'blockquote':
out += renderBlockquote(tok.tokens || []);
break;
case 'table':
out += renderTable(tok.header, tok.rows);
break;
case 'space':
break;
default:
if (tok.raw && tok.raw.trim()) out += renderParagraph(tok.raw);
}
}
return out;
}
function renderMarkdown(md) {
const { marked } = require('marked');
return renderTokens(marked.lexer(md));
}
// ────────────────── 图片渲染(jimp 块字符,完全控制输出)────────────
async function renderImage(url) {
try {
const res = await fetch(url, { signal: AbortSignal.timeout(10000) });
if (!res.ok) return null;
const buf = Buffer.from(await res.arrayBuffer());
const { Jimp } = require('jimp');
const img = await Jimp.fromBuffer(buf);
const targetW = Math.floor(TW * 0.75);
// 每个字符格高约为宽的 2 倍,用上下半块字符各代表 1 像素行
const targetH = Math.max(1, Math.round(targetW * img.height / img.width / 2)) * 2;
img.resize({ w: targetW, h: targetH });
const rows = [];
for (let y = 0; y < targetH; y += 2) {
let line = '';
for (let x = 0; x < targetW; x++) {
const top = img.getPixelColor(x, y);
const bot = img.getPixelColor(x, y + 1);
const tr = (top >>> 24) & 0xff, tg = (top >>> 16) & 0xff, tb = (top >>> 8) & 0xff;
const br = (bot >>> 24) & 0xff, bg = (bot >>> 16) & 0xff, bb = (bot >>> 8) & 0xff;
// 上半块用前景色,下半块用背景色
line += `\x1b[38;2;${tr};${tg};${tb}m\x1b[48;2;${br};${bg};${bb}m▀`;
}
line += '\x1b[0m'; // 每行末尾重置颜色
rows.push(line);
}
return rows; // 返回行数组,由调用方逐行写入
} catch { return null; }
}
// ───────────────── 将正文切分为文本段 + 图片段 ──────────────────────
async function render(markdown) {
const parts = [];
let last = 0;
// 匹配 [](link) 或 
const re = /\[!\[([^\]]*)\]\(([^)\s"]+)[^)]*\)\]\([^)]*\)|!\[([^\]]*)\]\(([^)\s"]+)[^)]*\)/g;
let m;
while ((m = re.exec(markdown)) !== null) {
if (m.index > last) parts.push({ type: 'md', content: markdown.slice(last, m.index) });
parts.push({ type: 'img', alt: m[1] ?? m[3], url: m[2] ?? m[4] });
last = m.index + m[0].length;
}
if (last < markdown.length) parts.push({ type: 'md', content: markdown.slice(last) });
for (const part of parts) {
if (part.type === 'md') {
if (!part.content.trim()) continue;
process.stdout.write(renderMarkdown(part.content));
} else {
if (part.url.startsWith('data:') || part.url.length < 8) continue;
const imgOut = await renderImage(part.url);
if (imgOut) {
const imgW = Math.floor(TW * 0.75);
const pad = ' '.repeat(Math.max(0, COL - imgW));
process.stdout.write('\r\n');
for (const line of imgOut) {
// 补空格至终端宽度,\r\n 保证光标归列 0
process.stdout.write(line + pad + '\r\n');
}
process.stdout.write('\r\n');
}
}
}
}
// ──────────────────────── Jina 元数据头部 ────────────────────────────
function extractJinaMeta(content) {
const metaRe = /^(Title|URL Source|Published Time):\s*(.+)$/gm;
const meta = {};
let m;
while ((m = metaRe.exec(content)) !== null) meta[m[1]] = m[2].trim();
const bodyMatch = content.match(/^Markdown Content:\s*\n([\s\S]*)$/m);
return { meta, body: bodyMatch ? bodyMatch[1] : content };
}
function printMeta(meta, rawUrl) {
const domain = rawUrl.replace(/^https?:\/\//, '').split('/')[0];
// 顶部状态栏
const left = `${D}${domain.toUpperCase()}${R}`;
const right = meta['Published Time'] ? `${D}${meta['Published Time']}${R}` : '';
const gap = COL - vlen(domain.toUpperCase()) - vlen(meta['Published Time'] || '');
process.stdout.write(SP + left + ' '.repeat(Math.max(1, gap - PAD)) + right + '\n');
// 大标题
if (meta['Title']) {
const title = meta['Title'].toUpperCase();
process.stdout.write(`\n${SP}${BL}${B}${title}${R}\n`);
process.stdout.write(`${SP}${BL}${D}${'═'.repeat(Math.min(vlen(title), TW))}${R}\n`);
}
// 分隔线
const dots = Array(Math.floor(TW / 2)).fill('·').join(' ');
process.stdout.write(`\n${SP}${BL}${D}${dots}${R}\n\n`);
}
// ─────────────────────────────── main ────────────────────────────────
async function main() {
const url = process.argv[2];
if (!url || url === '--help' || url === '-h') {
process.stdout.write(`${B}termread${R} — 在终端阅读任意网页\n\n`);
process.stdout.write(`用法: termread ${IT}<url>${R}\n`);
process.stdout.write(`示例: termread https://x.com/akshay_pachaar/status/2041146899319971922\n`);
process.exit(url ? 0 : 1);
}
const displayUrl = url.replace(/^https?:\/\//, '');
process.stderr.write(`${D}⏳ 正在读取 ${displayUrl}…${R}`);
try {
const content = await fetchContent(url);
process.stderr.write('\r\x1b[K');
const { meta, body } = extractJinaMeta(content);
if (Object.keys(meta).length) printMeta(meta, url);
await render(body);
} catch (err) {
process.stderr.write('\r\x1b[K');
process.stderr.write(`\x1b[31m错误: ${err.message}\x1b[0m\n`);
process.exit(1);
}
}
async function fetchContent(url) {
const res = await fetch(`${JINA_PREFIX}${url}`, {
headers: { 'Accept': 'text/markdown', 'X-Timeout': '30' },
signal: AbortSignal.timeout(30000),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.text();
}
if (require.main === module) main();
else module.exports = { vlen, wrap, inline, renderMarkdown, extractJinaMeta };