Skip to content

Commit

Permalink
feat: Add support for nested topics
Browse files Browse the repository at this point in the history
  • Loading branch information
ozxybox committed Sep 24, 2023
1 parent 1370996 commit a5a831c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 54 deletions.
49 changes: 8 additions & 41 deletions src/common/slug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'node:fs';

/**
* ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⡀⠀⣿⣿⠆
* ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡿⠇⠀⣿⠁⠀
Expand All @@ -19,52 +17,21 @@ export class Slug {
readonly topic?: string;
readonly article?: string;

readonly path: string;

constructor(slug: string) {
if (slug.toLowerCase().startsWith('ajax') || slug.toLowerCase().startsWith('assets'))
constructor(game: string, category?: string, topic?: string, article?: string) {
if (game.toLowerCase().startsWith('ajax') || game.toLowerCase().startsWith('assets'))
throw new Error(
`Page names starting with "ajax" or "assets" are not permitted! Trying to render page ${slug}.`
`Page names starting with "ajax" or "assets" are not permitted! Trying to render page ${game}/${category}/${topic}/${article}.`
);

const [game, category, topic, article] = slug.split('/');

if (game) {
this.game = game;
if (category && category !== 'index') {
this.category = category;
if (topic && topic !== 'index') {
this.topic = topic;
this.article = article ?? 'index';
} else this.topic = 'index';
} else this.category = 'index';
} else this.game = 'index';

const possiblePaths = [
`${this.game}/${this.topic}/${this.article}.md`,
`shared/${this.topic}/${this.article}.md`,
`${this.game}/index.md`,
`shared/index.md`
];

let found = false;
for (const path of possiblePaths) {
console.log('Checking file path', '../pages/' + path);
if (fs.existsSync('../pages/' + path)) {
this.path = '../pages/' + path;
console.log('Found file at path', path);
found = true;
break;
}
}
if (!found) throw new Error('Could not locate Markdown file for slug: ' + this.toString());
if (game) this.game = game;
if (category) this.category = category;
if (topic) this.topic = topic;
this.article = article ?? 'index';
}

toString() {
let str = this.game;
for (const property of [this.category, this.topic, this.article])
if (property) str += '/' + property;
else break;
for (const property of [this.category, this.topic, this.article]) if (property) str += '/' + property;
return str;
}
}
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type MarkdownString = string;

export interface Topic {
id: string;
path: string;
name: string;
}

Expand Down
2 changes: 1 addition & 1 deletion src/exporter/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Exporter {

generateSpecialPages() {
for (const game of this.pageHandler.games) {
const content = this.renderer.renderPage(new Slug(`${game.id}/index`));
const content = this.renderer.renderPage(`../pages/${game.id}/index.md`, new Slug(game.id));

this.pageHandler.savePage({
...content,
Expand Down
32 changes: 24 additions & 8 deletions src/exporter/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@ export class PageHandler {
const menuCategory: MenuCategoryItem[] = [];

for (const topic of category.topics) {
if (!topic.path) topic.path = topic.id;

// Check if index.md exists
const directoryPath = new Slug(`${game.id}/${category.id}/${topic.id}`).path.replace(
'index.md',
''
);
const possiblePaths = [`${game.id}/${topic.path}/`, `shared/${topic.path}/`];

let directoryPath = null;
for (const path of possiblePaths) {
console.log('Checking file path', '../pages/' + path);
if (fs.existsSync('../pages/' + path)) {
directoryPath = '../pages/' + path;
console.log('Found file at path', path);
break;
}
}
if (!directoryPath) throw new Error('Could not locate Markdown file for slug: ' + this.toString());

index[game.id].categories[category.id].topics[topic.id] = {
id: topic.id,
Expand All @@ -80,16 +90,22 @@ export class PageHandler {

console.log('Reading directory', directoryPath);

const articles = fs.readdirSync(directoryPath);
const articles = fs.readdirSync(directoryPath, {
withFileTypes: true
});

const articleList: MenuCategoryItem[] = [];

for (let articleString of articles) {
articleString = articleString.replace('.md', '');
for (const articleFile of articles) {
// Exclude all directories
if (articleFile.isDirectory()) continue;

const articleString = articleFile.name.replace('.md', '');

// Render out the markdown
const result = this.exporter.renderer.renderPage(
new Slug(`${game.id}/${category.id}/${topic.id}/${articleString}`)
directoryPath + articleFile.name,
new Slug(game.id, category.id, topic.id, articleString)
);

// Make sure the current game's feature set allows this article
Expand Down
9 changes: 5 additions & 4 deletions src/exporter/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ export class Renderer {

/**
* Renders Markdown into a page based on the slug
* @param {string} slug The slug to the page
* @param {string} path The path to the markdown file
* @param {Slug} slug The slug to the page
* @returns Rendered HTML of the page
*/
renderPage(slug: Slug): RenderedPage {
console.log('Rendering file', slug.path, '->', slug.toString());
renderPage(path: string, slug: Slug): RenderedPage {
console.log('Rendering file', path, '->', slug.toString());

return this.render(
fs.readFileSync(slug.path, {
fs.readFileSync(path, {
encoding: 'utf8'
}),
slug
Expand Down

0 comments on commit a5a831c

Please sign in to comment.