Template
461 lines
15 KiB
JavaScript
461 lines
15 KiB
JavaScript
(() => {
|
|
const OWNER = "You-re-like-Windows-a-bitch";
|
|
const REPO = "ISO";
|
|
const RELEASES_URL = `https://api.github.com/repos/${OWNER}/${REPO}/releases?per_page=5`;
|
|
const TAGS_URL = `https://api.github.com/repos/${OWNER}/${REPO}/tags?per_page=5`;
|
|
const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
|
|
|
|
const currentVersionNode = document.querySelector("[data-current-version]");
|
|
const currentVersionDateNode = document.querySelector("[data-current-version-date]");
|
|
const currentReleaseNoteNode = document.querySelector("[data-current-release-note]");
|
|
const latestDownloadNode = document.querySelector("[data-latest-download]");
|
|
const viewMoreNode = document.querySelector("[data-view-more]");
|
|
const versionsListNode = document.querySelector("[data-versions-list]");
|
|
const toggleNoteNode = document.querySelector("[data-toggle-release-note]");
|
|
|
|
let fullReleaseBodyHtml = "";
|
|
const TRUNCATE_LENGTH = 600;
|
|
|
|
function formatDate(value) {
|
|
if (!value) return "";
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return "";
|
|
return date.toLocaleDateString("fr-FR", {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
});
|
|
}
|
|
|
|
function getReleaseLabel(entry) {
|
|
return entry?.name || entry?.tag_name || entry?.ref || "Sans nom";
|
|
}
|
|
|
|
function getReleasePageUrl(entry, mode) {
|
|
if (mode === "releases") {
|
|
return pickDirectDownloadUrl(entry);
|
|
}
|
|
|
|
if (entry?.name) {
|
|
return `${REPO_URL}/releases/tag/${encodeURIComponent(entry.name)}`;
|
|
}
|
|
|
|
return REPO_URL;
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
return text
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">");
|
|
}
|
|
|
|
function parseInline(text) {
|
|
let result = escapeHtml(text);
|
|
result = result.replace(/\*\*\*(.+?)\*\*\*/g, "<strong><em>$1</em></strong>");
|
|
result = result.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
|
|
result = result.replace(/\*(.+?)\*/g, "<em>$1</em>");
|
|
result = result.replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
result = result.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img alt="$1" src="$2" loading="lazy">');
|
|
result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" rel="noreferrer">$1</a>');
|
|
return result;
|
|
}
|
|
|
|
function renderMarkdown(md) {
|
|
if (!md) return "";
|
|
|
|
var lines = md.split(/\r?\n/);
|
|
var html = "";
|
|
var inCodeBlock = false;
|
|
var codeBlockContent = "";
|
|
var codeBlockLang = "";
|
|
var listBuffer = "";
|
|
var listKind = "";
|
|
var tableRows = [];
|
|
var tableHeader = true;
|
|
var blockquoteBuffer = "";
|
|
|
|
function flushList() {
|
|
if (listBuffer) {
|
|
html += "<" + listKind + ">\n" + listBuffer + "</" + listKind + ">\n";
|
|
listBuffer = "";
|
|
listKind = "";
|
|
}
|
|
}
|
|
|
|
function flushTable() {
|
|
if (tableRows.length) {
|
|
html += "<table>\n" + tableRows.join("\n") + "\n</table>\n";
|
|
tableRows = [];
|
|
tableHeader = true;
|
|
}
|
|
}
|
|
|
|
function flushBlockquote() {
|
|
if (blockquoteBuffer) {
|
|
html += "<blockquote>" + blockquoteBuffer + "</blockquote>\n";
|
|
blockquoteBuffer = "";
|
|
}
|
|
}
|
|
|
|
function parseTableCells(line) {
|
|
var trimmed = line.replace(/^\|/, "").replace(/\|$/, "");
|
|
return trimmed.split("|").map(function (c) { return c.trim(); });
|
|
}
|
|
|
|
function isTableSep(line) {
|
|
return /^\|?[\s|:\-]+\|?$/.test(line) && line.indexOf("-") !== -1;
|
|
}
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
var line = lines[i];
|
|
|
|
if (line.startsWith("```")) {
|
|
flushBlockquote();
|
|
flushList();
|
|
flushTable();
|
|
if (inCodeBlock) {
|
|
var langAttr = codeBlockLang ? " class=\"language-" + escapeHtml(codeBlockLang) + "\"" : "";
|
|
html += "<pre><code" + langAttr + ">" + escapeHtml(codeBlockContent.trim()) + "</code></pre>\n";
|
|
codeBlockContent = "";
|
|
codeBlockLang = "";
|
|
inCodeBlock = false;
|
|
} else {
|
|
inCodeBlock = true;
|
|
codeBlockLang = line.slice(3).trim();
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (inCodeBlock) {
|
|
codeBlockContent += (codeBlockContent ? "\n" : "") + line;
|
|
continue;
|
|
}
|
|
|
|
if (line.trim() === "") {
|
|
flushBlockquote();
|
|
flushList();
|
|
flushTable();
|
|
continue;
|
|
}
|
|
|
|
var hMatch = line.match(/^(#{1,6})\s+(.+)$/);
|
|
if (hMatch) {
|
|
flushBlockquote();
|
|
flushList();
|
|
flushTable();
|
|
var level = hMatch[1].length;
|
|
html += "<h" + level + ">" + parseInline(hMatch[2]) + "</h" + level + ">\n";
|
|
continue;
|
|
}
|
|
|
|
if (/^[-*_]{3,}\s*$/.test(line.trim())) {
|
|
flushBlockquote();
|
|
flushList();
|
|
flushTable();
|
|
html += "<hr>\n";
|
|
continue;
|
|
}
|
|
|
|
if (line.startsWith("> ") || line.startsWith(">")) {
|
|
flushList();
|
|
flushTable();
|
|
var content = line.replace(/^>\s?/, "");
|
|
blockquoteBuffer += "<p>" + parseInline(content) + "</p>";
|
|
continue;
|
|
} else {
|
|
flushBlockquote();
|
|
}
|
|
|
|
if (line.includes("|") && !isTableSep(line)) {
|
|
flushList();
|
|
var cells = parseTableCells(line);
|
|
var tag = tableHeader ? "th" : "td";
|
|
tableRows.push("<tr>" + cells.map(function (c) { return "<" + tag + ">" + parseInline(c) + "</" + tag + ">"; }).join("") + "</tr>");
|
|
if (tableHeader) tableHeader = false;
|
|
continue;
|
|
}
|
|
|
|
if (isTableSep(line)) {
|
|
tableHeader = false;
|
|
continue;
|
|
}
|
|
|
|
if (line.includes("|")) {
|
|
flushTable();
|
|
}
|
|
|
|
var ulMatch = line.match(/^(\s*)[-*+]\s+(.+)$/);
|
|
if (ulMatch) {
|
|
flushTable();
|
|
if (listKind !== "ul") {
|
|
flushList();
|
|
listKind = "ul";
|
|
}
|
|
listBuffer += "<li>" + parseInline(ulMatch[2]) + "</li>\n";
|
|
continue;
|
|
}
|
|
|
|
var olMatch = line.match(/^(\s*)\d+\.\s+(.+)$/);
|
|
if (olMatch) {
|
|
flushTable();
|
|
if (listKind !== "ol") {
|
|
flushList();
|
|
listKind = "ol";
|
|
}
|
|
listBuffer += "<li>" + parseInline(olMatch[2]) + "</li>\n";
|
|
continue;
|
|
}
|
|
|
|
if (listBuffer && /^\s{2,}/.test(line)) {
|
|
listBuffer = listBuffer.replace(/<\/li>\n$/, " " + parseInline(line.trim()) + "</li>\n");
|
|
continue;
|
|
}
|
|
|
|
flushList();
|
|
flushTable();
|
|
html += "<p>" + parseInline(line) + "</p>\n";
|
|
}
|
|
|
|
flushBlockquote();
|
|
flushList();
|
|
flushTable();
|
|
if (inCodeBlock) {
|
|
var langAttr2 = codeBlockLang ? " class=\"language-" + escapeHtml(codeBlockLang) + "\"" : "";
|
|
html += "<pre><code" + langAttr2 + ">" + escapeHtml(codeBlockContent.trim()) + "</code></pre>\n";
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function sanitizeRenderedHtml(rawHtml) {
|
|
const wrapper = document.createElement("div");
|
|
wrapper.innerHTML = rawHtml;
|
|
|
|
wrapper.querySelectorAll("script, iframe, object, embed, link, style").forEach(function (node) { node.remove(); });
|
|
wrapper.querySelectorAll("*").forEach(function (node) {
|
|
Array.from(node.attributes).forEach(function (attribute) {
|
|
var name = attribute.name.toLowerCase();
|
|
var value = attribute.value || "";
|
|
if (name.startsWith("on")) { node.removeAttribute(attribute.name); return; }
|
|
if ((name === "href" || name === "src") && /^javascript:/i.test(value)) { node.removeAttribute(attribute.name); }
|
|
});
|
|
});
|
|
|
|
return wrapper.innerHTML;
|
|
}
|
|
|
|
function truncateHtml(fullHtml) {
|
|
var temp = document.createElement("div");
|
|
temp.innerHTML = fullHtml;
|
|
var text = temp.textContent || "";
|
|
|
|
if (text.length <= TRUNCATE_LENGTH) {
|
|
return { html: fullHtml, truncated: false };
|
|
}
|
|
|
|
var acc = "";
|
|
var count = 0;
|
|
var truncatedHtml = "";
|
|
var children = Array.from(temp.childNodes);
|
|
|
|
for (var ci = 0; ci < children.length; ci++) {
|
|
var child = children[ci];
|
|
var childText = child.textContent || "";
|
|
|
|
if (count + childText.length > TRUNCATE_LENGTH && count > 0) {
|
|
break;
|
|
}
|
|
|
|
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
truncatedHtml += child.outerHTML;
|
|
} else {
|
|
var remaining = TRUNCATE_LENGTH - count;
|
|
if (childText.length > remaining) {
|
|
truncatedHtml += escapeHtml(childText.slice(0, remaining)) + "…";
|
|
count = TRUNCATE_LENGTH;
|
|
break;
|
|
}
|
|
truncatedHtml += escapeHtml(childText);
|
|
}
|
|
count += childText.length;
|
|
}
|
|
|
|
return { html: truncatedHtml, truncated: true };
|
|
}
|
|
|
|
function setupToggle() {
|
|
if (!toggleNoteNode || !currentReleaseNoteNode) return;
|
|
|
|
toggleNoteNode.addEventListener("click", function () {
|
|
var expanded = toggleNoteNode.dataset.expanded === "true";
|
|
if (expanded) {
|
|
var result = truncateHtml(fullReleaseBodyHtml);
|
|
currentReleaseNoteNode.innerHTML = result.html;
|
|
toggleNoteNode.textContent = "Voir plus";
|
|
toggleNoteNode.dataset.expanded = "false";
|
|
} else {
|
|
currentReleaseNoteNode.innerHTML = fullReleaseBodyHtml;
|
|
toggleNoteNode.textContent = "Voir moins";
|
|
toggleNoteNode.dataset.expanded = "true";
|
|
}
|
|
});
|
|
}
|
|
|
|
setupToggle();
|
|
|
|
function pickDirectDownloadUrl(release) {
|
|
const assets = Array.isArray(release?.assets) ? release.assets : [];
|
|
const isoAsset = assets.find((asset) => /\.iso$/i.test(asset?.name || ""));
|
|
const directAsset = isoAsset || assets[0];
|
|
|
|
if (directAsset) {
|
|
return directAsset.browser_download_url || release.html_url || REPO_URL;
|
|
}
|
|
|
|
return release?.zipball_url || release?.html_url || REPO_URL;
|
|
}
|
|
|
|
function renderEmptyState(title, message) {
|
|
if (!versionsListNode) return;
|
|
versionsListNode.innerHTML = `
|
|
<li class="version-item version-item-empty">
|
|
<div class="version-main">
|
|
<strong>${title}</strong>
|
|
<span>${message}</span>
|
|
</div>
|
|
<div class="version-side">
|
|
<a class="button button-secondary" href="${REPO_URL}" target="_blank" rel="noreferrer">Voir le repo</a>
|
|
</div>
|
|
</li>
|
|
`;
|
|
}
|
|
|
|
function renderList(items, mode) {
|
|
if (!versionsListNode) return;
|
|
versionsListNode.innerHTML = "";
|
|
|
|
const visibleItems = items.slice(1, 6);
|
|
|
|
if (!visibleItems.length) {
|
|
if (items.length === 0) {
|
|
renderEmptyState("Aucune release trouvée", "Le dépôt n'expose pas encore de release ou les releases ne sont pas accessibles.");
|
|
} else {
|
|
renderEmptyState(
|
|
mode === "releases" ? "Aucune release précédente trouvée" : "Aucun tag précédent trouvé",
|
|
mode === "releases"
|
|
? "Il n'y a qu'une seule release publiée pour le moment."
|
|
: "Il n'y a qu'un seul tag disponible pour le moment."
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
|
|
visibleItems.forEach((entry) => {
|
|
const li = document.createElement("li");
|
|
li.className = "version-item";
|
|
|
|
const label = getReleaseLabel(entry);
|
|
const downloadUrl = getReleasePageUrl(entry, mode);
|
|
const published = formatDate(entry.published_at || entry.commit?.committer?.date);
|
|
|
|
li.innerHTML = `
|
|
<div class="version-main">
|
|
<strong>${label}</strong>
|
|
<span>${published ? `Publié le ${published}` : mode === "releases" ? "Date inconnue" : "Tag GitHub"}</span>
|
|
</div>
|
|
<div class="version-side">
|
|
<a class="button button-secondary" href="${downloadUrl}" target="_blank" rel="noreferrer">Télécharger</a>
|
|
</div>
|
|
`;
|
|
|
|
versionsListNode.appendChild(li);
|
|
});
|
|
}
|
|
|
|
async function fetchJson(url) {
|
|
const response = await fetch(url, {
|
|
headers: { Accept: "application/vnd.github+json" },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`GitHub API ${response.status}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
async function loadReleases() {
|
|
if (currentVersionNode) currentVersionNode.textContent = "Chargement...";
|
|
if (currentVersionDateNode) currentVersionDateNode.textContent = "";
|
|
if (currentReleaseNoteNode) currentReleaseNoteNode.textContent = "";
|
|
if (latestDownloadNode) latestDownloadNode.href = REPO_URL;
|
|
if (viewMoreNode) viewMoreNode.href = `${REPO_URL}/releases`;
|
|
|
|
try {
|
|
const releases = await fetchJson(RELEASES_URL);
|
|
|
|
if (Array.isArray(releases) && releases.length > 0) {
|
|
const latest = releases[0];
|
|
const latestLabel = getReleaseLabel(latest);
|
|
const latestDownloadUrl = pickDirectDownloadUrl(latest);
|
|
const latestDate = formatDate(latest.published_at);
|
|
|
|
if (currentVersionNode) currentVersionNode.textContent = latestLabel;
|
|
if (currentVersionDateNode) currentVersionDateNode.textContent = latestDate ? ` · ${latestDate}` : "";
|
|
if (currentReleaseNoteNode) {
|
|
fullReleaseBodyHtml = sanitizeRenderedHtml(renderMarkdown(latest.body || ""));
|
|
var result = truncateHtml(fullReleaseBodyHtml);
|
|
currentReleaseNoteNode.innerHTML = result.html || "<p>Release GitHub la plus récente.</p>";
|
|
if (toggleNoteNode) {
|
|
toggleNoteNode.style.display = result.truncated ? "inline-block" : "none";
|
|
toggleNoteNode.dataset.expanded = "false";
|
|
toggleNoteNode.textContent = "Voir plus";
|
|
}
|
|
}
|
|
if (latestDownloadNode) latestDownloadNode.href = latestDownloadUrl;
|
|
|
|
renderList(releases, "releases");
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
console.error("Erreur fetch releases:", error.message);
|
|
// On tente le fallback tags ci-dessous.
|
|
}
|
|
|
|
try {
|
|
const tags = await fetchJson(TAGS_URL);
|
|
|
|
if (Array.isArray(tags) && tags.length > 0) {
|
|
const latestTag = tags[0];
|
|
const latestLabel = getReleaseLabel(latestTag);
|
|
const latestDownloadUrl = `${REPO_URL}/releases/tag/${encodeURIComponent(latestTag.name)}`;
|
|
|
|
if (currentVersionNode) currentVersionNode.textContent = `${latestLabel} (tag)`;
|
|
if (currentVersionDateNode) currentVersionDateNode.textContent = "";
|
|
if (currentReleaseNoteNode) {
|
|
currentReleaseNoteNode.textContent = "Aucune release GitHub publiée pour le moment, affichage des tags du dépôt.";
|
|
}
|
|
if (latestDownloadNode) latestDownloadNode.href = latestDownloadUrl;
|
|
|
|
renderList(tags, "tags");
|
|
return;
|
|
}
|
|
|
|
if (currentVersionNode) currentVersionNode.textContent = "Aucune release trouvée";
|
|
if (currentReleaseNoteNode) {
|
|
currentReleaseNoteNode.textContent = "Le dépôt ne renvoie ni release ni tag public.";
|
|
}
|
|
renderEmptyState("Aucune release trouvée", "Le dépôt ne renvoie ni release ni tag public.");
|
|
} catch (error) {
|
|
console.error("Erreur fetch tags:", error.message);
|
|
if (currentVersionNode) currentVersionNode.textContent = "Impossible de charger les releases";
|
|
if (currentReleaseNoteNode) {
|
|
currentReleaseNoteNode.textContent = `Erreur: ${error.message}. Vérifie que le repo GitHub est public et qu'il expose des releases ou des tags.`;
|
|
}
|
|
renderEmptyState("Impossible de charger les releases", "Ouvre le repo GitHub pour vérifier les releases ou les tags.");
|
|
}
|
|
}
|
|
|
|
loadReleases();
|
|
})(); |