Skip to content

Commit

Permalink
Update url used for querying patchnotes (#178)
Browse files Browse the repository at this point in the history
* Fix patchnote url

* Fix patchnote ordering
  • Loading branch information
TheEpicBlock authored Feb 2, 2024
1 parent 98cf8d7 commit df17795
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/layouts/PatchNotes.astro
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ import Base from "./Base.astro";
</style>

<script is:inline>
const baseUrl = "https://launchercontent.mojang.com";
const v2Url = baseUrl+"/v2";

// Fetch launcher metadata
fetch("https://launchercontent.mojang.com/javaPatchNotes.json")
fetch(v2Url + "/javaPatchNotes.json")
.then((response) => response.json())
.then((data) => {
// Render patch notes
Expand All @@ -109,18 +112,23 @@ import Base from "./Base.astro";
if (entry) {
document.getElementById("title").scrollIntoView();

const body = entry.body
.replaceAll("​", "")
.replaceAll("<p></p>", "");

// Replace title, body and image url with the selected version
document.getElementById("title").innerText = entry.title;
document.getElementById("body").innerHTML = body;

fetch(v2Url + "/" + entry.contentPath)
.then((response) => response.json())
.then((content) => {
const body = content.body
.replaceAll("​", "")
.replaceAll("<p></p>", "");
document.getElementById("body").innerHTML = body;
});

document
.getElementById("image")
.setAttribute(
"src",
"https://launchercontent.mojang.com" + entry.image.url
baseUrl + entry.image.url
);

for (let elem of document.getElementsByClassName(
Expand All @@ -135,22 +143,39 @@ import Base from "./Base.astro";
};

// Create nav-links
data.entries.forEach((entry) => {
const listItem = document.createElement("li");
const newRef = document.createElement("a");

newRef.classList.add("version-item");
newRef.dataset["action"] = "click->sidebar#toggle";
newRef.id = entry.version;
newRef.innerText = entry.version;
newRef.setAttribute(
"href",
window.location.pathname + "#" + entry.version
);

listItem.appendChild(newRef);
document.getElementById("sidebar").appendChild(listItem);
});

// Query meta, because meta contains the versions in their proper order
fetch("https://meta.quiltmc.org/v3/versions/game")
.then((response) => response.json())
.then((meta) => {
const version2index = {};
var i = 0;
meta.forEach((entry) => {
version2index[entry.version] = i++;
});

data.entries.sort((a,b) => version2index[a.version] - version2index[b.version]);
})
.catch(e => console.warn("Failed to sort versions", e))
.finally(() => {
// Insert the (hopefully sorted) entries into the dom
data.entries.forEach((entry) => {
const listItem = document.createElement("li");
const newRef = document.createElement("a");

newRef.classList.add("version-item");
newRef.dataset["action"] = "click->sidebar#toggle";
newRef.id = entry.version;
newRef.innerText = entry.version;
newRef.setAttribute(
"href",
window.location.pathname + "#" + entry.version
);

listItem.appendChild(newRef);
document.getElementById("sidebar").appendChild(listItem);
});
});

// Rerender the patch note when the url changes
window.onhashchange = render;
Expand Down

1 comment on commit df17795

@Cozy-GitHub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See preview on Cloudflare Pages: https://79f73d93.quiltmc-org.pages.dev

Please sign in to comment.