Skip to content
Open
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
20 changes: 19 additions & 1 deletion apps/web/lib/scrape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,25 @@ describe("scrapeUrl redirect guard", () => {

const post = await scrapeUrl("https://example.com/post");

expect(post.title).toBe("AI's ’test—case");
expect(post.title).toBe("AI's \u2019test\u2014case");
expect(post.body).toBe("Tom & Jerry check");
});

it("reads valid meta tags with spaced attribute assignments", async () => {
const fetchMock = vi.fn(async () =>
new Response(
`<html><head>
<meta property = "og:title" content = "Spaced title">
<meta content = "Spaced description" name = "description">
</head></html>`,
{ status: 200, headers: { "content-type": "text/html" } },
),
);
vi.stubGlobal("fetch", fetchMock);

const post = await scrapeUrl("https://example.com/post");

expect(post.title).toBe("Spaced title");
expect(post.body).toBe("Spaced description");
});
});
4 changes: 2 additions & 2 deletions apps/web/lib/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function stripTags(html: string): string {

function metaContent(html: string, prop: string): string | null {
const patterns = [
new RegExp(`<meta[^>]+(?:property|name)=["']${prop}["'][^>]*content=["']([^"']*)["']`, "i"),
new RegExp(`<meta[^>]+content=["']([^"']*)["'][^>]*(?:property|name)=["']${prop}["']`, "i"),
new RegExp(`<meta[^>]+(?:property|name)\\s*=\\s*["']${prop}["'][^>]*content\\s*=\\s*["']([^"']*)["']`, "i"),
new RegExp(`<meta[^>]+content\\s*=\\s*["']([^"']*)["'][^>]*(?:property|name)\\s*=\\s*["']${prop}["']`, "i"),
];
for (const re of patterns) {
const m = html.match(re);
Expand Down
Loading