Skip to content
Open
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
42 changes: 40 additions & 2 deletions src/trending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ const SEARCH_QUERIES = [
{ q: "topic:machine-learning", label: "ml" },
];

// ---------------------------------------------------------------------------
// HTML entity decoding
// ---------------------------------------------------------------------------

const NAMED_ENTITIES: Record<string, string> = {
amp: "&",
lt: "<",
gt: ">",
quot: '"',
apos: "'",
nbsp: " ",
};

/**
* Decode the HTML entities that survive tag stripping on scraped github.com
* trending descriptions (e.g. `&amp;`, `&#39;`, `&lt;`). Handles named,
* decimal (`&#NN;`) and hex (`&#xNN;`) references in a single pass so
* already-decoded text and unknown entities are left untouched.
*/
function decodeHtmlEntities(text: string): string {
return text.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, entity: string) => {
if (entity[0] === "#") {
const codePoint =
entity[1] === "x" || entity[1] === "X"
? parseInt(entity.slice(2), 16)
: parseInt(entity.slice(1), 10);
return Number.isNaN(codePoint) || codePoint > 0x10ffff
? match
: String.fromCodePoint(codePoint);
}
return NAMED_ENTITIES[entity.toLowerCase()] ?? match;
});
}

// ---------------------------------------------------------------------------
// GitHub Trending HTML fetch
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -79,11 +113,15 @@ async function fetchGitHubTrending(): Promise<{ repos: TrendingRepo[]; success:

// description from col-9 paragraph
const descMatch = block.match(/<p[^>]*class="[^"]*col-9[^"]*"[^>]*>([\s\S]*?)<\/p>/);
const description = descMatch?.[1] ? descMatch[1].replace(/<[^>]+>/g, "").trim() : "";
const description = descMatch?.[1]
? decodeHtmlEntities(descMatch[1].replace(/<[^>]+>/g, "").trim())
: "";

// language
const langMatch = block.match(/<span[^>]+itemprop="programmingLanguage"[^>]*>([\s\S]*?)<\/span>/);
const language = langMatch?.[1] ? langMatch[1].replace(/<[^>]+>/g, "").trim() : "";
const language = langMatch?.[1]
? decodeHtmlEntities(langMatch[1].replace(/<[^>]+>/g, "").trim())
: "";

// today stars
const todayMatch = block.match(/([\d,]+)\s+stars?\s+today/i);
Expand Down