Skip to content

Commit

Permalink
refactor: Get rid of the Index and stop passing around Exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ozxybox committed Dec 14, 2023
1 parent 619a17e commit e3610b1
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 179 deletions.
14 changes: 7 additions & 7 deletions src/client/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetaGame, Menu } from '../common/types';
import { MetaGame, Menu, RenderedPage } from '../common/types';
import { Slug } from '../common/slug';
import { clearNotices, notify } from './notices';
import { anchorHeaderFix, addAnchorLinks } from './anchors';
Expand Down Expand Up @@ -94,9 +94,9 @@ async function navigate(slug, replace = false, loadData = true) {
return;
}

const data = await req.json().catch(() => {
const data: RenderedPage = (await req.json().catch(() => {
throw new Error('Error parsing page data');
});
})) as RenderedPage;
document.querySelector('#content').innerHTML = data.content;

console.log('NAV RESULT', data);
Expand Down Expand Up @@ -125,7 +125,7 @@ async function navigate(slug, replace = false, loadData = true) {

if (showExclusiveNotice) {
notify(
`This page contains sections that are irrelevant to your selected game. If you're missing a section, consider <a href="javascript:showGameSelector()">changing your game</a>.`,
`This page contains sections that are irrelevant to your selected game. If you're missing a section, consider <a href="?force=gameselect">changing your game</a>.`,
'eye-off'
);
}
Expand All @@ -150,7 +150,7 @@ async function navigate(slug, replace = false, loadData = true) {

document.querySelector('html').className = 'theme-' + info.game;

document.title = `${data.title || 'Page not found'} - ${games[info.game].name} Wiki`;
document.title = `${data.meta.title || 'Page not found'} - ${games[info.game].name} Wiki`;
document.querySelector<HTMLDivElement>('#current-game').innerText = games[info.game].name;

document.querySelector<HTMLLinkElement>('link[rel=icon]').href = games[info.game].favicon || games[info.game].icon;
Expand All @@ -159,10 +159,10 @@ async function navigate(slug, replace = false, loadData = true) {

document.querySelector<HTMLAnchorElement>('.top-nav .game a').href = `/${info.game}`;

if (loadData || data.file) {
if (loadData || data.path) {
// Update the edit button to reflect our current page
document.querySelector<HTMLAnchorElement>('.edit a').href = `https://github.com/StrataSource/Wiki/edit/main/${
data.file || '404.md'
data.path || '404.md'
}`;
}

Expand Down
30 changes: 6 additions & 24 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,22 @@ export interface MetaGame {
features: string[];
}

export interface Article {
id: string;
content: HTMLString;
name: string;
slug: Slug;
file: string;
meta: PageMeta;
}

export interface PageMeta {
title?: string;
features?: string[];
example?: string;
}

export interface RenderedPage {
path: string;
content: HTMLString;
meta: PageMeta;
}

export interface Index {
[gameID: string]: {
id: string;
categories: {
[categoryID: string]: {
topics: {
[topicID: string]: {
articles: {
[articleID: string]: Article;
};
};
};
};
};
};
export interface Article {
id: string;
slug: Slug;
page: RenderedPage;
}

export interface MenuArticle {
Expand Down
140 changes: 74 additions & 66 deletions src/exporter/export.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
import fs from 'fs-extra';

import { PageHandler } from './pages';
import { Templater } from './template';
import { Renderer } from './render';
import { Slug } from '../common/slug';
import { MetaGame } from '../common/types';
import { MetaGame, Article, HTMLString } from '../common/types';

export class Exporter {
pageHandler: PageHandler;
templater: Templater;
renderer: Renderer;
games: MetaGame[];

constructor() {
this.pageHandler = new PageHandler(this);
this.templater = new Templater();
this.renderer = new Renderer();

// Read the pages folder, anything with a meta.json is a "game"
this.games = fs
.readdirSync('../pages')
.filter((game) => fs.existsSync(`../pages/${game}/meta.json`))
.map(
(game) =>
({
...fs.readJSONSync(`../pages/${game}/meta.json`),
id: game
} as MetaGame)
);

// For each game, register up a handler for its game exclusive block
for (const game of this.games) {
this.renderer.registerGame(game.id, game.nameShort || game.name || game.id);
}

// For each game, cache all articles
console.log('Caching articles...');
for (const game of this.games) {
this.pageHandler.cacheArticles(game.id);
}
this.pageHandler = new PageHandler();
}

export() {
Expand All @@ -52,28 +25,20 @@ export class Exporter {

step();
this.clean();

step();
this.copyAssets();

step();
this.copyResources();

step();
this.templater.generateNav(this.games);
this.findGameMeta();
this.copyGameMeta();

step();
this.pageHandler.buildIndex(this.games);
fs.writeFileSync('public/ajax/menu.json', JSON.stringify(this.pageHandler.menu));

step();
this.saveAllPages();

step();
this.generateSpecialPages();

step();
this.copyGameMeta();

const endTime = performance.now();

console.log('Done!');
Expand All @@ -100,44 +65,87 @@ export class Exporter {
fs.copySync('static', 'public');
}

saveAllPages() {
for (const gameMeta of this.games)
for (const category of Object.values(this.pageHandler.index[gameMeta.id].categories))
for (const topic of Object.values(category.topics))
for (const article of Object.values(topic.articles)) {
this.pageHandler.savePage(gameMeta, article);
}
findGameMeta() {
// Read the pages folder, anything with a meta.json is a "game"
this.games = fs
.readdirSync('../pages')
.filter((game) => fs.existsSync(`../pages/${game}/meta.json`))
.map(
(game) =>
({
...fs.readJSONSync(`../pages/${game}/meta.json`),
id: game
} as MetaGame)
);
}

copyGameMeta() {
// Copy over the game meta data
const games = {};
for (const game of this.games) {
games[game.id] = game;
}

for (const game of this.games) games[game.id] = game;
fs.writeFileSync('public/ajax/games.json', JSON.stringify(games));
}

generateSpecialPages() {
for (const game of this.games) {
const content = this.renderer.renderPage(`../pages/${game.id}/index.md`);
/**
* Saves an article to the right directories
* @param templater The target template
* @param metaGame The game meta data
* @param article The article
*/
savePage(templater: Templater, metaGame: MetaGame, article: Article): void {
const path = article.slug.toString().split('/').slice(0, -1).join('/');

// Write article JSON meta to file
fs.mkdirSync('public/ajax/article/' + path, { recursive: true });
fs.writeFileSync('public/ajax/article/' + path + '/' + article.id + '.json', JSON.stringify(article.page));

// Generate HTML content
const content = templater.applyTemplate({
metaGame: metaGame,
html: article.page.content,
title: article.page.meta.title || article.id,
menuTopics: this.pageHandler.menu[metaGame.id][article.slug.category]
});

// Writing HTML to file
// console.log('public/' + path);
fs.mkdirSync('public/' + path, { recursive: true });
fs.writeFileSync('public/' + path + '/' + article.id + '.html', content);
}

this.pageHandler.savePage(game, {
...content,
saveAllPages() {
// Read template HTML
const templateMain: HTMLString = fs.readFileSync('templates/main.html', 'utf8');
const template404: HTMLString = fs.readFileSync('templates/404.html', 'utf8');

// Create the templater for articles
const templater = new Templater(templateMain);

// Apply templates for all pages
for (const game of this.games) {
// Generate the nav bar links
templater.generateNav(game);

// Save the Home page
const content = this.pageHandler.articleCache[game.id]['index'];
content.meta.title = 'Home';
this.savePage(templater, game, {
id: 'index',
slug: new Slug(game.id),
name: content.meta.title || 'Home',
file: `/pages/${game.id}/index.md`,
id: 'index'
page: content
});

this.pageHandler.savePage(game, {
...content,
slug: new Slug(game.id),
name: 'Home',
file: '',
// Save the 404 page
this.savePage(templater, game, {
id: '404',
content: fs.readFileSync('templates/404.html', 'utf8')
slug: new Slug(game.id),
page: { content: template404, meta: { title: '404' }, path: '' }
});

// Save all articles
for (const article of this.pageHandler.gameArticles[game.id]) {
this.savePage(templater, game, article);
}
}
}
}
Loading

0 comments on commit e3610b1

Please sign in to comment.