repo-page.ts
typescript
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa
Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As…
Human
2 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 highlightReadme(): void { |
| 45 | document.querySelectorAll<HTMLElement>('.rh-readme-body pre code').forEach((block) => { |
| 46 | if (block.dataset['highlighted'] !== 'yes') { |
| 47 | hljs.highlightElement(block); |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function initReadmeAnchors(): void { |
| 53 | document.querySelectorAll<HTMLElement>('.rh-readme-body h1,.rh-readme-body h2,.rh-readme-body h3,.rh-readme-body h4,.rh-readme-body h5,.rh-readme-body h6').forEach((heading) => { |
| 54 | if (heading.querySelector('.rh-md-anchor')) return; |
| 55 | const slug = heading.id; |
| 56 | if (!slug) return; |
| 57 | const anchor = document.createElement('a'); |
| 58 | anchor.href = '#' + slug; |
| 59 | anchor.className = 'rh-md-anchor'; |
| 60 | anchor.textContent = '#'; |
| 61 | anchor.setAttribute('aria-hidden', 'true'); |
| 62 | heading.appendChild(anchor); |
| 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 | highlightReadme(); |
| 81 | initReadmeAnchors(); |
| 82 | initCloneCopyButtons(); |
| 83 | } |
File History
3 commits
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa
Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As…
Human
2 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f
fix: use wire_bytes not mpack_bytes_raw in compute_object_b…
Sonnet 4.6
patch
10 days ago
sha256:4aed3d8601c8dd3ed37074de35f11f4a9699a0a4b99d43727048fd3f8e6fd13d
chore: doc sweep, ignore wrangler build state, misc fixes
Sonnet 4.6
minor
⚠
13 days ago