-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextract-content-convert-md.js
More file actions
292 lines (262 loc) · 10.6 KB
/
extract-content-convert-md.js
File metadata and controls
292 lines (262 loc) · 10.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
/**
* Custom JavaScript for Screaming Frog SEO Spider
* Extracts main body content (excluding header, footer, nav, aside, noscript)
* and converts it to markdown format following Mozilla Readability best practices
*/
function convertToMarkdown(element) {
function processNode(node) {
if (!node) return '';
if (node.nodeType === 3) {
let text = node.textContent;
text = text.replace(/\s+/g, ' ');
return text;
}
if (node.nodeType !== 1) {
return '';
}
const tagName = node.tagName.toLowerCase();
let result = '';
if (['header', 'footer', 'nav', 'aside', 'noscript', 'script', 'style'].indexOf(tagName) !== -1) {
return '';
}
const className = node.className || '';
const classString = typeof className === 'string' ? className : '';
const id = node.id || '';
const role = node.getAttribute('role') || '';
// Skip common header/footer/nav patterns by class or id
if (classString.indexOf('theme-header') !== -1 ||
classString.indexOf('theme-footer') !== -1 ||
classString.indexOf('site-header') !== -1 ||
classString.indexOf('site-footer') !== -1 ||
classString.indexOf('global-header') !== -1 ||
classString.indexOf('global-footer') !== -1 ||
classString.indexOf('loading-animation') !== -1 ||
classString.indexOf('cookie-banner') !== -1 ||
classString.indexOf('cookie-consent') !== -1 ||
classString.indexOf('newsletter-popup') !== -1 ||
classString.indexOf('modal-overlay') !== -1 ||
id === 'header' ||
id === 'footer' ||
id === 'site-header' ||
id === 'site-footer') {
return '';
}
// Only exclude role="banner" if it looks like a site header (at top level or has nav)
if (role === 'banner' && (node.querySelector('nav') || node.parentElement === document.body)) {
return '';
}
// Only exclude role="navigation"
if (role === 'navigation') {
return '';
}
// Only exclude role="complementary" if it's a sidebar-like element (small portion of content)
if (role === 'complementary') {
const parentWidth = node.parentElement ? node.parentElement.offsetWidth : 0;
const nodeWidth = node.offsetWidth || 0;
// Skip if it's less than 40% of parent width (likely a sidebar)
if (parentWidth > 0 && nodeWidth < parentWidth * 0.4) {
return '';
}
}
function getChildrenText() {
let text = '';
const children = node.childNodes;
for (let i = 0; i < children.length; i++) {
text += processNode(children[i]);
}
return text;
}
switch (tagName) {
case 'h1':
result = '\n\n# ' + getChildrenText().trim() + '\n\n';
break;
case 'h2':
result = '\n\n## ' + getChildrenText().trim() + '\n\n';
break;
case 'h3':
result = '\n\n### ' + getChildrenText().trim() + '\n\n';
break;
case 'h4':
result = '\n\n#### ' + getChildrenText().trim() + '\n\n';
break;
case 'h5':
result = '\n\n##### ' + getChildrenText().trim() + '\n\n';
break;
case 'h6':
result = '\n\n###### ' + getChildrenText().trim() + '\n\n';
break;
case 'p':
result = '\n\n' + getChildrenText().trim() + '\n\n';
break;
case 'br':
result = ' \n';
break;
case 'strong':
case 'b':
result = '**' + getChildrenText() + '**';
break;
case 'em':
case 'i':
result = '*' + getChildrenText() + '*';
break;
case 'code':
if (node.parentElement && node.parentElement.tagName.toLowerCase() === 'pre') {
result = getChildrenText();
} else {
result = '`' + getChildrenText() + '`';
}
break;
case 'pre':
result = '\n\n```\n' + getChildrenText().trim() + '\n```\n\n';
break;
case 'a':
const href = node.getAttribute('href') || '';
const linkText = getChildrenText().trim();
if (href && linkText) {
result = '[' + linkText + '](' + href + ')';
} else {
result = linkText;
}
break;
case 'img':
const src = node.getAttribute('src') || '';
const alt = node.getAttribute('alt') || '';
if (src) {
result = '';
}
break;
case 'ul':
case 'ol':
result = '\n\n';
const items = node.children;
let itemIndex = 0;
for (let i = 0; i < items.length; i++) {
if (items[i].tagName.toLowerCase() === 'li') {
itemIndex++;
const prefix = tagName === 'ul' ? '- ' : itemIndex + '. ';
result += prefix + processNode(items[i]).trim() + '\n';
}
}
result += '\n';
break;
case 'li':
result = getChildrenText();
break;
case 'blockquote':
const quoteText = getChildrenText().trim();
result = '\n\n> ' + quoteText.replace(/\n/g, '\n> ') + '\n\n';
break;
case 'hr':
result = '\n\n---\n\n';
break;
case 'table':
result = '\n\n' + processTable(node) + '\n\n';
break;
case 'del':
case 's':
case 'strike':
result = '~~' + getChildrenText() + '~~';
break;
default:
result = getChildrenText();
break;
}
return result;
}
function processTable(table) {
let md = '';
const rows = table.querySelectorAll('tr');
if (rows.length === 0) return '';
let headers = [];
let isFirstRowHeader = false;
const firstRowCells = rows[0].querySelectorAll('th, td');
if (rows[0].querySelector('th')) {
isFirstRowHeader = true;
for (let i = 0; i < firstRowCells.length; i++) {
headers.push(firstRowCells[i].textContent.trim());
}
} else {
for (let i = 0; i < firstRowCells.length; i++) {
headers.push(firstRowCells[i].textContent.trim());
}
isFirstRowHeader = true;
}
md += '| ' + headers.join(' | ') + ' |\n';
md += '| ' + headers.map(function() { return '---'; }).join(' | ') + ' |\n';
const startRow = isFirstRowHeader ? 1 : 0;
for (let i = startRow; i < rows.length; i++) {
const cells = rows[i].querySelectorAll('td, th');
const cellTexts = [];
for (let j = 0; j < cells.length; j++) {
cellTexts.push(cells[j].textContent.trim());
}
if (cellTexts.length > 0) {
md += '| ' + cellTexts.join(' | ') + ' |\n';
}
}
return md;
}
let markdown = processNode(element);
markdown = markdown
.replace(/\n{3,}/g, '\n\n')
.replace(/^\s+|\s+$/g, '')
.replace(/ +/g, ' ');
return markdown;
}
let contentRoot = document.querySelector('.entry-content') ||
document.querySelector('.wp-block-post-content') ||
document.querySelector('main article') ||
document.querySelector('main') ||
document.querySelector('article') ||
document.querySelector('[role="main"]') ||
document.querySelector('.content') ||
document.querySelector('.main-content') ||
document.querySelector('.page-content') ||
document.querySelector('.site-content') ||
document.querySelector('.post-content') ||
document.querySelector('.article-content') ||
document.querySelector('.body-content') ||
document.querySelector('[data-content]') ||
document.querySelector('[data-main-content]') ||
document.querySelector('.layout-container') ||
document.querySelector('.container') ||
document.querySelector('#content') ||
document.querySelector('#main') ||
document.querySelector('#main-content') ||
document.body;
let markdownContent = convertToMarkdown(contentRoot);
// Fallback: if content is too short, try extracting from body with relaxed filtering
if (markdownContent.length < 500 && contentRoot !== document.body) {
const bodyContent = convertToMarkdown(document.body);
if (bodyContent.length > markdownContent.length) {
markdownContent = bodyContent;
}
}
// Second fallback: if still too short, extract all visible text from common content elements
if (markdownContent.length < 500) {
const contentSelectors = [
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'li', 'td', 'th', 'blockquote', 'figcaption',
'[class*="text"]', '[class*="content"]', '[class*="body"]',
'[class*="description"]', '[class*="paragraph"]'
];
let fallbackText = [];
contentSelectors.forEach(function(selector) {
try {
document.querySelectorAll(selector).forEach(function(el) {
// Skip if inside excluded elements
if (el.closest('header, footer, nav, aside, noscript, script, style')) {
return;
}
const text = el.textContent.trim();
if (text.length > 20 && fallbackText.indexOf(text) === -1) {
fallbackText.push(text);
}
});
} catch(e) {}
});
if (fallbackText.join('\n\n').length > markdownContent.length) {
markdownContent = fallbackText.join('\n\n');
}
}
return seoSpider.data(markdownContent);