-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdocs.ts
More file actions
58 lines (50 loc) · 1.51 KB
/
docs.ts
File metadata and controls
58 lines (50 loc) · 1.51 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
import { JSDOM } from 'jsdom';
/**
* Fetches a list of all the docs on the langbase website
*
*
* @returns {Promise<string>} A promise that resolves to a string of all the docs on the langbase website
*/
export async function fetchDocsList() {
try {
const response = await fetch('https://langbase.com/docs/llms.txt');
if (!response.ok) {
throw new Error('Failed to fetch docs');
}
const text = await response.text();
return text;
} catch (error) {
throw new Error('Failed to fetch docs ' + JSON.stringify(error));
}
}
/**
* Fetches and converts a blog post to markdown
*
*
* @param {string} url - The URL of the blog post to fetch
* @returns {Promise<string>} A promise that resolves to a string of the blog post in markdown format
*/
export async function fetchDocsPost(url: string): Promise<string> {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch blog post');
}
const html = await response.text();
const dom = new JSDOM(html);
const document = dom.window.document;
// Remove Next.js initialization code
const scripts = document.querySelectorAll('script');
scripts.forEach(script => script.remove());
// Get the main content
const content = document.body.textContent?.trim() || '';
if (!content) {
throw new Error('No content found in docs');
}
return content;
} catch (error) {
throw new Error(
`Failed to fetch docs: ${error instanceof Error ? error.message : 'Something went wrong. Please try again.'}`
);
}
}