fix(duckduckgo): preserve words around highlighted matches (#111460)

This commit is contained in:
mushuiyu886
2026-07-20 10:09:09 +08:00
committed by GitHub
parent 27f05c8993
commit c5bc6c3d2d
2 changed files with 18 additions and 1 deletions
+3 -1
View File
@@ -53,7 +53,9 @@ function decodeHtmlEntities(text: string): string {
}
function stripHtml(html: string): string {
return html
// DuckDuckGo match highlights can occur inside words, so remove them without adding whitespace.
const withoutMatchHighlights = html.replace(/<\/?b\b[^>]*>/gi, "");
return withoutMatchHighlights
.replace(/<[^>]+>/g, " ")
.replace(/\s+/g, " ")
.trim();
@@ -254,6 +254,21 @@ describe("duckduckgo web search provider", () => {
]);
});
it("keeps inline result markup from splitting words", () => {
const html = `
<a class="result__a" href="https://example.com/cafe">Caf<b>é</b> guide</a>
<a class="result__snippet">Find the best caf<b>é</b> near you.</a>
`;
expect(ddgClientTesting.parseDuckDuckGoHtml(html)).toEqual([
{
title: "Café guide",
url: "https://example.com/cafe",
snippet: "Find the best café near you.",
},
]);
});
it("detects bot challenge pages without flagging ordinary result snippets", () => {
const challengeHtml = `
<html>