@rr0/cms
Version:
RR0 Content Management System (CMS)
82 lines (81 loc) • 2.84 kB
JavaScript
import { DirectoryStep } from "ssg-api";
import { StringUtil } from "./util/index.js";
/**
* Builds a directory index.
*/
export class IndexDirectoryStep extends DirectoryStep {
/**
*
* @param rootDirs The directories where UFO cases info can be found.
* @param excludedDirs The directories to exclude from the UFO case directory search.
* @param templateFileName The template of the directory page to build.
* @param outputFunc
* @param config
*/
constructor(rootDirs, excludedDirs, templateFileName, outputFunc, config) {
super({ rootDirs, excludedDirs, templateFileName, getOutputPath: config.getOutputPath }, "directory index");
this.outputFunc = outputFunc;
}
/**
* Convert an array of Case[] to an <ul> HTML unordered list.
*
* @param context
* @param cases
*/
toList(context, cases) {
const listItems = cases.map(dirCase => {
if (!dirCase.title) {
const lastSlash = dirCase.dirName.lastIndexOf("/");
const lastDir = dirCase.dirName.substring(lastSlash + 1);
dirCase.title = StringUtil.camelToText(lastDir);
}
return this.toListItem(context, dirCase);
});
const ul = context.file.document.createElement("ul");
ul.append(...listItems);
ul.className = "links";
return ul;
}
/**
* Convert a Case object to an HTML list item.
*
* @param context
* @param dirCase
*/
toListItem(context, dirCase) {
const item = context.file.document.createElement("li");
const ref = document.createElement("a");
ref.href = dirCase.dirName + "/";
item.appendChild(ref);
return item;
}
async processDirs(context, dirNames) {
const cases = await this.scan(context, dirNames);
const ul = this.toList(context, cases);
const config = this.config;
const outputPath = config.getOutputPath(context);
const output = context.newOutput(outputPath);
output.contents = context.file.contents.replace(`<!--#echo var="directories" -->`, ul.outerHTML);
await this.outputFunc(context, output);
}
/**
* Read case JSON files contents and instantiate them as Case objects.
*
* @param context
* @param dirNames The directories to look for case.json files.
*/
async scan(context, dirNames) {
const cases = [];
for (const dirName of dirNames) {
try {
const dirCases = dirName;
cases.push(...dirCases);
}
catch (e) {
context.warn(`${dirName} has no case.json description`);
// No json, just guess title.
}
}
return cases;
}
}