Skip to content

Replacements

Jérôme Beau edited this page Aug 6, 2023 · 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 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.

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