gabriel / musehub public
repo-page.ts typescript
73 lines 2.5 KB
Raw
sha256:bb2baaabdd19320bde50cb69d447fd1c1e571467df729be1a23b5e93064b046a feat(intel): standardize headers, gauge icon, velocity card… Sonnet 4.6 minor ⚠ breaking 142 days ago
1 /**
2 * repo-page.ts — universal initialiser for every repo-scoped page.
3 *
4 * Pages that only need repo-nav hydration register `{ "page": "repo" }` (or
5 * include `"repo_id"` in their page_json block). More complex pages extend
6 * this and register their own key in MusePages.
7 *
8 * Also runs highlight.js over any .rh-readme-body code blocks so that fenced
9 * code in READMEs gets syntax highlighting without a separate page module.
10 *
11 * Registered as: window.MusePages['repo']
12 */
13
14 import hljs from 'highlight.js/lib/core';
15 import python from 'highlight.js/lib/languages/python';
16 import typescript from 'highlight.js/lib/languages/typescript';
17 import javascript from 'highlight.js/lib/languages/javascript';
18 import bash from 'highlight.js/lib/languages/bash';
19 import rust from 'highlight.js/lib/languages/rust';
20 import go from 'highlight.js/lib/languages/go';
21 import yaml from 'highlight.js/lib/languages/yaml';
22 import json from 'highlight.js/lib/languages/json';
23 import toml from 'highlight.js/lib/languages/ini'; // toml ~= ini
24
25 hljs.registerLanguage('python', python);
26 hljs.registerLanguage('typescript', typescript);
27 hljs.registerLanguage('javascript', javascript);
28 hljs.registerLanguage('bash', bash);
29 hljs.registerLanguage('sh', bash);
30 hljs.registerLanguage('shell', bash);
31 hljs.registerLanguage('rust', rust);
32 hljs.registerLanguage('go', go);
33 hljs.registerLanguage('yaml', yaml);
34 hljs.registerLanguage('json', json);
35 hljs.registerLanguage('toml', toml);
36
37 export interface RepoPageData {
38 page?: string;
39 repo_id?: string;
40 clone_url?: string;
41 [key: string]: unknown;
42 }
43
44 function initStarToggle(): void {
45 const starBtn = document.getElementById('nav-star-btn');
46 const statCard = document.getElementById('stat-stars');
47 [starBtn, statCard].forEach((el) => {
48 el?.addEventListener('click', (e) => {
49 e.preventDefault();
50 const w = window as unknown as Record<string, unknown>;
51 if (typeof w.toggleStar === 'function') {
52 (w.toggleStar as () => void)();
53 }
54 });
55 });
56 }
57
58 function highlightReadme(): void {
59 document.querySelectorAll<HTMLElement>('.rh-readme-body pre code').forEach((block) => {
60 if (block.dataset['highlighted'] !== 'yes') {
61 hljs.highlightElement(block);
62 }
63 });
64 }
65
66 export function initRepoPage(data: RepoPageData): void {
67 const repoId = data.repo_id;
68 if (repoId && typeof window.initRepoNav === 'function') {
69 window.initRepoNav(String(repoId));
70 }
71 initStarToggle();
72 highlightReadme();
73 }
File History 1 commit
sha256:bb2baaabdd19320bde50cb69d447fd1c1e571467df729be1a23b5e93064b046a feat(intel): standardize headers, gauge icon, velocity card… Sonnet 4.6 minor 142 days ago