cache the release notes in the browser session (#2673)

# Description

Please provide a summary of the changes, including relevant motivation
and context.

Closes
https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/164

## Checklist

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have performed a self-review of my own code
- [ ] I have attached images of the change if it is UI based
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] If my code has heavily changed functionality I have updated
relevant docs on [Stirling-PDFs doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
- [x] My changes generate no new warnings
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)
This commit is contained in:
Ludy 2025-01-12 14:46:46 +01:00 committed by GitHub
parent d2d8e2ef42
commit c17b4e29ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -274,9 +274,31 @@
function formatText(text) { function formatText(text) {
const container = document.createElement('div'); const container = document.createElement('div');
if (!text || typeof text !== 'string') {
console.error('Invalid input to formatText:', text);
return container;
}
let textWithoutComments;
try {
textWithoutComments = text.replace(
/<!-- Release notes generated using configuration in .github\/release\.yml at main -->/,
''
);
} catch (error) {
console.error('Error in replace operation:', error);
return container;
}
// Split the text into lines // Split the text into lines
const textWithoutComments = text.replace(/<!--[\s\S]*?-->/g, ''); let lines;
const lines = textWithoutComments.split('\n'); try {
lines = textWithoutComments.split('\n');
} catch (error) {
console.error('Error in split operation:', error);
return container;
}
let currentList = null; let currentList = null;
lines.forEach(line => { lines.forEach(line => {
@ -390,10 +412,26 @@ async function loadReleases() {
while (container.firstChild) { while (container.firstChild) {
container.removeChild(container.firstChild); container.removeChild(container.firstChild);
} }
const cachedReleases = sessionStorage.getItem('releases');
let releases;
if (cachedReleases) {
releases = JSON.parse(cachedReleases);
console.log("Read from storage");
} else {
const response = await fetch(GITHUB_API + '/releases'); const response = await fetch(GITHUB_API + '/releases');
if (!response.ok) throw new Error('Failed to fetch releases'); if (!response.ok) {
const releases = await response.json(); if (response.status === 403) {
throw new Error('API rate limit exceeded');
}
if (response.status === 404) {
throw new Error('Repository not found');
}
throw new Error('Failed to fetch releases');
}
releases = await response.json();
sessionStorage.setItem('releases', JSON.stringify(releases));
}
// Sort releases by version number (descending) // Sort releases by version number (descending)
releases.sort((a, b) => compareVersions(b.tag_name, a.tag_name)); releases.sort((a, b) => compareVersions(b.tag_name, a.tag_name));