fix: ansible card values are build-time only (Galaxy blocks CORS)

This commit is contained in:
Kevin Horst
2026-05-08 19:26:55 +02:00
parent 88863335fd
commit a9869a55a6
3 changed files with 6 additions and 36 deletions
+1 -24
View File
@@ -12,14 +12,6 @@
} }
const platforms = { const platforms = {
"ansible-role": {
"results.0.download_count": "downloads",
"results.0.summary_fields.versions.0.name": "version",
},
"ansible-collection": {
download_count: "downloads",
"highest_version.version": "version",
},
github: { github: {
full_name: "full_name", full_name: "full_name",
description: "description", description: "description",
@@ -57,27 +49,12 @@
}, },
}; };
const formatThousands = (value) =>
value == null ? value : Number(value).toLocaleString("en-US");
const processors = { const processors = {
huggingface: { huggingface: {
description: (value) => value?.replace(/Dataset Card for .+?\s+Dataset Summary\s+/, "").trim() || value, description: (value) => value?.replace(/Dataset Card for .+?\s+Dataset Summary\s+/, "").trim() || value,
}, },
"ansible-role": {
"results.0.download_count": formatThousands,
},
"ansible-collection": {
download_count: formatThousands,
},
}; };
const getNested = (obj, path) =>
path.split(".").reduce((acc, key) => {
if (acc == null) return undefined;
return Array.isArray(acc) ? acc[Number(key)] : acc[key];
}, obj);
const platform = Object.keys(platforms).find((p) => repoId.startsWith(p)) || "github"; const platform = Object.keys(platforms).find((p) => repoId.startsWith(p)) || "github";
const mapping = platforms[platform]; const mapping = platforms[platform];
@@ -100,7 +77,7 @@
Object.entries(mapping).forEach(([dataField, elementSuffix]) => { Object.entries(mapping).forEach(([dataField, elementSuffix]) => {
const element = document.getElementById(`${repoId}-${elementSuffix}`); const element = document.getElementById(`${repoId}-${elementSuffix}`);
if (element) { if (element) {
let value = getNested(data, dataField); let value = data[dataField];
if (processors[platform]?.[dataField]) { if (processors[platform]?.[dataField]) {
value = processors[platform][dataField](value); value = processors[platform][dataField](value);
} }
@@ -200,6 +200,8 @@ The alert sign (`+` or `-`) is optional to control whether the admonition is fol
Set exactly one of `role` or `collection` per call. Set exactly one of `role` or `collection` per call.
All card values are fetched at build time via Hugo's `resources.GetRemote`. Galaxy does not allow cross-origin requests, so the card is not refreshed in the browser — rebuild the site to update the values.
**Example 1: Role** **Example 1: Role**
```md ```md
+3 -12
View File
@@ -22,7 +22,7 @@
{{- $type = "collection" -}} {{- $type = "collection" -}}
{{- end -}} {{- end -}}
{{ $id := delimit (slice "ansible" $type (partial "functions/uid.html" .)) "-" }} {{ $id := delimit (slice "ansible" (partial "functions/uid.html" .)) "-" }}
{{- $title := "" -}} {{- $title := "" -}}
{{- $description := "" -}} {{- $description := "" -}}
@@ -124,7 +124,7 @@
<span class="text-md mr-2 text-neutral-800 dark:text-neutral"> <span class="text-md mr-2 text-neutral-800 dark:text-neutral">
{{ partial "icon.html" "download" }} {{ partial "icon.html" "download" }}
</span> </span>
<div id="{{ $id }}-downloads" class="m-0 mr-5 text-md text-neutral-800 dark:text-neutral"> <div class="m-0 mr-5 text-md text-neutral-800 dark:text-neutral">
{{ template "_format-int" $downloads }} {{ template "_format-int" $downloads }}
</div> </div>
@@ -132,7 +132,7 @@
<span class="text-md mr-2 text-neutral-800 dark:text-neutral"> <span class="text-md mr-2 text-neutral-800 dark:text-neutral">
{{ partial "icon.html" "tag" }} {{ partial "icon.html" "tag" }}
</span> </span>
<div id="{{ $id }}-version" class="m-0 mr-5 text-md text-neutral-800 dark:text-neutral"> <div class="m-0 mr-5 text-md text-neutral-800 dark:text-neutral">
{{ $version }} {{ $version }}
</div> </div>
{{- end }} {{- end }}
@@ -158,15 +158,6 @@
</div> </div>
{{- end }} {{- end }}
</div> </div>
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
<script
async
type="text/javascript"
src="{{ $fetchRepo.RelPermalink }}"
integrity="{{ $fetchRepo.Data.Integrity }}"
data-repo-url="{{ $apiURL }}"
data-repo-id="{{ $id }}"></script>
</a> </a>
</div> </div>
{{- else if $apiURL -}} {{- else if $apiURL -}}