-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableReplaceCommand.ts
37 lines (34 loc) · 1.26 KB
/
TableReplaceCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import path from "path"
import { RegexReplaceCommand } from "../../RegexReplaceCommand.js"
import { RegexReplacer } from "../../RegexReplacer.js"
import { SsgContext } from "../../../../../SsgContext.js"
import { FileContents } from "../../../../../util/index.js"
/**
* Replaces SSI's `<!-- #include virtual="myFileName" -->` by fileName's contents.
*/
export class SsiIncludeReplaceCommand extends RegexReplaceCommand {
constructor() {
super(/<!--\s*#include\s+virtual="(.+?)"\s*-->/g)
}
protected async createReplacer(context: SsgContext): Promise<RegexReplacer> {
return {
replace: (_match: string, ...args: any[]): string => {
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
}
}
}
}