@rr0/cms
Version:
RR0 Content Management System (CMS)
35 lines (34 loc) • 1.18 kB
JavaScript
import { SsiEchoVarReplaceCommand } from "ssg-api";
/**
* Replaces the SSI expression "<!--#echo var="title" -->" by the page's <title> content,
* with a link if there's a <meta name="url"> content.
*/
export class SsiTitleReplaceCommand extends SsiEchoVarReplaceCommand {
/**
* @param defaultHandlers Will generate a title for a given context/file, if no title is found.
*/
constructor(defaultHandlers = []) {
super("title");
this.defaultHandlers = defaultHandlers;
}
async createReplacer(context) {
return {
replace: (_match, ..._args) => {
const titleStr = this.getTitle(context);
context.file.title = titleStr;
const titleUrl = context.file.meta.url;
return titleUrl ? `<a href="${titleUrl}" target="_blank">${titleStr}</a>` : titleStr;
}
};
}
getTitle(context) {
let title = context.file.title;
if (!title) {
this.defaultHandlers.some(handle => !title && (title = handle(context)));
}
if (!title) {
title = context.file.name;
}
return title;
}
}