-
Notifications
You must be signed in to change notification settings - Fork 0
Replacements
Jérôme Beau edited this page Nov 11, 2022
·
11 revisions
ContentStep replacements are classes implementing the ReplaceCommand interface,
so you can define your own replacements.
A number predefined replace commands are also available:
- Abstract classes 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
.htaccessfile.
- Concrete classes specialize those techniques to achieve concrete goals, such as:
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).- SsiLastModifiedReplaceCommand replaces a SSI last-modified directive with the input file's last modified date.
- SsiIncludeReplaceCommand replaces a SSI #include directive with the specified 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
and others in the repository (you'll find a number of SSI ones because RR0 used to rely on them).
- 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.
}
}
]
new Ssg(config)
.add(new ContentStep(contentConfigs, outputFunc))
.start(context) // Start the generation
.then(result => console.log("Completed", result))
.catch(err => console.error(err, context.inputFile.name, "=>", context.outputFile.name))