Skip to content

Replacements

javarome edited this page Jul 30, 2024 · 11 revisions

ContentStep replacements are commands that are executed on the contents of a file.

A number predefined replacements commands are available:

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 a configuration of your ContentStep. Your need a ContentConfig for each set of files your want apply the same replacements to. So you'll probably only need only one, but here is a general example of using 2 content step configs:

const contentStepConfig1 = {  // First content config is about writing a netlify.toml file which as generated from the contents of an .htaccess file
  roots: [".htaccess"],
  replacements: [new HtAccessToNetlifyConfigReplaceCommand("https://rr0.org/")],
  getOutputPath(context: SsgContext): string {
    return "out/netlify.toml"
  }
}

const contentStepConfig2 = {  // Second content config is about issuing a series of replacements on all HTML pages + index + 404 file
  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/")
  ],
  getOutputPath(context: SsgContext): string {
    return "out/" + context.file.name  // Under output root, I don't want to change the file path.
  }
}

// All the configurations for my contents step
const contentConfigs: ContentStepConfig[] = [contentStepConfig2, contentStepConfig2]

const myContentStep = new ContentStep(contentConfigs, outputFunc);

// The generation itself
new Ssg(config)
  .add(myContentStep) // The only step in this example is content replacements
  .start(context)     // Start the generation
  .then(result => console.log("Completed", result))
  .catch(err => console.error(err, context.file.name))

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.

Custom

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.

Clone this wiki locally