(() => { 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, ">"); } function parseInline(text) { let result = escapeHtml(text); result = result.replace(/\*\*\*(.+?)\*\*\*/g, "$1"); result = result.replace(/\*\*(.+?)\*\*/g, "$1"); result = result.replace(/\*(.+?)\*/g, "$1"); result = result.replace(/`([^`]+)`/g, "$1"); result = result.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '$1'); result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1'); 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 + "\n"; listBuffer = ""; listKind = ""; } } function flushTable() { if (tableRows.length) { html += "\n" + tableRows.join("\n") + "\n
\n"; tableRows = []; tableHeader = true; } } function flushBlockquote() { if (blockquoteBuffer) { html += "
" + blockquoteBuffer + "
\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 += "
" + escapeHtml(codeBlockContent.trim()) + "
\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 += "" + parseInline(hMatch[2]) + "\n"; continue; } if (/^[-*_]{3,}\s*$/.test(line.trim())) { flushBlockquote(); flushList(); flushTable(); html += "
\n"; continue; } if (line.startsWith("> ") || line.startsWith(">")) { flushList(); flushTable(); var content = line.replace(/^>\s?/, ""); blockquoteBuffer += "

" + parseInline(content) + "

"; continue; } else { flushBlockquote(); } if (line.includes("|") && !isTableSep(line)) { flushList(); var cells = parseTableCells(line); var tag = tableHeader ? "th" : "td"; tableRows.push("" + cells.map(function (c) { return "<" + tag + ">" + parseInline(c) + ""; }).join("") + ""); 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 += "
  • " + parseInline(ulMatch[2]) + "
  • \n"; continue; } var olMatch = line.match(/^(\s*)\d+\.\s+(.+)$/); if (olMatch) { flushTable(); if (listKind !== "ol") { flushList(); listKind = "ol"; } listBuffer += "
  • " + parseInline(olMatch[2]) + "
  • \n"; continue; } if (listBuffer && /^\s{2,}/.test(line)) { listBuffer = listBuffer.replace(/<\/li>\n$/, " " + parseInline(line.trim()) + "\n"); continue; } flushList(); flushTable(); html += "

    " + parseInline(line) + "

    \n"; } flushBlockquote(); flushList(); flushTable(); if (inCodeBlock) { var langAttr2 = codeBlockLang ? " class=\"language-" + escapeHtml(codeBlockLang) + "\"" : ""; html += "
    " + escapeHtml(codeBlockContent.trim()) + "
    \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 = `
  • ${title} ${message}
    Voir le repo
  • `; } 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 = `
    ${label} ${published ? `Publié le ${published}` : mode === "releases" ? "Date inconnue" : "Tag GitHub"}
    Télécharger
    `; 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 || "

    Release GitHub la plus récente.

    "; 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(); })();