Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(templates): provide note to custom variable substitution functions #709

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cristobalgvera
Copy link

@cristobalgvera cristobalgvera commented Sep 11, 2024

Descrition

Provide the Note related with the template to the custom variable substitution function. This helps to define custom variables that require Note data.

Examples

Uppercase the note ID

-- ...
templates = {
  substitutions = {
    ---@param note obsidian.Note
    uppercase_id = function(note)
      return note.id:upper()
    end,
  }
}
-- ...

Set a custom title based on the note ID

-- ...
templates = {
  substitutions = {
    ---Format `id` field of `Note` to a more human-readable string.
    ---For an `id` "17823674-My-note-title", it will returns "My note title"
    ---@param note obsidian.Note
    normalized_title = function(note)
        return note.id:gsub("%d+-?", ""):gsub("-", " ")
    end,
  }
}
-- ...

This will make the template variables more flexible, e.g.:

<!-- This template -->
# {{normalized_title}}

<!-- ...will be replaced by -->
# My note title
<!-- Instead of this template -->
# {{id}}

<!-- ...that will be replaced to -->
# 17823674-My-note-title

Changes

  • Provide Note when executing substitution functions.
  • Update test related with the substitution functions.
  • Add change to CHANGELOG.md.

@cristobalgvera cristobalgvera force-pushed the feature/provide-note-in-substitutions branch 2 times, most recently from 97130e6 to 3116126 Compare September 11, 2024 02:51
@cristobalgvera cristobalgvera force-pushed the feature/provide-note-in-substitutions branch from 3116126 to 11186ba Compare September 11, 2024 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant