From 9a25999092dc2112e6eda6b6042231187abee97e Mon Sep 17 00:00:00 2001 From: German Swan Date: Wed, 20 Mar 2024 11:39:03 +0200 Subject: [PATCH 1/5] jsdom import -> require --- core/indexing/docs/article.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index ec9a0c98b..8c9c06e3e 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -2,7 +2,8 @@ import { Readability } from "@mozilla/readability"; import { Chunk } from "../.."; import { MAX_CHUNK_SIZE } from "../../llm/constants"; import { cleanFragment, cleanHeader } from "../chunk/markdown"; -import { PageData } from "./crawl" +import { PageData } from "./crawl"; +import type jsdom from "jsdom"; export type ArticleComponent = { title: string; @@ -96,7 +97,7 @@ export function chunkArticle(articleResult: Article): Chunk[] { async function extractTitlesAndBodies( html: string, ): Promise { - const { JSDOM } = await import("jsdom"); + const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; const dom = new JSDOM(html); const document = dom.window.document; @@ -123,7 +124,7 @@ export async function stringToArticle( subpath: string, ): Promise
{ try { - const { JSDOM } = await import("jsdom"); + const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; const dom = new JSDOM(htmlContent); let reader = new Readability(dom.window.document); let article = reader.parse(); @@ -133,7 +134,7 @@ export async function stringToArticle( } let article_components = await extractTitlesAndBodies(article.content); - + return { url, subpath, From 59b2040faf424b731c590f02a5f99a6b82878be0 Mon Sep 17 00:00:00 2001 From: German Swan Date: Wed, 20 Mar 2024 13:09:12 +0200 Subject: [PATCH 2/5] add warning comment remove unnecessary async/await --- core/indexing/docs/article.ts | 29 +++++++++++++++++------------ core/indexing/docs/index.ts | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index 8c9c06e3e..ceb23dbec 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -94,11 +94,17 @@ export function chunkArticle(articleResult: Article): Chunk[] { return chunks; } -async function extractTitlesAndBodies( - html: string, -): Promise { +function htmlToJSDOM(html: string) { + // This project uses the CommonJS module system. + // Do not use inline `import` (of ES modules) here. const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; - const dom = new JSDOM(html); + return new JSDOM(html); +} + +function extractTitlesAndBodies( + html: string, +): ArticleComponent[] { + const dom = htmlToJSDOM(html); const document = dom.window.document; const titles = Array.from(document.querySelectorAll("h2")); @@ -118,14 +124,13 @@ async function extractTitlesAndBodies( return result; } -export async function stringToArticle( +export function stringToArticle( url: string, - htmlContent: string, + html: string, subpath: string, -): Promise
{ +): Article | undefined { try { - const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; - const dom = new JSDOM(htmlContent); + const dom = htmlToJSDOM(html); let reader = new Readability(dom.window.document); let article = reader.parse(); @@ -133,7 +138,7 @@ export async function stringToArticle( return undefined; } - let article_components = await extractTitlesAndBodies(article.content); + let article_components = extractTitlesAndBodies(article.content); return { url, @@ -147,9 +152,9 @@ export async function stringToArticle( } } -export async function pageToArticle( +export function pageToArticle( page: PageData -): Promise
{ +): Article | undefined { try { return stringToArticle(page.url, page.html, page.path); } catch (err) { diff --git a/core/indexing/docs/index.ts b/core/indexing/docs/index.ts index 89906f0f1..79d96371c 100644 --- a/core/indexing/docs/index.ts +++ b/core/indexing/docs/index.ts @@ -29,7 +29,7 @@ export async function* indexDocs( const articles: Article[] = []; for await (const page of crawlPage(baseUrl)) { - const article = await pageToArticle(page); + const article = pageToArticle(page); if (!article) continue; articles.push(article); From 0ecac8461057685cc39cf74bf5bddb791277b9a8 Mon Sep 17 00:00:00 2001 From: German Swan Date: Wed, 20 Mar 2024 13:10:30 +0200 Subject: [PATCH 3/5] add issue to the comment --- core/indexing/docs/article.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index ceb23dbec..6e7c60e75 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -97,6 +97,7 @@ export function chunkArticle(articleResult: Article): Chunk[] { function htmlToJSDOM(html: string) { // This project uses the CommonJS module system. // Do not use inline `import` (of ES modules) here. + // See https://github.com/continuedev/continue/pull/999 const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; return new JSDOM(html); } From f6c8fbec3bd98765996801ae001b454986c025c5 Mon Sep 17 00:00:00 2001 From: German Swan Date: Thu, 21 Mar 2024 12:23:08 +0200 Subject: [PATCH 4/5] remove unnecessary async/await --- core/indexing/docs/article.ts | 11 ++++++----- core/indexing/docs/index.ts | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index 7301625dd..6e7c60e75 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -2,7 +2,8 @@ import { Readability } from "@mozilla/readability"; import { Chunk } from "../.."; import { MAX_CHUNK_SIZE } from "../../llm/constants"; import { cleanFragment, cleanHeader } from "../chunk/markdown"; -import { PageData } from "./crawl" +import { PageData } from "./crawl"; +import type jsdom from "jsdom"; export type ArticleComponent = { title: string; @@ -138,8 +139,8 @@ export function stringToArticle( return undefined; } - let article_components = await extractTitlesAndBodies(article.content); - + let article_components = extractTitlesAndBodies(article.content); + return { url, subpath, @@ -152,9 +153,9 @@ export function stringToArticle( } } -export async function pageToArticle( +export function pageToArticle( page: PageData -): Promise
{ +): Article | undefined { try { return stringToArticle(page.url, page.html, page.path); } catch (err) { diff --git a/core/indexing/docs/index.ts b/core/indexing/docs/index.ts index 89906f0f1..79d96371c 100644 --- a/core/indexing/docs/index.ts +++ b/core/indexing/docs/index.ts @@ -29,7 +29,7 @@ export async function* indexDocs( const articles: Article[] = []; for await (const page of crawlPage(baseUrl)) { - const article = await pageToArticle(page); + const article = pageToArticle(page); if (!article) continue; articles.push(article); From 942d82b5c51f1f2da39c5f688c54111598b5363d Mon Sep 17 00:00:00 2001 From: German Swan Date: Thu, 21 Mar 2024 12:34:38 +0200 Subject: [PATCH 5/5] remove require("jsdom") as it can be imported as usual --- core/indexing/docs/article.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index b13cb55fb..a931fc166 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -94,17 +94,9 @@ export function chunkArticle(articleResult: Article): Chunk[] { return chunks; } -function htmlToJSDOM(html: string) { - // This project uses the CommonJS module system. - // Do not use inline `import` (of ES modules) here. - // See https://github.com/continuedev/continue/pull/999 - const JSDOM = require("jsdom").JSDOM as typeof jsdom.JSDOM; - return new JSDOM(html); -} - function extractTitlesAndBodies( html: string, -): Promise { +): ArticleComponent[] { const dom = new JSDOM(html); const document = dom.window.document; @@ -131,7 +123,7 @@ export function stringToArticle( subpath: string, ): Article | undefined { try { - const dom = new JSDOM(htmlContent); + const dom = new JSDOM(html); let reader = new Readability(dom.window.document); let article = reader.parse(); @@ -139,7 +131,7 @@ export function stringToArticle( return undefined; } - let article_components = await extractTitlesAndBodies(article.content); + let article_components = extractTitlesAndBodies(article.content); return { url, @@ -153,9 +145,9 @@ export function stringToArticle( } } -export async function pageToArticle( +export function pageToArticle( page: PageData, -): Promise
{ +): Article | undefined { try { return stringToArticle(page.url, page.html, page.path); } catch (err) {