-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
sitemap.ts
153 lines (131 loc) Β· 4.3 KB
/
sitemap.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Document, DocumentInterface } from "@langchain/core/documents";
import { chunkArray } from "@langchain/core/utils/chunk_array";
import { CheerioWebBaseLoader, WebBaseLoaderParams } from "./cheerio.js";
/**
* Interface representing the parameters for initializing a SitemapLoader.
* @interface SitemapLoaderParams
* @extends WebBaseLoaderParams
*/
export interface SitemapLoaderParams extends WebBaseLoaderParams {
/**
* @property {(string | RegExp)[] | undefined} filterUrls - A list of regexes. Only URLs that match one of the filter URLs will be loaded.
* WARNING: The filter URLs are interpreted as regular expressions. Escape special characters if needed.
*/
filterUrls?: (string | RegExp)[];
/**
* The size to chunk the sitemap URLs into for scraping.
* @default {300}
*/
chunkSize?: number;
}
const DEFAULT_CHUNK_SIZE = 300;
type SiteMapElement = {
loc: string;
changefreq?: string;
lastmod?: string;
priority?: string;
};
export class SitemapLoader
extends CheerioWebBaseLoader
implements SitemapLoaderParams
{
allowUrlPatterns: (string | RegExp)[] | undefined;
chunkSize: number;
constructor(public webPath: string, params: SitemapLoaderParams = {}) {
const paramsWithDefaults = { chunkSize: DEFAULT_CHUNK_SIZE, ...params };
let path = webPath.endsWith("/") ? webPath.slice(0, -1) : webPath;
// Allow for custom sitemap paths to be passed in with the url.
path = path.endsWith(".xml") ? path : `${path}/sitemap.xml`;
super(path, paramsWithDefaults);
this.webPath = path;
this.allowUrlPatterns = paramsWithDefaults.filterUrls;
this.chunkSize = paramsWithDefaults.chunkSize;
}
_checkUrlPatterns(url: string): boolean {
if (!this.allowUrlPatterns) {
return false;
}
return !this.allowUrlPatterns.some(
(pattern) => !new RegExp(pattern).test(url)
);
}
async parseSitemap() {
const $ = await CheerioWebBaseLoader._scrape(
this.webPath,
this.caller,
this.timeout,
this.textDecoder,
{
xmlMode: true,
xml: true,
}
);
const elements: Array<SiteMapElement> = [];
$("url").each((_, element) => {
const loc = $(element).find("loc").text();
if (!loc) {
return;
}
if (this._checkUrlPatterns(loc)) {
return;
}
const changefreq = $(element).find("changefreq").text();
const lastmod = $(element).find("lastmod").text();
const priority = $(element).find("priority").text();
elements.push({ loc, changefreq, lastmod, priority });
});
$("sitemap").each((_, element) => {
const loc = $(element).find("loc").text();
if (!loc) {
return;
}
const changefreq = $(element).find("changefreq").text();
const lastmod = $(element).find("lastmod").text();
const priority = $(element).find("priority").text();
elements.push({ loc, changefreq, lastmod, priority });
});
return elements;
}
async _loadSitemapUrls(
elements: Array<SiteMapElement>
): Promise<DocumentInterface[]> {
const all = await CheerioWebBaseLoader.scrapeAll(
elements.map((ele) => ele.loc),
this.caller,
this.timeout,
this.textDecoder
);
const documents: Array<DocumentInterface> = all.map(($, i) => {
if (!elements[i]) {
throw new Error("Scraped docs and elements not in sync");
}
const text = $(this.selector).text();
const { loc: source, ...metadata } = elements[i];
// extract page metadata
const description = $("meta[name='description']").attr("content");
const title = $("meta[property='og:title']").attr("content");
const lang = $("meta[property='og:locale']").attr("content");
return new Document({
pageContent: text,
metadata: {
...metadata,
description,
title,
lang,
source: source.trim(),
},
});
});
return documents;
}
async load(): Promise<Document[]> {
const elements = await this.parseSitemap();
const chunks = chunkArray(elements, this.chunkSize);
const documents: DocumentInterface[] = [];
for await (const chunk of chunks) {
const chunkedDocuments = await this._loadSitemapUrls(chunk);
documents.push(...chunkedDocuments);
}
return documents;
}
}