11/// <reference types="mdast" />
22import { h } from "hastscript" ;
3+ import githubCardData from "../constants/github-card-data.json" with {
4+ type : "json" ,
5+ } ;
6+ import { isValidGithubRepository } from "../utils/github-card-utils.ts" ;
7+
8+ function formatCount ( value ) {
9+ return Intl . NumberFormat ( "en-us" , {
10+ notation : "compact" ,
11+ maximumFractionDigits : 1 ,
12+ } )
13+ . format ( value )
14+ . replaceAll ( "\u202f" , "" ) ;
15+ }
316
417/**
518 * Creates a GitHub Card component.
@@ -15,7 +28,7 @@ export function GithubCardComponent(properties, children) {
1528 'Invalid directive. ("github" directive must be leaf type "::github{repo="owner/repo"}")' ,
1629 ] ) ;
1730
18- if ( ! properties . repo ?. includes ( "/" ) )
31+ if ( ! isValidGithubRepository ( properties . repo ) )
1932 return h (
2033 "div" ,
2134 { class : "hidden" } ,
@@ -24,12 +37,23 @@ export function GithubCardComponent(properties, children) {
2437
2538 const repo = properties . repo ;
2639 const cardUuid = `GC${ Math . random ( ) . toString ( 36 ) . slice ( - 6 ) } ` ; // Collisions are not important
40+ const data = githubCardData [ repo . toLowerCase ( ) ] ?? null ;
41+ const hasData = data !== null ;
2742
28- const nAvatar = h ( `div#${ cardUuid } -avatar` , { class : "gc-avatar" } ) ;
43+ const avatarUrl = data ?. avatarUrl
44+ ? `${ data . avatarUrl } ${ data . avatarUrl . includes ( "?" ) ? "&" : "?" } s=32`
45+ : null ;
46+ const avatarStyle = avatarUrl
47+ ? `background-image: url("${ avatarUrl } "); background-color: transparent;`
48+ : undefined ;
49+ const nAvatar = h ( `div#${ cardUuid } -avatar` , {
50+ class : "gc-avatar" ,
51+ style : avatarStyle ,
52+ } ) ;
2953 const nLanguage = h (
3054 `span#${ cardUuid } -language` ,
3155 { class : "gc-language" } ,
32- "Waiting... ",
56+ data ?. language ?? "Unavailable ",
3357 ) ;
3458
3559 const nTitle = h ( "div" , { class : "gc-titlebar" } , [
@@ -47,40 +71,31 @@ export function GithubCardComponent(properties, children) {
4771 const nDescription = h (
4872 `div#${ cardUuid } -description` ,
4973 { class : "gc-description" } ,
50- "Waiting for api.github.com..." ,
74+ hasData
75+ ? data . description || "Description not set"
76+ : "Repository details unavailable" ,
5177 ) ;
5278
53- const nStars = h ( `div#${ cardUuid } -stars` , { class : "gc-stars" } , "00K" ) ;
54- const nForks = h ( `div#${ cardUuid } -forks` , { class : "gc-forks" } , "0K" ) ;
55- const nLicense = h ( `div#${ cardUuid } -license` , { class : "gc-license" } , "0K" ) ;
56-
57- const nScript = h (
58- `script#${ cardUuid } -script` ,
59- { type : "text/javascript" , defer : true } ,
60- `
61- fetch('https://api.github.com/repos/${ repo } ', { referrerPolicy: "no-referrer" }).then(response => response.json()).then(data => {
62- document.getElementById('${ cardUuid } -description').innerText = data.description?.replace(/:[a-zA-Z0-9_]+:/g, '') || "Description not set";
63- document.getElementById('${ cardUuid } -language').innerText = data.language;
64- document.getElementById('${ cardUuid } -forks').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.forks).replaceAll("\u202f", '');
65- document.getElementById('${ cardUuid } -stars').innerText = Intl.NumberFormat('en-us', { notation: "compact", maximumFractionDigits: 1 }).format(data.stargazers_count).replaceAll("\u202f", '');
66- const avatarEl = document.getElementById('${ cardUuid } -avatar');
67- avatarEl.style.backgroundImage = 'url(' + data.owner.avatar_url + '&s=32' + ')';
68- avatarEl.style.backgroundColor = 'transparent';
69- document.getElementById('${ cardUuid } -license').innerText = data.license?.spdx_id || "no-license";
70- document.getElementById('${ cardUuid } -card').classList.remove("fetch-waiting");
71- console.log("[GITHUB-CARD] Loaded card for ${ repo } | ${ cardUuid } .")
72- }).catch(err => {
73- const c = document.getElementById('${ cardUuid } -card');
74- c?.classList.add("fetch-error");
75- console.warn("[GITHUB-CARD] (Error) Loading card for ${ repo } | ${ cardUuid } .")
76- })
77- ` ,
79+ const nStars = h (
80+ `div#${ cardUuid } -stars` ,
81+ { class : "gc-stars" } ,
82+ hasData ? formatCount ( data . stars ) : "—" ,
83+ ) ;
84+ const nForks = h (
85+ `div#${ cardUuid } -forks` ,
86+ { class : "gc-forks" } ,
87+ hasData ? formatCount ( data . forks ) : "—" ,
88+ ) ;
89+ const nLicense = h (
90+ `div#${ cardUuid } -license` ,
91+ { class : "gc-license" } ,
92+ hasData ? data . license || "no-license" : "—" ,
7893 ) ;
7994
8095 return h (
8196 `a#${ cardUuid } -card` ,
8297 {
83- class : " card-github fetch-waiting no-styling" ,
98+ class : ` card-github${ hasData ? "" : " fetch-error" } no-styling` ,
8499 href : `https://github.com/${ repo } ` ,
85100 target : "_blank" ,
86101 repo,
@@ -89,7 +104,6 @@ export function GithubCardComponent(properties, children) {
89104 nTitle ,
90105 nDescription ,
91106 h ( "div" , { class : "gc-infobar" } , [ nStars , nForks , nLicense , nLanguage ] ) ,
92- nScript ,
93107 ] ,
94108 ) ;
95109}
0 commit comments