docs(docs): add BacklinksList component and shared tagTarget logic for badge routing, update docs pages

This commit is contained in:
Prad Nukala committed 2026-09-29 15:12:54 -04:00
1 parent eea8d242e0
commit 40e965aa06
5 files changed
+161 -23

No files matched your search

+33 -1
View File
@@ -1,10 +1,12 @@
/**
* LeetCode topic-tag slug → the campaign landing routes that teach it, in
* default preference order. The one answer to "does the campaign cover this
* tag", read by two sides that must agree:
* tag", read by the sides that must agree:
*
* - `components/ProblemBadges.astro` links a covered tag's badge to its
* landing page, and every other tag's badge to `/guides/<slug>`;
* - `components/BacklinksList.astro` lists, under a landing or guide page,
* every problem whose badge links there — both via `tagTarget()`;
* - `apps/cli/sync.ts` creates a placeholder `content/guides/<slug>.mdx` for
* every tag a problem page carries that is NOT a key here.
*
@@ -33,3 +35,33 @@ export const TOPIC_LANDINGS: Record<string, readonly string[]> = {
"dynamic-programming": ["/1d-dynamic-programming", "/multi-d-grid-dp"],
greedy: ["/greedy"],
};
/** One of LeetCode's topic tags, as a problem page's `topics:` frontmatter carries it. */
export interface TopicTag {
name: string;
slug: string;
}
/**
* Where a problem page's badge for `tag` points: a root-relative landing or
* guide route (the caller adds the base), or LeetCode's problem list when the
* site serves neither, so a badge never links to a 404.
*
* A tag naming two landings links to the page's own topic — its route's first
* segment — when that is one of them; otherwise DFS/BFS prefer the tree topic
* for a problem LeetCode also tags `Tree` and the graph topic for the rest.
*/
export function tagTarget(
tag: TopicTag,
page: { route: string; tags: readonly TopicTag[] },
served: ReadonlySet<string>,
): { href: string; kind: "landing" | "guide" | "external" } {
const landings = (TOPIC_LANDINGS[tag.slug] ?? []).filter((path) => served.has(path));
const own = `/${page.route.split("/")[1]}`;
const prefer = page.tags.some((t) => t.slug === "tree") ? "/tree-" : "/graph-";
const landing = landings.includes(own) ? own : (landings.find((p) => p.startsWith(prefer)) ?? landings[0]);
if (landing) return { href: landing, kind: "landing" };
const guide = `/guides/${tag.slug}`;
if (served.has(guide)) return { href: guide, kind: "guide" };
return { href: `https://leetcode.com/problem-list/${tag.slug}/`, kind: "external" };
}