Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/webfetch/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ function extractReadableArticle(html: string, url: string): ReadableArticle | un
};
break;
}
if (explicitArticle) return explicitArticle;

const article = new Readability(dom.window.document, {
charThreshold: 80,
keepClasses: false,
}).parse();
if (explicitArticle) return explicitArticle;
if (!article?.content || !article.textContent) return undefined;
return {
title: normalizePlainText(
Expand Down
45 changes: 45 additions & 0 deletions test/webfetch-explicit-article.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const readability = vi.hoisted(() => ({
parse: vi.fn(() => {
throw new Error("Readability should not run for explicit article matches");
}),
}));

vi.mock("@mozilla/readability", () => ({
Readability: class {
parse(): unknown {
return readability.parse();
}
},
}));

import { htmlToMarkdown } from "../src/webfetch/content.js";

function explicitArticleHtml(): string {
return `<!doctype html>
<html>
<body>
<h1 class="tit_post">Explicit Article</h1>
<div class="article_view">
<p>Explicit article body has enough words to pass the direct article selector threshold.</p>
</div>
</body>
</html>`;
}

describe("webfetch explicit article extraction", () => {
beforeEach(() => {
readability.parse.mockClear();
});

it("#given an explicit article container #when converting markdown #then skips Readability fallback parsing", () => {
// given / when
const markdown = htmlToMarkdown(explicitArticleHtml(), "https://example.test/post");

// then
expect(markdown).toContain("# Explicit Article");
expect(markdown).toContain("Explicit article body");
expect(readability.parse).not.toHaveBeenCalled();
});
});
Loading