From 3ee9ed824f38e5a46ffa8b3e3e171be7e3164a4d Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Tue, 29 Jul 2025 04:28:54 +0800 Subject: [PATCH] feat: add huggingface shortcode (#2376) --- assets/js/fetch-repo.js | 19 ++++- layouts/shortcodes/huggingface.html | 106 ++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 layouts/shortcodes/huggingface.html diff --git a/assets/js/fetch-repo.js b/assets/js/fetch-repo.js index 41ee97fb..c15854b0 100644 --- a/assets/js/fetch-repo.js +++ b/assets/js/fetch-repo.js @@ -36,6 +36,17 @@ stars_count: "stars_count", forks_count: "forks_count", }, + huggingface: { + description: "description", + likes: "likes", + downloads: "downloads", + }, + }; + + const processors = { + huggingface: { + description: (value) => value?.replace(/Dataset Card for .+?\s+Dataset Summary\s+/, "").trim() || value, + }, }; const platform = Object.keys(platforms).find((p) => repoId.startsWith(p)) || "github"; @@ -49,7 +60,13 @@ Object.entries(mapping).forEach(([dataField, elementSuffix]) => { const element = document.getElementById(`${repoId}-${elementSuffix}`); - if (element) element.innerHTML = data[dataField]; + if (element) { + let value = data[dataField]; + if (processors[platform]?.[dataField]) { + value = processors[platform][dataField](value); + } + element.innerHTML = value; + } }); } catch (error) { console.error(error); diff --git a/layouts/shortcodes/huggingface.html b/layouts/shortcodes/huggingface.html new file mode 100644 index 00000000..154a469a --- /dev/null +++ b/layouts/shortcodes/huggingface.html @@ -0,0 +1,106 @@ +{{ $id := delimit (slice "huggingface" (partial "functions/uid.html" .)) "-" }} +{{- $model := .Get "model" -}} +{{- $dataset := .Get "dataset" -}} +{{- $hfAPI := "" -}} +{{- $repoLink := "" -}} +{{- $type := "" -}} + +{{- if $model -}} + {{- $hfAPI = print "https://huggingface.co/api/models/" $model -}} + {{- $repoLink = print "https://huggingface.co/" $model -}} + {{- $type = "model" -}} +{{- else if $dataset -}} + {{- $hfAPI = print "https://huggingface.co/api/datasets/" $dataset -}} + {{- $repoLink = print "https://huggingface.co/datasets/" $dataset -}} + {{- $type = "dataset" -}} +{{- end -}} + +{{- $huggingfaceData := dict -}} +{{- with try (resources.GetRemote $hfAPI) -}} + {{- with .Err -}} + {{- warnf "huggingface shortcode: failed to fetch remote resource from %q: %s" $hfAPI .Position -}} + {{- else with .Value -}} + {{- $huggingfaceData = . | transform.Unmarshal -}} + {{- else -}} + {{- warnf "huggingface shortcode: unable to get remote resource from %q: %s" $hfAPI .Position -}} + {{- end -}} +{{- end -}} + +{{- with $huggingfaceData -}} +
+{{- else -}} + {{ warnf "huggingface shortcode: unable to fetch %q: %s" $hfAPI .Position }} +{{- end -}} + +{{ define "HuggingFaceSVG" }} + {{/* prettier-ignore-start */}} + + {{/* prettier-ignore-end */}} +{{ end }}