dsh-reading-inbox 0.2.1 → 0.4.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/README.en.md +214 -208
- package/README.md +214 -208
- package/VERIFY.md +139 -126
- package/index.js +81 -80
- package/package.json +83 -83
- package/src/config.js +44 -44
- package/src/fetch.js +173 -173
- package/src/markdown.js +316 -309
- package/src/routes.js +314 -254
- package/src/search.js +109 -0
- package/src/store.js +1000 -647
- package/src/tools.js +488 -438
- package/web/app.js +1014 -785
- package/web/index.html +102 -74
- package/web/style.css +865 -739
package/src/markdown.js
CHANGED
|
@@ -1,309 +1,316 @@
|
|
|
1
|
-
import { Readability } from '@mozilla/readability'
|
|
2
|
-
import { dump as dumpYaml } from 'js-yaml'
|
|
3
|
-
import { parseHTML } from 'linkedom'
|
|
4
|
-
import { marked } from 'marked'
|
|
5
|
-
import sanitizeHtml from 'sanitize-html'
|
|
6
|
-
import TurndownService from 'turndown'
|
|
7
|
-
import { gfm } from 'turndown-plugin-gfm'
|
|
8
|
-
|
|
9
|
-
function escapeHtmlAttribute(value) {
|
|
10
|
-
return String(value)
|
|
11
|
-
.replaceAll('&', '&')
|
|
12
|
-
.replaceAll('"', '"')
|
|
13
|
-
.replaceAll('<', '<')
|
|
14
|
-
.replaceAll('>', '>')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function withBaseHref(html, baseUrl) {
|
|
18
|
-
const base = `<base href="${escapeHtmlAttribute(baseUrl)}">`
|
|
19
|
-
if (/<head[^>]*>/i.test(html)) return html.replace(/<head([^>]*)>/i, `<head$1>${base}`)
|
|
20
|
-
if (/<html[^>]*>/i.test(html)) return html.replace(/<html([^>]*)>/i, `<html$1><head>${base}</head>`)
|
|
21
|
-
return `<!doctype html><html><head>${base}</head><body>${html}</body></html>`
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function metaContent(document, selectors) {
|
|
25
|
-
for (const selector of selectors) {
|
|
26
|
-
const element = document.querySelector(selector)
|
|
27
|
-
if (element === null) continue
|
|
28
|
-
const value = element.getAttribute('content') ?? element.getAttribute('href') ?? element.textContent
|
|
29
|
-
if (typeof value === 'string' && value.trim() !== '') return value.trim()
|
|
30
|
-
}
|
|
31
|
-
return ''
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function absoluteUrl(value, baseUrl) {
|
|
35
|
-
const raw = String(value ?? '').trim()
|
|
36
|
-
if (raw === '') return ''
|
|
37
|
-
if (raw.startsWith('#')) return raw
|
|
38
|
-
try {
|
|
39
|
-
return new URL(raw, baseUrl).toString()
|
|
40
|
-
} catch {
|
|
41
|
-
return ''
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function isSafeHref(value) {
|
|
46
|
-
if (value.startsWith('#')) return true
|
|
47
|
-
try {
|
|
48
|
-
const parsed = new URL(value)
|
|
49
|
-
return parsed.protocol === 'http:' || parsed.protocol === 'https:' || parsed.protocol === 'mailto:'
|
|
50
|
-
} catch {
|
|
51
|
-
return false
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function makeTurndown(baseUrl) {
|
|
56
|
-
const turndown = new TurndownService({
|
|
57
|
-
headingStyle: 'atx',
|
|
58
|
-
codeBlockStyle: 'fenced',
|
|
59
|
-
bulletListMarker: '-',
|
|
60
|
-
emDelimiter: '*',
|
|
61
|
-
})
|
|
62
|
-
turndown.use(gfm)
|
|
63
|
-
turndown.remove(['script', 'style', 'noscript', 'iframe', 'form', 'button', 'input', 'select', 'textarea', 'svg', 'canvas'])
|
|
64
|
-
|
|
65
|
-
turndown.addRule('absoluteLinks', {
|
|
66
|
-
filter: 'a',
|
|
67
|
-
replacement(content, node) {
|
|
68
|
-
const href = absoluteUrl(node.getAttribute?.('href'), baseUrl)
|
|
69
|
-
const text = content.trim()
|
|
70
|
-
if (href === '' || !isSafeHref(href)) return text
|
|
71
|
-
if (text === '') return ''
|
|
72
|
-
return `[${text}](${href})`
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
turndown.addRule('absoluteImages', {
|
|
77
|
-
filter: 'img',
|
|
78
|
-
replacement(_content, node) {
|
|
79
|
-
const src = absoluteUrl(node.getAttribute?.('src'), baseUrl)
|
|
80
|
-
if (src === '' || !/^https?:/i.test(src)) return ''
|
|
81
|
-
const alt = String(node.getAttribute?.('alt') ?? '').replace(/\s+/g, ' ').trim()
|
|
82
|
-
const title = String(node.getAttribute?.('title') ?? '').trim()
|
|
83
|
-
return `}"`})`
|
|
84
|
-
},
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
return turndown
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function countWords(text) {
|
|
91
|
-
const cjk = (text.match(/[-ヿ㐀-䶿一-鿿가-]/g) || []).length
|
|
92
|
-
const latin = (text.replace(/[-ヿ㐀-䶿一-鿿가-]/g, ' ').match(/[A-Za-z0-9_]+/g) || []).length
|
|
93
|
-
return { wordCount: cjk + latin, charCount: text.length, cjk }
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function detectLanguage(document, text, cjk) {
|
|
97
|
-
const declared = String(document?.documentElement?.getAttribute?.('lang') ?? '').trim().toLowerCase()
|
|
98
|
-
if (declared !== '') return declared.split('-')[0]
|
|
99
|
-
return cjk > 0 && cjk >= text.length * 0.05 ? 'zh' : 'en'
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function cleanMarkdown(markdown) {
|
|
103
|
-
return markdown
|
|
104
|
-
.replace(/\]\((?:javascript|data|vbscript|file):[^)\s]*\)/gi, '](#)')
|
|
105
|
-
.replace(/[ \t]+\n/g, '\n')
|
|
106
|
-
.replace(/\n{3,}/g, '\n\n')
|
|
107
|
-
.trim()
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function clampText(value, maxLength) {
|
|
111
|
-
const text = String(value ?? '').replace(/\s+/g, ' ').trim()
|
|
112
|
-
return text.length > maxLength ? text.slice(0, maxLength) : text
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
'meta[
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
'meta[
|
|
145
|
-
'meta[name="
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
'meta[
|
|
151
|
-
'meta[name="
|
|
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
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
1
|
+
import { Readability } from '@mozilla/readability'
|
|
2
|
+
import { dump as dumpYaml } from 'js-yaml'
|
|
3
|
+
import { parseHTML } from 'linkedom'
|
|
4
|
+
import { marked } from 'marked'
|
|
5
|
+
import sanitizeHtml from 'sanitize-html'
|
|
6
|
+
import TurndownService from 'turndown'
|
|
7
|
+
import { gfm } from 'turndown-plugin-gfm'
|
|
8
|
+
|
|
9
|
+
function escapeHtmlAttribute(value) {
|
|
10
|
+
return String(value)
|
|
11
|
+
.replaceAll('&', '&')
|
|
12
|
+
.replaceAll('"', '"')
|
|
13
|
+
.replaceAll('<', '<')
|
|
14
|
+
.replaceAll('>', '>')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function withBaseHref(html, baseUrl) {
|
|
18
|
+
const base = `<base href="${escapeHtmlAttribute(baseUrl)}">`
|
|
19
|
+
if (/<head[^>]*>/i.test(html)) return html.replace(/<head([^>]*)>/i, `<head$1>${base}`)
|
|
20
|
+
if (/<html[^>]*>/i.test(html)) return html.replace(/<html([^>]*)>/i, `<html$1><head>${base}</head>`)
|
|
21
|
+
return `<!doctype html><html><head>${base}</head><body>${html}</body></html>`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function metaContent(document, selectors) {
|
|
25
|
+
for (const selector of selectors) {
|
|
26
|
+
const element = document.querySelector(selector)
|
|
27
|
+
if (element === null) continue
|
|
28
|
+
const value = element.getAttribute('content') ?? element.getAttribute('href') ?? element.textContent
|
|
29
|
+
if (typeof value === 'string' && value.trim() !== '') return value.trim()
|
|
30
|
+
}
|
|
31
|
+
return ''
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function absoluteUrl(value, baseUrl) {
|
|
35
|
+
const raw = String(value ?? '').trim()
|
|
36
|
+
if (raw === '') return ''
|
|
37
|
+
if (raw.startsWith('#')) return raw
|
|
38
|
+
try {
|
|
39
|
+
return new URL(raw, baseUrl).toString()
|
|
40
|
+
} catch {
|
|
41
|
+
return ''
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isSafeHref(value) {
|
|
46
|
+
if (value.startsWith('#')) return true
|
|
47
|
+
try {
|
|
48
|
+
const parsed = new URL(value)
|
|
49
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:' || parsed.protocol === 'mailto:'
|
|
50
|
+
} catch {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function makeTurndown(baseUrl) {
|
|
56
|
+
const turndown = new TurndownService({
|
|
57
|
+
headingStyle: 'atx',
|
|
58
|
+
codeBlockStyle: 'fenced',
|
|
59
|
+
bulletListMarker: '-',
|
|
60
|
+
emDelimiter: '*',
|
|
61
|
+
})
|
|
62
|
+
turndown.use(gfm)
|
|
63
|
+
turndown.remove(['script', 'style', 'noscript', 'iframe', 'form', 'button', 'input', 'select', 'textarea', 'svg', 'canvas'])
|
|
64
|
+
|
|
65
|
+
turndown.addRule('absoluteLinks', {
|
|
66
|
+
filter: 'a',
|
|
67
|
+
replacement(content, node) {
|
|
68
|
+
const href = absoluteUrl(node.getAttribute?.('href'), baseUrl)
|
|
69
|
+
const text = content.trim()
|
|
70
|
+
if (href === '' || !isSafeHref(href)) return text
|
|
71
|
+
if (text === '') return ''
|
|
72
|
+
return `[${text}](${href})`
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
turndown.addRule('absoluteImages', {
|
|
77
|
+
filter: 'img',
|
|
78
|
+
replacement(_content, node) {
|
|
79
|
+
const src = absoluteUrl(node.getAttribute?.('src'), baseUrl)
|
|
80
|
+
if (src === '' || !/^https?:/i.test(src)) return ''
|
|
81
|
+
const alt = String(node.getAttribute?.('alt') ?? '').replace(/\s+/g, ' ').trim()
|
|
82
|
+
const title = String(node.getAttribute?.('title') ?? '').trim()
|
|
83
|
+
return `}"`})`
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
return turndown
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function countWords(text) {
|
|
91
|
+
const cjk = (text.match(/[-ヿ㐀-䶿一-鿿가-]/g) || []).length
|
|
92
|
+
const latin = (text.replace(/[-ヿ㐀-䶿一-鿿가-]/g, ' ').match(/[A-Za-z0-9_]+/g) || []).length
|
|
93
|
+
return { wordCount: cjk + latin, charCount: text.length, cjk }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function detectLanguage(document, text, cjk) {
|
|
97
|
+
const declared = String(document?.documentElement?.getAttribute?.('lang') ?? '').trim().toLowerCase()
|
|
98
|
+
if (declared !== '') return declared.split('-')[0]
|
|
99
|
+
return cjk > 0 && cjk >= text.length * 0.05 ? 'zh' : 'en'
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function cleanMarkdown(markdown) {
|
|
103
|
+
return markdown
|
|
104
|
+
.replace(/\]\((?:javascript|data|vbscript|file):[^)\s]*\)/gi, '](#)')
|
|
105
|
+
.replace(/[ \t]+\n/g, '\n')
|
|
106
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
107
|
+
.trim()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function clampText(value, maxLength) {
|
|
111
|
+
const text = String(value ?? '').replace(/\s+/g, ' ').trim()
|
|
112
|
+
return text.length > maxLength ? text.slice(0, maxLength) : text
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function htmlToMarkdown(html, baseUrl = '') {
|
|
116
|
+
const document = parseHTML(`<!doctype html><html><body><div id="manual-root">${String(html ?? '')}</div></body></html>`).document
|
|
117
|
+
const root = document.getElementById('manual-root')
|
|
118
|
+
if (root === null) return ''
|
|
119
|
+
return cleanMarkdown(makeTurndown(baseUrl).turndown(root.innerHTML))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Turn raw HTML into a clean article: metadata, a Turndown Markdown body, and
|
|
124
|
+
* a safe rendered HTML fragment. Readability runs on a fresh document so meta
|
|
125
|
+
* extraction and the fallback path cannot see a half-mutated DOM.
|
|
126
|
+
*/
|
|
127
|
+
export function parseArticle(html, options = {}) {
|
|
128
|
+
const url = options.url ?? 'https://example.invalid/'
|
|
129
|
+
const maxMarkdownChars = options.maxMarkdownChars ?? 400_000
|
|
130
|
+
const withBase = withBaseHref(String(html ?? ''), url)
|
|
131
|
+
|
|
132
|
+
const metaDocument = parseHTML(withBase).document
|
|
133
|
+
const metaTitle = metaContent(metaDocument, [
|
|
134
|
+
'meta[property="og:title"]',
|
|
135
|
+
'meta[name="twitter:title"]',
|
|
136
|
+
'meta[name="title"]',
|
|
137
|
+
]) || String(metaDocument.title ?? '').trim()
|
|
138
|
+
const byline = metaContent(metaDocument, [
|
|
139
|
+
'meta[name="author"]',
|
|
140
|
+
'meta[property="article:author"]',
|
|
141
|
+
'meta[name="byl"]',
|
|
142
|
+
])
|
|
143
|
+
const siteName = metaContent(metaDocument, [
|
|
144
|
+
'meta[property="og:site_name"]',
|
|
145
|
+
'meta[name="application-name"]',
|
|
146
|
+
]) || (() => {
|
|
147
|
+
try { return new URL(url).hostname } catch { return '' }
|
|
148
|
+
})()
|
|
149
|
+
const published = metaContent(metaDocument, [
|
|
150
|
+
'meta[property="article:published_time"]',
|
|
151
|
+
'meta[name="date"]',
|
|
152
|
+
'meta[name="pubdate"]',
|
|
153
|
+
'meta[name="publishdate"]',
|
|
154
|
+
'meta[name="dc.date"]',
|
|
155
|
+
])
|
|
156
|
+
const keywords = metaContent(metaDocument, [
|
|
157
|
+
'meta[name="keywords"]',
|
|
158
|
+
'meta[name="news_keywords"]',
|
|
159
|
+
])
|
|
160
|
+
const canonicalRaw = absoluteUrl(
|
|
161
|
+
metaContent(metaDocument, ['link[rel="canonical"]']),
|
|
162
|
+
url,
|
|
163
|
+
)
|
|
164
|
+
// A hostile page can put megabytes in <link rel=canonical>. Keep the URL
|
|
165
|
+
// usable as a base, but do not persist an unbounded value.
|
|
166
|
+
const canonical = canonicalRaw.length <= 2_048 ? canonicalRaw : ''
|
|
167
|
+
|
|
168
|
+
// Relative links and images should survive the archive. Prefer the page's
|
|
169
|
+
// declared canonical URL over the URL we happened to fetch through, so a
|
|
170
|
+
// mirror/local server does not bake its own origin into the saved copy.
|
|
171
|
+
const articleBase = canonical !== '' ? canonical : url
|
|
172
|
+
const articleDocument = parseHTML(withBaseHref(String(html ?? ''), articleBase)).document
|
|
173
|
+
let article = null
|
|
174
|
+
try {
|
|
175
|
+
article = new Readability(articleDocument, {
|
|
176
|
+
charThreshold: 80,
|
|
177
|
+
keepClasses: false,
|
|
178
|
+
disableJSONLD: true,
|
|
179
|
+
}).parse()
|
|
180
|
+
} catch {
|
|
181
|
+
article = null
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const body = articleDocument.body
|
|
185
|
+
const fallbackText = String(body?.textContent ?? '').replace(/\s+/g, ' ').trim()
|
|
186
|
+
const contentHtml = article?.content ?? body?.innerHTML ?? ''
|
|
187
|
+
const textContent = String(article?.textContent ?? fallbackText).replace(/\s+/g, ' ').trim()
|
|
188
|
+
|
|
189
|
+
if (contentHtml.trim() === '' || textContent.length < 40) {
|
|
190
|
+
return {
|
|
191
|
+
ok: false,
|
|
192
|
+
reason: '没有提取到正文。页面可能是登录页、SPA 或反爬页;请用浏览器打开后复制正文,或换一个来源。',
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const title = clampText(article?.title ?? metaTitle ?? '', 300) || (() => {
|
|
197
|
+
try { return new URL(url).hostname } catch { return '未命名文章' }
|
|
198
|
+
})()
|
|
199
|
+
const turndown = makeTurndown(articleBase)
|
|
200
|
+
let markdown = cleanMarkdown(
|
|
201
|
+
turndown.turndown(contentHtml)
|
|
202
|
+
.replace(/^#\s+.*\n+/, ''),
|
|
203
|
+
)
|
|
204
|
+
let truncated = false
|
|
205
|
+
if (markdown.length > maxMarkdownChars) {
|
|
206
|
+
markdown = `${markdown.slice(0, maxMarkdownChars)}\n\n> 内容过长,已截断。`
|
|
207
|
+
truncated = true
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const excerptSource = String(article?.excerpt ?? textContent).replace(/\s+/g, ' ').trim()
|
|
211
|
+
const excerpt = excerptSource.length > 240 ? `${excerptSource.slice(0, 240)}…` : excerptSource
|
|
212
|
+
const counts = countWords(textContent)
|
|
213
|
+
const images = (markdown.match(/!\[[^\]]*\]\([^)]*\)/g) || []).length
|
|
214
|
+
const keywordList = clampText(keywords, 1_000)
|
|
215
|
+
.split(',')
|
|
216
|
+
.map(value => clampText(value, 40))
|
|
217
|
+
.filter(Boolean)
|
|
218
|
+
.slice(0, 10)
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
ok: true,
|
|
222
|
+
title,
|
|
223
|
+
byline: clampText(article?.byline ?? byline, 200),
|
|
224
|
+
siteName: clampText(article?.siteName ?? siteName, 200),
|
|
225
|
+
published: clampText(published, 64),
|
|
226
|
+
excerpt,
|
|
227
|
+
markdown,
|
|
228
|
+
textContent,
|
|
229
|
+
canonicalUrl: canonical,
|
|
230
|
+
keywords: keywordList,
|
|
231
|
+
wordCount: counts.wordCount,
|
|
232
|
+
charCount: counts.charCount,
|
|
233
|
+
lang: detectLanguage(articleDocument, textContent, counts.cjk),
|
|
234
|
+
images,
|
|
235
|
+
truncated,
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const RENDER_ALLOWED_TAGS = [
|
|
240
|
+
'a', 'abbr', 'b', 'blockquote', 'br', 'caption', 'code', 'col', 'colgroup', 'dd', 'del',
|
|
241
|
+
'details', 'div', 'dl', 'dt', 'em', 'figcaption', 'figure', 'h1', 'h2', 'h3', 'h4', 'h5',
|
|
242
|
+
'h6', 'hr', 'i', 'img', 'ins', 'kbd', 'li', 'mark', 'ol', 'p', 'pre', 'q', 's', 'section',
|
|
243
|
+
'small', 'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
|
|
244
|
+
'thead', 'tr', 'u', 'ul',
|
|
245
|
+
]
|
|
246
|
+
|
|
247
|
+
export function renderMarkdown(markdown) {
|
|
248
|
+
const raw = marked.parse(String(markdown ?? ''), { gfm: true, breaks: false, async: false })
|
|
249
|
+
return sanitizeHtml(raw, {
|
|
250
|
+
allowedTags: RENDER_ALLOWED_TAGS,
|
|
251
|
+
allowedAttributes: {
|
|
252
|
+
a: ['href', 'title', 'target', 'rel'],
|
|
253
|
+
img: ['src', 'alt', 'title', 'loading'],
|
|
254
|
+
code: ['class'],
|
|
255
|
+
th: ['align'],
|
|
256
|
+
td: ['align'],
|
|
257
|
+
},
|
|
258
|
+
allowedSchemes: ['http', 'https', 'mailto'],
|
|
259
|
+
allowedSchemesByTag: { img: ['http', 'https'] },
|
|
260
|
+
transformTags: {
|
|
261
|
+
a: (tagName, attribs) => ({
|
|
262
|
+
tagName,
|
|
263
|
+
attribs: {
|
|
264
|
+
...attribs,
|
|
265
|
+
target: '_blank',
|
|
266
|
+
rel: 'noopener noreferrer',
|
|
267
|
+
},
|
|
268
|
+
}),
|
|
269
|
+
img: (tagName, attribs) => ({
|
|
270
|
+
tagName,
|
|
271
|
+
attribs: { ...attribs, loading: 'lazy' },
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function exportMarkdown(meta, body, annotations = []) {
|
|
278
|
+
const frontmatter = dumpYaml({
|
|
279
|
+
title: meta.title,
|
|
280
|
+
url: meta.url,
|
|
281
|
+
site: meta.site,
|
|
282
|
+
author: meta.author,
|
|
283
|
+
published: meta.published,
|
|
284
|
+
clipped: meta.clippedAt,
|
|
285
|
+
tags: meta.tags,
|
|
286
|
+
}, {
|
|
287
|
+
lineWidth: 120,
|
|
288
|
+
noRefs: true,
|
|
289
|
+
quotingType: '"',
|
|
290
|
+
}).trimEnd()
|
|
291
|
+
const safeTitle = String(meta.title ?? '').replaceAll('\n', ' ').trim()
|
|
292
|
+
const source = meta.author
|
|
293
|
+
? `> 来源:[${meta.site || meta.url}](${meta.url}) · ${meta.author}`
|
|
294
|
+
: `> 来源:[${meta.site || meta.url}](${meta.url})`
|
|
295
|
+
|
|
296
|
+
const usable = (Array.isArray(annotations) ? annotations : [])
|
|
297
|
+
.filter(annotation => annotation !== null && typeof annotation === 'object' && annotation.status !== 'stale')
|
|
298
|
+
const annotationBlock = usable.length === 0 ? '' : `\n## 批注\n\n${usable.map(annotation => {
|
|
299
|
+
const quote = String(annotation.quote ?? '').replaceAll('\n', '\n> ')
|
|
300
|
+
const note = String(annotation.note ?? '').trim()
|
|
301
|
+
return `> ${quote}${note === '' ? '' : `\n\n**笔记:** ${note}`}`
|
|
302
|
+
}).join('\n\n')}\n`
|
|
303
|
+
|
|
304
|
+
return `---\n${frontmatter}\n---\n\n# ${safeTitle}\n\n${source}\n\n${String(body ?? '').trim()}\n${annotationBlock}`
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The plain text a reader sees in the rendered article. Highlight offsets are
|
|
309
|
+
* stored against this projection, not against Markdown source, so UI selections
|
|
310
|
+
* and server-side validation use the same coordinates.
|
|
311
|
+
*/
|
|
312
|
+
export function renderedText(markdown) {
|
|
313
|
+
const html = renderMarkdown(markdown)
|
|
314
|
+
const document = parseHTML(`<!doctype html><html><body>${html}</body></html>`).document
|
|
315
|
+
return String(document.body?.textContent ?? '')
|
|
316
|
+
}
|