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
45 changes: 19 additions & 26 deletions src/webfetch/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,13 @@ function htmlFragmentToPlainText(html: string): string {

function htmlFragmentToPlainTextFallback(html: string): string {
return decodeHtmlEntities(
html
.replace(TAGS_TO_REMOVE, "")
.replace(VOID_TAGS_TO_REMOVE, "")
.replace(BLOCK_BREAK_TAGS, "\n")
.replace(TAGS, "")
.replace(WHITESPACE, " ")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n[ \t]+/g, "\n")
.replace(NEWLINE_RUN, "\n\n")
.trim(),
normalizePlainText(
html
.replace(TAGS_TO_REMOVE, "")
.replace(VOID_TAGS_TO_REMOVE, "")
.replace(BLOCK_BREAK_TAGS, "\n")
.replace(TAGS, ""),
),
);
}

Expand Down Expand Up @@ -196,25 +193,21 @@ function extractReadableArticle(html: string, url: string): ReadableArticle | un
}

function normalizePlainText(text: string): string {
return decodeHtmlEntities(
text
.replace(WHITESPACE, " ")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n[ \t]+/g, "\n")
.replace(NEWLINE_RUN, "\n\n")
.trim(),
);
return text
.replace(WHITESPACE, " ")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n[ \t]+/g, "\n")
.replace(NEWLINE_RUN, "\n\n")
.trim();
}

function normalizeMarkdown(markdown: string): string {
return decodeHtmlEntities(
markdown
.replace(/\r\n?/g, "\n")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n[ \t]+/g, "\n")
.replace(NEWLINE_RUN, "\n\n")
.trim(),
);
return markdown
.replace(/\r\n?/g, "\n")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n[ \t]+/g, "\n")
.replace(NEWLINE_RUN, "\n\n")
.trim();
}

export function decodeHtmlEntities(text: string): string {
Expand Down
37 changes: 37 additions & 0 deletions test/webfetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ function newlineFixtureHtml(): string {
</html>`;
}

function literalEntityFixtureHtml(): string {
return `<!doctype html>
<html>
<body>
<article>
<h1>Literal Entity Fixture</h1>
<p>Rendered tag example: &amp;lt;custom-element&amp;gt;</p>
<p>Escaped ampersand example: AT&amp;amp;T docs</p>
</article>
</body>
</html>`;
}

async function waitUntil(assertion: () => void): Promise<void> {
const deadline = Date.now() + 500;
let lastError: unknown;
Expand Down Expand Up @@ -347,6 +360,30 @@ describe("webfetch", () => {
expect(text).not.toContain("첫 줄둘째 줄");
});

it("#given literal HTML entity examples #when fetching markdown and text #then preserves one decoded layer only", async () => {
// given
const server = await createFixtureServer((_request, response) => {
response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
response.end(literalEntityFixtureHtml());
});

// when
const markdown = textContent(
await executeWebfetch({ url: `${server.baseUrl}/literal-entity`, format: "markdown" }),
);
const text = textContent(await executeWebfetch({ url: `${server.baseUrl}/literal-entity`, format: "text" }));

// then
expect(markdown).toContain("&lt;custom-element&gt;");
expect(markdown).toContain("AT&amp;T docs");
expect(markdown).not.toContain("<custom-element>");
expect(markdown).not.toContain("AT&T docs");
expect(text).toContain("&lt;custom-element&gt;");
expect(text).toContain("AT&amp;T docs");
expect(text).not.toContain("<custom-element>");
expect(text).not.toContain("AT&T docs");
});

it("#given html page #when fetching html #then returns raw html", async () => {
// given
const html = "<h1>Raw</h1><p>HTML</p>";
Expand Down
Loading