First version of webring with simple landing page and control functions.
This commit is contained in:
commit
554960fbef
22 changed files with 3621 additions and 0 deletions
15
src/render_templates.ts
Normal file
15
src/render_templates.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { WebringData, WebringEntry } from './webring';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
function generateEntryHTML(entry: WebringEntry) {
|
||||
return `<div><a href="${entry.url}">${entry.title}</a><span>by ${entry.author}</span></div>`;
|
||||
}
|
||||
|
||||
let index = readFileSync(
|
||||
join(import.meta.dirname, '../templates/index.html'),
|
||||
'utf8'
|
||||
);
|
||||
const renderedEntries = WebringData.map(generateEntryHTML).join('\n');
|
||||
index = index.replace('{{WEBRING_ENTRIES}}', renderedEntries);
|
||||
writeFileSync(join(import.meta.dirname, '../public/index.html'), index);
|
6
src/tsconfig.json
Normal file
6
src/tsconfig.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": ["node"]
|
||||
}
|
||||
}
|
43
src/webring.ts
Normal file
43
src/webring.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import WebringData from '../webring_data.json';
|
||||
export { WebringData };
|
||||
|
||||
export type WebringEntry = (typeof WebringData)[number];
|
||||
|
||||
export function randomEntry() {
|
||||
const selectedIndex = Math.floor(Math.random() * WebringData.length);
|
||||
return WebringData[selectedIndex];
|
||||
}
|
||||
|
||||
export function previous(id: string) {
|
||||
id = id.toLowerCase();
|
||||
|
||||
if (id === 'main') {
|
||||
return WebringData[WebringData.length - 1];
|
||||
}
|
||||
|
||||
let itemIndex = WebringData.findIndex((e) => e.id.toLowerCase() === id);
|
||||
|
||||
if (itemIndex === -1) {
|
||||
return randomEntry();
|
||||
}
|
||||
|
||||
itemIndex += WebringData.length - 1; // `length` is added to force the index to wrap around in case it goes negative. The `- 1` is what makes us go to the previous entry.
|
||||
return WebringData[itemIndex % WebringData.length];
|
||||
}
|
||||
|
||||
export function next(id: string) {
|
||||
id = id.toLowerCase();
|
||||
|
||||
if (id === 'main') {
|
||||
return WebringData[0];
|
||||
}
|
||||
|
||||
let itemIndex = WebringData.findIndex((e) => e.id.toLowerCase() === id);
|
||||
|
||||
if (itemIndex === -1) {
|
||||
return randomEntry();
|
||||
}
|
||||
|
||||
itemIndex += 1;
|
||||
return WebringData[itemIndex % WebringData.length];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue