@rr0/cms
Version:
RR0 Content Management System (CMS)
33 lines (32 loc) • 1.26 kB
JavaScript
import path from "path";
import { RegexReplaceCommand } from "ssg-api";
import { FileContents } from "@javarome/fileutil";
/**
* Replaces SSI's `<!-- #include virtual="myFileName" -->` by fileName's contents.
*/
export class SsiIncludeReplaceCommand extends RegexReplaceCommand {
constructor() {
super(/<!--\s*#include\s+virtual="(.+?)"\s*-->/g);
}
async createReplacer(context) {
return {
replace: (_match, ...args) => {
let currentDir = process.cwd();
const toInclude = args[0];
if (!toInclude.startsWith("/")) {
const currentFile = context.file;
if (currentFile) {
const currentFileName = currentFile.name;
const lastSlash = currentFileName.lastIndexOf("/");
if (lastSlash) {
currentDir = path.join(process.cwd(), currentFileName.substring(0, lastSlash));
}
}
}
const fileName = path.join(currentDir, toInclude);
const replacement = FileContents.read(fileName);
return replacement.contents;
}
};
}
}