fix: ansible card values are build-time only (Galaxy blocks CORS)
This commit is contained in:
+1
-24
@@ -12,14 +12,6 @@
|
||||
}
|
||||
|
||||
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: {
|
||||
full_name: "full_name",
|
||||
description: "description",
|
||||
@@ -57,27 +49,12 @@
|
||||
},
|
||||
};
|
||||
|
||||
const formatThousands = (value) =>
|
||||
value == null ? value : Number(value).toLocaleString("en-US");
|
||||
|
||||
const processors = {
|
||||
huggingface: {
|
||||
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 mapping = platforms[platform];
|
||||
|
||||
@@ -100,7 +77,7 @@
|
||||
Object.entries(mapping).forEach(([dataField, elementSuffix]) => {
|
||||
const element = document.getElementById(`${repoId}-${elementSuffix}`);
|
||||
if (element) {
|
||||
let value = getNested(data, dataField);
|
||||
let value = data[dataField];
|
||||
if (processors[platform]?.[dataField]) {
|
||||
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.
|
||||
|
||||
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**
|
||||
|
||||
```md
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
{{- $type = "collection" -}}
|
||||
{{- end -}}
|
||||
|
||||
{{ $id := delimit (slice "ansible" $type (partial "functions/uid.html" .)) "-" }}
|
||||
{{ $id := delimit (slice "ansible" (partial "functions/uid.html" .)) "-" }}
|
||||
|
||||
{{- $title := "" -}}
|
||||
{{- $description := "" -}}
|
||||
@@ -124,7 +124,7 @@
|
||||
<span class="text-md mr-2 text-neutral-800 dark:text-neutral">
|
||||
{{ partial "icon.html" "download" }}
|
||||
</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 }}
|
||||
</div>
|
||||
|
||||
@@ -132,7 +132,7 @@
|
||||
<span class="text-md mr-2 text-neutral-800 dark:text-neutral">
|
||||
{{ partial "icon.html" "tag" }}
|
||||
</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 }}
|
||||
</div>
|
||||
{{- end }}
|
||||
@@ -158,15 +158,6 @@
|
||||
</div>
|
||||
{{- end }}
|
||||
</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>
|
||||
</div>
|
||||
{{- else if $apiURL -}}
|
||||
|
||||
Reference in New Issue
Block a user