np.Templating Documentation

website·Indexed 30 days ago

https://nptemplating-docs.netlify.app/

A template language plugin for NotePlan that allows users to insert variables and method results into templates. It includes a suite of built-in modules for dates, time, web APIs, system commands, and note access, as well as support for JavaScript string interpolation, for loops, and frontmatter formatted templates.

Tokens
19.9K
Snippets
141
Records
178
Agent score
72%

What's inside np.Templating

  1. Overview of np.Templating for NotePlan

    np.Templating is a template language plugin for NotePlan that enables the insertion of variables and method results into notes. It allows for the execution of custom JavaScript within templates to create structured, consistent notes for repetitive tasks. The plugin replaces placeholders in template files with actual values and transforms them into NotePlan notes, separating data generation from presentation.
  2. Overview of the Date Module

    The Date Module provides methods and variables for working with dates in np.Templating templates. It uses the JavaScript moment library internally, allowing users to use any standard moment formatting options. Many methods accept an optional pivotDate argument; if omitted, the current date is used as the reference for calculations.
  3. Understand np.Templating basic syntax and terminology

    In np.Templating, a template is a note consisting of static text and commands. Commands (also called placeholders) are text snippets wrapped in opening <% and closing %> tags. These commands invoke variables or methods that return a replacement string.

    There are two types of available objects:

    1. Template Methods: Predefined built-in variables/methods (e.g., date.now).
    2. User Defined Functions: Custom variables or methods provided by other NotePlan plugins (e.g., sweepTasks or listTodaysEvents).

    Syntax Rules:

    • Variables: Do not use parentheses. Example: <% firstName %>.
    • Methods: Must include opening and closing parentheses, which may contain optional parameters. Example: <% date.yesterday() %>.
    First Name: <% firstName %>
    Yesterday: <% date.yesterday() %>
    Tomorrow: <% date.tomorrow("YYYY-MM-DD") %>
  4. Automate note creation with np.Templating

    The np.Templating plugin for NotePlan allows users to automate the input of repetitive information in notes by using templates. It provides an API consisting of built-in variables and methods, as well as the ability to extend functionality through custom plugins created by the user or the community.
  5. Iterate through note paragraphs

    Use note.paragraphs() to retrieve an array of paragraph objects. Each object contains metadata about the line, including:

    • type: Type of paragraph
    • content: Text content
    • rawContent: Content including markdown
    • prefix: First character of rawContent
    • heading: Paragraph heading
    • isRecurring: Boolean for recurring todo items
    • filename: Note filename
    • noteType: 'Calendar' or 'Notes'
    • linkedNoteTitles: Array of linked note titles
    • subItems: Array of subitems
    • referenceBlocks: Array of reference items
    <% const paragraphs = await note.paragraphs() -%>
    <% paragraphs.map(item => { -%>
    <%- item.content %>
    <%}) -%>
  6. Manually migrate templates to np.Templating syntax

    If templates were not migrated automatically during installation, you can run the migration process manually using the np:migrate-templates command. Alternatively, you can manually update template tags: replace the legacy {{ ... }} syntax with the np.Templating tags <%- and %>.
    // Legacy syntax
    {{formattedDateTime({format: '%A, %B %d, %Y'})}}
    
    // np.Templating syntax
    <%- formattedDateTime({format: '%A, %B %d, %Y'}) %>
  7. Use reentrance to reference attributes in templates

    NotePlan processes templates top-down using "reentrance." This allows you to reference the value of a previously defined attribute to calculate or set a subsequent attribute within the Frontmatter, or to use any attribute within the template body.
    ---
    startDate: <%- prompt('startDate','Enter Date') %>
    endDate: <%- date.add(startDate, 7) %>
    formattedDate: <%- startDate %> to <%- endDate %>
    type: empty-note
    ---
    **Date Range:**
    Start: <%- startDate %>
    End: <%- endDate %>
    Full Range: <%- formattedDate %>
  8. Render a template and insert it into a NotePlan note

    To use a template in a NotePlan plugin, use NPTemplating.renderTemplate to generate the content string and Editor.insertTextAtCursor to place that content into the active note (project or calendar note).
    import NPTemplating from "NPTemplating"
    
    const templateData = { /* your data object */ };
    const result = await NPTemplating.renderTemplate('templateName', templateData);
    
    Editor.insertTextAtCursor(result);
  9. Create and organize blog posts in Docusaurus

    Docusaurus blogging features are powered by the @docusaurus/plugin-content-blog plugin. To create blog posts, add Markdown files or folders to the blog directory. To co-locate images with a specific post, use a folder structure (e.g., blog/post-name/index.md).