continue-custom/core/context/providers/URLContextProvider.ts
2024-09-24 16:26:46 -07:00

62 lines
1.5 KiB
TypeScript

import { Readability } from "@mozilla/readability";
import { JSDOM } from "jsdom";
import { NodeHtmlMarkdown } from "node-html-markdown";
import { BaseContextProvider } from "../";
import {
ContextItem,
ContextProviderDescription,
ContextProviderExtras,
} from "../../index.js";
import { fetchFavicon } from "../../util/fetchFavicon";
class URLContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
title: "url",
displayTitle: "URL",
description: "Reference a webpage at a given URL",
type: "query",
};
async getContextItems(
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
try {
const url = new URL(query);
const icon = await fetchFavicon(url);
const resp = await extras.fetch(url);
const html = await resp.text();
const dom = new JSDOM(html);
let reader = new Readability(dom.window.document);
let article = reader.parse();
const content = article?.content || "";
const markdown = NodeHtmlMarkdown.translate(
content,
{},
undefined,
undefined,
);
const title = article?.title || url.pathname;
return [
{
icon,
description: url.toString(),
content: markdown,
name: title,
uri: {
type: "url",
value: url.toString(),
},
},
];
} catch (e) {
console.log(e);
return [];
}
}
}
export default URLContextProvider;