/
index.ts
138 lines (121 loc) · 3.66 KB
/
index.ts
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
import titleExtractor from './title-extractor'
import urlExtractor from './url-extractor'
import { DOMParser } from 'linkedom'
import defaultExtractors, {
DefaultExtractors,
TitleExtracted,
UrlExtracted
} from './default-extractors'
import { DeepMerged, NestedPartial } from './utils/types'
import { dedupe, deepMerge } from './utils/memoized-functions'
import { Extractor, ExtractorExtracted } from './utils/extractors'
import sanitizeHtml, { defaultSanitizeOptions } from './utils/sanitize-html'
import sanitize from 'sanitize-html'
import { absolutifyDocument } from './utils/urls'
type MergedExtracted<T> = NestedPartial<DeepMerged<T>> &
TitleExtracted &
UrlExtracted
type DefaultExtracted = MergedExtracted<ExtractorExtracted<DefaultExtractors>>
export { selectors as contentSelectors } from './content-extractor/selector-extractors'
export interface ExtractOptions<T> {
/**
* Url for the page. **may not be the final result**
* @see urlExtractor
*/
url?: string
/**
* Extractors for extract.
* @see defaultExtractors
*/
extractors?: Extractor<T>[]
/**
* Options of sanitize-html
* @see https://www.npmjs.com/package/sanitize-html
*/
sanitizeHtml?: sanitize.IOptions
/**
* For parse date
* @see mapToNearestLanguage
*/
lang?: string
}
export async function extract(
html: string | Document
): Promise<DefaultExtracted>
export async function extract<T>(
html: string | Document,
options: Omit<ExtractOptions<T>, 'extractors'>
): Promise<DefaultExtracted>
export async function extract<T>(
html: string | Document,
options?: ExtractOptions<T>
): Promise<MergedExtracted<T>>
export async function extract<T extends object>(
html: string | Document,
options: ExtractOptions<T> = {}
) {
const finalOptions = {
extractors: <Extractor<T>[]>defaultExtractors,
sanitizeHtml: defaultSanitizeOptions,
...options
}
let document =
typeof html === 'string'
? (new DOMParser().parseFromString(
sanitizeHtml(html, finalOptions.sanitizeHtml),
'text/html'
) as unknown as Document)
: html
finalOptions.lang ??= document.documentElement.lang
const title = titleExtractor.selector(
titleExtractor.processor(
// eslint-disable-next-line unicorn/no-useless-spread
[
...titleExtractor.operators
.flatMap((it) => it[1](document))
.filter((it): it is string => !!it)
],
finalOptions
),
undefined,
finalOptions
)
const url = urlExtractor.selector(
urlExtractor.processor(
// eslint-disable-next-line unicorn/no-useless-spread
[
...urlExtractor.operators
.flatMap((it) => it[1](document))
.filter((it): it is string => !!it)
],
finalOptions
),
title.title,
finalOptions
)
if (!document.baseURI && url?.url) {
const base = document.createElement('base')
base.setAttribute('href', url.url)
document.head.append(base)
}
document = absolutifyDocument(document)
const context = {
...finalOptions,
url: url?.url
}
const results = await Promise.all(finalOptions.extractors).then((it) =>
it
.map(({ operators, processor, selector }) => {
const operated = operators
.flatMap((it) => it[1](document, url.url))
.filter((it): it is string => !!it)
const processed = dedupe(processor([...operated], context), (it) => it)
if (processed.length > 0)
return selector(processed, title.title, context)
return
})
.filter((it): it is T => !!it)
)
const result = deepMerge(title, url, ...results)
return <MergedExtracted<T> & TitleExtracted & UrlExtracted>result
}