Skip to main content

Contributing

You can contribute to Templater by developing a new internal function / variable.

The process to develop a new one is really easy.

Keep in mind that only pertinent submissions will be accepted, don't submit a very specific internal variable / function that you'll be the only one using.

Layout#

Internal variables / functions are sorted by modules. Each module has a dedicated folder under src/InternalTemplates.

Let's take the date module as an example.

It contains an InternalModuleDate file where all our internal date's related variables and functions are defined and registered:

export class InternalModuleDate extends InternalModule {
name = "date";
async createStaticTemplates() {
this.static_templates.set("now", this.generate_now());
this.static_templates.set("tomorrow", this.generate_tomorrow());
this.static_templates.set("yesterday", this.generate_yesterday());
}
async updateTemplates() {}
generate_now() {
return (format: string = "YYYY-MM-DD", offset?: number, reference?: string, reference_format?: string) => {
if (reference && !window.moment(reference, reference_format).isValid()) {
throw new Error("Invalid title date format, try specifying one with the argument 'reference'");
}
return get_date_string(format, offset, reference, reference_format);
}
}
generate_tomorrow() {
return (format: string = "YYYY-MM-DD") => {
return get_date_string(format, 1);
}
}
generate_yesterday() {
return (format: string = "YYYY-MM-DD") => {
return get_date_string(format, -1);
}
}
}

Every module extends the InternalModule abstract class, which means they contain the following attributes and methods:

  • this.app attribute: the obsidian API App object.
  • this.file attribute: The destination file where the template will be inserted.
  • this.plugin attribute: The Templater plugin object.
  • this.static_templates attribute: A map containing all (name; variable/function) that are static. A static variable / function means that it doesn't depend on the file when executed. These type of variables / functions won't be updated each time we insert a new template, to save some overhead.
  • this.dynamic_templates attribute: Same as static_templates except that it contains variables / functions dependent on the file when executed.
  • this.createStaticTemplates() method: Registers all static internal variable / function for that module.
  • this.updateTemplates() method: Registers every dynamic internal variable / function for that module.

You can use these attributes in your new internal variable / function if you need them.

Registering a new internal variable / function#

Here are the different steps you need to follow, in order to register a new internal variable / function in a module.

1st step: Create a method inside the module called generate_<internal_variable_or_function_name>() that will generate your internal variable / function, that means it will return either a lambda function (representing the internal function) or directly the internal variable you want to expose.

All generation methods are ordered by alphabetical order based on the internal variable / function name.

Try to give a good, self-explanatory name for your variable / function.

2nd step: Register your internal variable / function in the static_templates or dynamic_templates map depending on whether your internal variable / function on the file when executed. The registration happens either in createStaticTemplates or updateTemplates.

To register your variable / function, use your this.generate_<internal_variable_or_function_name>() method you defined earlier:

this.static_templates.set(<internal_variable_or_function_name>, this.generate_<internal_variable_or_function_name>());
OR
this.dynamic_templates.set(<internal_variable_or_function_name>, this.generate_<internal_variable_or_function_name>());

Internal variable / function registrations are also ordered by alphabetical order based on the variable / function name.

3rd step: Add your internal variable / function documentation to Templater's documentation.

And you are done ! Thanks for contributing to Templater !

Now, just submit a pull request on Github, I'll try to be as reactive as possible.