Skip to content

Commit

Permalink
feat: Pull articles from more than one topic search path
Browse files Browse the repository at this point in the history
  • Loading branch information
ozxybox authored and JoLoZ committed Dec 9, 2023
1 parent da1e3cc commit 6344487
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/exporter/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class PageHandler {
const index = {};
const menu = {};

const pageDir: string = '../pages/';

// For each game, for each category, for each topic, render all articles
for (const game of this.games) {
index[game.id] = {
Expand Down Expand Up @@ -60,20 +62,30 @@ export class PageHandler {
for (const topic of category.topics) {
if (!topic.path) topic.path = topic.id;

// Check if index.md exists
const possiblePaths = [`${game.id}/${topic.path}/`, `shared/${topic.path}/`];
// Array of tuples of [directory path, article]
const articles: [string, string][] = [];

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;
// Search all possible paths for articles and build a list of every article we find
const possiblePaths = [`${game.id}/${topic.path}/`, `shared/${topic.path}/`];
for (const searchPath of possiblePaths) {
const path: string = pageDir + searchPath;
if (fs.existsSync(path)) {
console.log(`Searching for articles in ${path}`);
const dirArticles = fs.readdirSync(path, {
withFileTypes: true
});

for (const articleFile of dirArticles) {
// Exclude all directories
if (articleFile.isDirectory()) continue;

// Push the parent and the name
articles.push([path, articleFile.name]);
}
}
}
if (!directoryPath)
throw new Error(`Could not locate directory: ../pages/${game.id}/${topic.path}/`);

if (articles.length === 0) throw new Error(`Could not locate articles: ${game.id}/${topic.path}/`);

index[game.id].categories[category.id].topics[topic.id] = {
id: topic.id,
Expand All @@ -89,23 +101,17 @@ export class PageHandler {
link: `${game.id}/${category.id}/${topic.id}`
});

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

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

const articleList: MenuCategoryItem[] = [];

for (const articleFile of articles) {
// Exclude all directories
if (articleFile.isDirectory()) continue;

const articleString = articleFile.name.replace('.md', '');
// Render article markdown
for (const [articleDir, articleFile] of articles) {
// Get a path to the article and a clean version for the slug
const articlePath = articleDir + articleFile;
const articleString = articleFile.replace('.md', '');

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

Expand All @@ -123,7 +129,7 @@ export class PageHandler {
content: result.content,
title: meta.title || articleString,
slug: result.slug,
file: directoryPath + articleString + '.md',
file: articlePath,
meta: meta
};

Expand Down

0 comments on commit 6344487

Please sign in to comment.