Files
joelhooks__joelclaw/apps/web/lib/rehype-normalize-code-langs.ts
Joel Hooks 016dccc295 redesign: cool finds index — full titles, readable descriptions, compact cards
- Titles wrap fully instead of truncating
- Relevance text shows complete (no line-clamp)
- Date shown as relative time (2d ago) above title, not stealing width
- Single type badge (repo/article/video) replaces full tag list
- Source URL removed from index (shown on detail page)
- Tighter card spacing for better scan density
- Re-removed duplicate self-hosting discovery (also from Vault source)
2026-02-22 08:07:35 -08:00

40 lines
1.2 KiB
TypeScript

import { visit } from "unist-util-visit";
const LANGUAGE_ALIASES: Record<string, string> = {
sh: "bash",
shell: "bash",
shellscript: "bash",
zsh: "bash",
yml: "yaml",
ts: "typescript",
js: "javascript",
py: "python",
md: "markdown",
gql: "graphql",
};
/**
* Normalize common fenced-code language aliases before Shiki runs.
* This keeps highlighting deterministic even when authors use shorthand tags.
*/
export function rehypeNormalizeCodeLangs() {
return (tree: unknown) => {
visit(tree, "element", (node: any) => {
if (node.tagName !== "code") return;
const className = node.properties?.className;
if (!Array.isArray(className)) return;
const langClassIndex = className.findIndex(
(value) => typeof value === "string" && value.startsWith("language-"),
);
if (langClassIndex === -1) return;
const langClass = className[langClassIndex] as string;
const rawLang = langClass.slice("language-".length).toLowerCase();
const normalizedLang = LANGUAGE_ALIASES[rawLang] ?? rawLang;
className[langClassIndex] = `language-${normalizedLang}`;
node.properties.className = className;
});
};
}