🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { Readability } from '@mozilla/readability';
import { Window } from 'happy-dom';
import type { TranslatorConfigObject } from 'node-html-markdown';
import { NodeHtmlMarkdown } from 'node-html-markdown';
import type { FilterOptions } from '../type';
/** Truncate HTML to 1 MB before DOM parsing to prevent CPU spikes on large pages */
export const MAX_HTML_SIZE = 1024 * 1024;
const cleanObj = <T extends object>(
obj: T,
): {
[K in keyof T as T[K] extends null ? never : K]: T[K];
} => Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== null)) as any;
interface HtmlToMarkdownOutput {
author?: string;
content: string;
description?: string;
dir?: string;
lang?: string;
length?: number;
publishedTime?: string;
siteName?: string;
title?: string;
}
export const htmlToMarkdown = (
rawHtml: string,
{ url, filterOptions }: { filterOptions: FilterOptions; url: string },
): HtmlToMarkdownOutput => {
const html = rawHtml.length > MAX_HTML_SIZE ? rawHtml.slice(0, MAX_HTML_SIZE) : rawHtml;
const window = new Window({
settings: { disableCSSFileLoading: true, disableJavaScriptEvaluation: true },
url,
});
try {
const document = window.document;
document.body.innerHTML = html;
let parsedContent: ReturnType<Readability<string>['parse']> = null;
try {
// @ts-expect-error reason: Readability expects a Document type
parsedContent = new Readability(document).parse();
} catch {
// happy-dom may throw on pages with invalid CSS selectors — fall back to raw HTML
}
const useReadability = filterOptions.enableReadability ?? true;
let htmlNode = html;
if (useReadability && parsedContent?.content) {
htmlNode = parsedContent?.content;
}
const customTranslators = (
filterOptions.pureText
? {
a: {
postprocess: (_: string, content: string) => content,
},
img: {
ignore: true,
},
}
: {}
) as TranslatorConfigObject;
const nodeHtmlMarkdown = new NodeHtmlMarkdown({}, customTranslators);
const content = nodeHtmlMarkdown.translate(htmlNode);
const result = {
author: parsedContent?.byline,
content,
description: parsedContent?.excerpt,
dir: parsedContent?.dir,
lang: parsedContent?.lang,
length: parsedContent?.length,
publishedTime: parsedContent?.publishedTime,
siteName: parsedContent?.siteName,
title: parsedContent?.title,
};
return cleanObj(result) as HtmlToMarkdownOutput;
} finally {
// Release the happy-dom Window so its DOM tree is GC-able immediately, instead
// of waiting for the function scope to drop. JS evaluation is disabled so the
// returned promise resolves synchronously in practice — fire and forget.
void window.happyDOM.close();
}
};