Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Use some JS to insert API version
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Sep 26, 2023
1 parent f424b41 commit c28ff3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
8 changes: 0 additions & 8 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ Add the following to your `build.gradle` or `pom.xml` file to use the API:

=== ":simple-gradle: Gradle"

--8<-- "api_version.md"

Make sure to replace `{version}` with the version displayed above.

```groovy title="build.gradle"
repositorories {
maven { url = 'https://jitpack.io/' }
Expand All @@ -34,10 +30,6 @@ Add the following to your `build.gradle` or `pom.xml` file to use the API:

=== ":simple-apachemaven: Maven"

--8<-- "api_version.md"

Make sure to replace `{version}` with the version displayed above.

```xml title="pom.xml"
<repositories>
<repository>
Expand Down
37 changes: 33 additions & 4 deletions docs/assets/js/repo-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ document$.subscribe(async () => {
const url = 'https://codeberg-stats-proxy.vercel.app/repo'
const repo_stats = document.querySelector('[data-md-component="source"] .md-source__repository');

function loadInfo(data) {
function loadCodebergInfo(data) {
const facts = document.createElement("ul");
facts.className = "md-source__facts";

Expand All @@ -26,6 +26,26 @@ document$.subscribe(async () => {
repo_stats.appendChild(facts);
}

function loadApiInfo(data) {
const version = data["version"];
const versionToken = '{version}';
const codeBlocks = document.querySelectorAll('.md-content pre code');
for(const codeBlock of codeBlocks) {
codeBlock.innerHTML = codeBlock.innerHTML.replace(new RegExp(versionToken, 'g'), version);
}
}

async function fetchApiInfo() {
const tag = await fetch("https://api.github.com/repos/Andre601/asl-api/releases/latest").then(_ => _.json());

const data = {
"version": tag.tag_name
};

__md_set("__api_tag", data, sessionStorage);
loadApiInfo(data);
}

async function fetchInfo() {
const [release, repo] = await Promise.all([
fetch(`${url}/latest-release`).then(_ => _.json()),
Expand All @@ -39,15 +59,24 @@ document$.subscribe(async () => {
};

__md_set("__git_repo", data, sessionStorage);
loadInfo(data);
loadCodebergInfo(data);
}

if(!document.querySelector('[data-md-component="source"] .md-source__facts')){
if(!document.querySelector('[data-md-component="source"] .md-source__facts')) {
const cached = __md_get("__git_repo", sessionStorage);
if((cached != null) && (cached["version"])) {
loadInfo(cached);
loadCodebergInfo(cached);
} else {
fetchInfo();
}
}

if(location.href.includes('/api/')) {
const cachedApi = __md_get("__api_tag", sessionStorage);
if((cachedApi != null) && (cachedApi["version"])) {
loadApiInfo(cachedApi);
} else {
fetchApiInfo();
}
}
})

0 comments on commit c28ff3a

Please sign in to comment.