-
Notifications
You must be signed in to change notification settings - Fork 0
Replacements
ContentStep replacements are commands that are executed on the contents of a file.
A number predefined replacements commands are available:
- SsiLastModifiedReplaceCommand replaces a SSI last-modified directive with the input file's last modified date.
- IncludeReplacement to replace some content pattern with some file contents.
- SsiIfReplaceCommand replaces a SSI #if directive with some content or another, depending on the truthyness of the expression.
-
StringEchoVarReplaceCommand replaces variables expressed as template literals (
${varName}) in the input files. -
SsiSetVarCommand sets a context variable from the template (which can be checked by the
SsiIfReplaceCommandor displayed by theSsiEchoVarCommand). - SsiEchoVarCommand
HtmlTagReplaceCommand replaces HTML tags using Regexes (use DOM version instead if you have inner tags).ClassRegexReplaceCommand replaces HTML tags bearing a specific class using Regexes (use DOM version instead if you have inner tags).
and others in the repository (you'll find a number of SSI commands because RR0 used to rely on them).
To use such replacement commands, just provide instances of them to your ContentStep. For instance:
const contentConfigs: ContentStepConfig[] = [
{ // A content config that converts .htaccess to netlify.toml format
roots: [".htaccess"],
replacements: [new HtAccessToNetlifyConfigReplaceCommand("https://rr0.org/")],
getOutputFile(context: SsgContext): SsgFile {
return SsgFile.readOrNew(context, "netlify.toml", "utf-8")
}
},
{ // A content config that replaces parts in roots
roots: ["index.html", "404.html", "pages/**/*.html"],
replacements: [
new SsiIncludeReplaceCommand(),
new TitleReplaceCommand(),
new StringEchoVarReplaceCommand("mail"),
new AngularExpressionReplaceCommand(),
new SsiEchoVarReplaceCommand("copyright"),
new SsiIfReplaceCommand(),
new SsiSetVarReplaceCommand("title", (match: string, ...args: any[]) => `<title>${args[0]}</title>`),
new SsiLastModifiedReplaceCommand(context.time.options),
new AuthorReplaceCommand(),
new HtmlTagReplaceCommand("time", new MyTimeReplacerFactory()),
new ClassRegexReplaceCommand("people", new MyPeopleClassReplacerFactory()),
new ClassRegexReplaceCommand("part(.?)", new MyPartXReplacerFactory()),
new LinkReplaceCommand(),
new AnchorReplaceCommand("https://my.base.url/")
],
getOutputFile(context: SsgContext): SsgFile {
return context.inputFile // Under output root, I don't want to change the file path.
}
}
]Those commands will be executed sequentially (so that each replacement can build upon previous replacements) on the contents of each file handled by the ContentStep.
Since a command is just a class implementing the ReplaceCommand interface,
so you can implement your own.
To ease such implementation, your can extends existing abstract command classes which mutualize the basics of different replacement techniques:
- RegexReplaceCommand runs a RegexReplacer as long it changes the contents of the current file.
- DomReplaceCommand runs a DomReplacer on all nodes matched by a DOM selector.
- HtAccessReplaceCommand replaces sections of an .htaccess file.