Skip to content

Commit

Permalink
change url structure
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 13, 2024
1 parent d07d1be commit 9f9d01c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 9 additions & 5 deletions docs/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ function copyDirectory(src, dest) {
};


function linkToDay(year, day) {
return `/${year}/day/${day}`;
}

function loadTemplate(templatePath) {
return fs.readFileSync(templatePath, 'utf8');
}
Expand All @@ -93,7 +97,7 @@ function generateYearPicker(year, day, yearToDays) {
let options = '';
for (let y of Object.keys(yearToDays).sort()){
let lastDay = yearToDays[y][yearToDays[y].length-1];
let target = `/${y}/${lastDay}/`
let target = linkToDay(y, lastDay);
options += `<a href="${target}">${y}</a>`
}

Expand All @@ -108,7 +112,7 @@ function generateYearPicker(year, day, yearToDays) {
function generateDayPicker(year, day, yearToDays) {
let res = '';
for (i = 1; i <= yearToDays[year].length; i++) {
const link = `<a href="/${year}/${i}">${i.toString().padStart(2, '0')}</a>`;
const link = `<a href="${linkToDay(year, i)}">${i.toString().padStart(2, '0')}</a>`;
res += i == day ? `<span class="current">${link}</span>` : `<span>${link}</span>`;
}
return res;
Expand All @@ -134,7 +138,7 @@ const lastDay = Math.max(...yearToDays[lastYear]);
copyDirectory('docs/static', 'build');

const filledRedirectTemplate = fillTemplate(redirectTemplate, {
'default-page': `/${lastYear}/${lastDay}/`,
'default-page': linkToDay(lastYear, lastDay),
});

fs.writeFileSync(path.join('build', 'index.html'), filledRedirectTemplate);
Expand All @@ -143,7 +147,7 @@ const currentYear = new Date().getFullYear();
// Iterate over readme.md files and print filled templates
for (const { year, day, name, notes, code, illustration } of findReadmes('.')) {
const filledHtml = fillTemplate(template, {
url: `https://aoc.csokavar.hu/${year}/${day}/`,
url: `https://aoc.csokavar.hu/${linkToDay(year, day)}`,
'problem-id': `${year}/${day}`,
'problem-name': `${name}`,
'year-picker': generateYearPicker(year,day, yearToDays),
Expand All @@ -152,7 +156,7 @@ for (const { year, day, name, notes, code, illustration } of findReadmes('.')) {
notes,
code,
});
const dst = `build/${year}/${day}`;
const dst = `build/${year}/day/${day}`;
fs.mkdirSync(dst, { recursive: true });
fs.writeFileSync(path.join(dst, 'index.html'), filledHtml);
fs.copyFileSync(illustration, path.join(dst, 'illustration.jpeg'));
Expand Down
3 changes: 2 additions & 1 deletion docs/redirect_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<script>
const url = new URL(window.location.href);
if (url.search.match(/^\?(\d{4})\/\d{1,2}/)) {
window.location = url.search.substring(1);
let m = url.search.match(/^\?(\d{4})\/(\d{1,2})/);
window.location = m[1] + "/day/"+m[2];
} else {
window.location = '{{default-page}}';
}
Expand Down

0 comments on commit 9f9d01c

Please sign in to comment.