Repository navigation
Expand file tree
/
Copy pathhtml.ts
More file actions
150 lines (120 loc) · 3.17 KB
/
Copy pathhtml.ts
File metadata and controls
150 lines (120 loc) · 3.17 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
import * as cheerio from 'cheerio'
export function convertHTMLAsDOM(_html: string) {
const html = _html.replace(/<\//g, ' </')
const $ = cheerio.load(html)
$('script, style').remove()
const important = [
'main',
'[role="main"]',
'#bodyContent',
'#search',
'#searchform',
'.kp-header'
]
// move important content to top
important.forEach((im) => {
$(im).each((i, el) => {
$(el).prependTo('body')
})
})
return $
}
export function makeTag(element: cheerio.Element) {
const $ = cheerio.load('<body></body>')
let textContent = $(element).text().replace(/\s+/g, ' ').trim()
let placeholder = $(element).attr('placeholder')
let tagName = element.name
let title = $(element).attr('title')
let value = $(element).attr('value')
let role = $(element).attr('role')
let type = $(element).attr('type')
let href = $(element).attr('href')
let pgpt_id = $(element).attr('pgpt-id')
if (href && href.length > 32) {
href = href.substring(0, 32) + '[..]'
}
if (placeholder && placeholder.length > 32) {
placeholder = placeholder.substring(0, 32) + '[..]'
}
if (title && title.length > 32) {
title = title.substring(0, 32) + '[..]'
}
if (textContent && textContent.length > 200) {
textContent = textContent.substring(0, 200) + '[..]'
}
let tag = `<${tagName}`
if (href) {
tag += ` href="${href}"`
}
if (type) {
tag += ` type="${type}"`
}
if (placeholder) {
tag += ` placeholder="${placeholder}"`
}
if (title) {
tag += ` title="${title}"`
}
if (role) {
tag += ` role="${role}"`
}
if (value) {
tag += ` value="${value}"`
}
if (pgpt_id) {
tag += ` pgpt-id="${pgpt_id}"`
}
tag += `>`
let obj: Record<string, any> = {
tag: tag
}
if (textContent) {
obj.text = textContent
obj.tag += `${textContent}</${tagName}>`
}
return obj
}
export function summarizeImportantHTML(html: string) {
const $ = convertHTMLAsDOM('<body>' + html + '</body>')
function traverse(element: cheerio.Element) {
let output = ''
let children = element.children
if ($(element).is('h1, h2, h3, h4, h5, h6')) {
output += '<' + element.name + '>'
}
if ($(element).is('form')) {
output += '\n<' + element.name + '>\n'
}
if ($(element).is('div, section, main')) {
output += '\n'
}
let the_tag = makeTag(element)
if ($(element).attr('pgpt-id')) {
output += ' ' + (the_tag.tag ? the_tag.tag : '')
} else if (
(element.type as any) === 'text' &&
!$(element.parent).attr('pgpt-id')
) {
output += ' ' + (element as any).data.trim()
}
if (children) {
children.forEach((child) => {
output += traverse(child as cheerio.Element)
})
}
if ($(element).is('h1, h2, h3, h4, h5, h6')) {
output += '</' + element.name + '>'
}
if ($(element).is('form')) {
output += '\n</' + element.name + '>\n'
}
if ($(element).is('h1, h2, h3, h4, h5, h6, div, section, main')) {
output += '\n'
}
return output
.replace(/[^\S\n]+/g, ' ')
.replace(/ \n+/g, '\n')
.replace(/[\n]+/g, '\n')
}
return traverse($('body')[0])
}