gabriel / musehub public
repo-page.ts typescript
83 lines 2.9 KB
Raw
sha256:f99af7b1a7f36c4d537d1c630d4b71fc39222b1255f82e930929e2fc89015e11 fix: relax browse_repo perf budget to 500ms — 200ms was too… Sonnet 4.6 100 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 function initCloneCopyButtons(): void {
67 document.querySelectorAll<HTMLButtonElement>('.clone-copy-btn').forEach((btn) => {
68 btn.addEventListener('click', () => {
69 const input = btn.previousElementSibling as HTMLInputElement | null;
70 if (input?.value) void navigator.clipboard.writeText(input.value);
71 });
72 });
73 }
74
75 export function initRepoPage(data: RepoPageData): void {
76 const repoId = data.repo_id;
77 if (repoId && typeof window.initRepoNav === 'function') {
78 window.initRepoNav(String(repoId));
79 }
80 initStarToggle();
81 highlightReadme();
82 initCloneCopyButtons();
83 }
File History 2 commits
sha256:f99af7b1a7f36c4d537d1c630d4b71fc39222b1255f82e930929e2fc89015e11 fix: relax browse_repo perf budget to 500ms — 200ms was too… Sonnet 4.6 100 days ago
sha256:763eb2cb8675073b84c19345b27586d2ed939a9aee97c5479b69f502f1a70eff fix(tests): update test suite to match current implementation Sonnet 4.6 patch 121 days ago