Skip to content

Conversation

@riodelphino
Copy link

@riodelphino riodelphino commented Nov 22, 2025

Update content automatically, based on predefined rules.
Toggles enable/disable this function on demand, by :ZkUpdateToggle command.

For example (on save):

  • Auto updating datetime fields in YAML
  • Sort tags in YAML
  • Change tags style from block to flow in YAML

(e.g.) Update datetime field in YAML frontmatter on save

Before:

---
modified : 2025-01-01 00:00:00
---

After (on save):

---
modified : 2025-11-23 12:31:12
---

modified field is automatically updated to current date and time.

options for above:

  ...
  update = {
    enabled = true,
    triggers = {
      on_save = {
        enabled = true,
        event = 'BufWritePre',
        rules = {
          ["Update datetime in YAML (modified as 2025-01-01 00:00:00)"] = {
            scope = "line",
            pattern = "^(modified *: *)(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d)$",
            format = function(captures, line)
              captures[2] = os.date("%Y-%m-%d %H:%M:%S")
              return table.concat(captures)
            end,
            in_yaml = true,
            notebook_paths = {},
            dirs = {},
          },
        },
      },
    },
  },
  ...
  • in_yaml:
    true = only search in YAML
    false = only search in content
  • dirs:
    valid directories (relative to notebook root). {} allows all directories.
  • notebook_paths:
    valid notebook_paths. {} allows all notebook_paths.

Command

:ZkUpdateToggle toggles enabled.
:ZkUpdateToggle "on_save" toggles only on_save trigger.

Issues

  • Slow down on saving a long file.

Notes

Cannot use zk.api.list or lyaml to get YAML information for the following reasons.

  • BufWritePre requires sync process. (BufWriteCmd may not ?)
  • The last change has not yet been caught by zk.index or lyaml.
  • Ensure to keep the order of tags in YAML. (They break the order.)

Please give me feedback, also whether this PR should be another plugin or included in zk-nvim.

@riodelphino riodelphino changed the title feat: Update content automatically feat: Update content automatically (e.g. datetime field) Nov 22, 2025
@riodelphino riodelphino changed the title feat: Update content automatically (e.g. datetime field) feat: Update content automatically (e.g. datetime fields on save) Nov 22, 2025
@riodelphino
Copy link
Author

riodelphino commented Nov 24, 2025

Somehow zk.txt and README.md were contaminated from my main branch (which merged all my un-merged PR).
So I cleaned up these unintended lines in latest three commits.

@riodelphino
Copy link
Author

riodelphino commented Nov 24, 2025

Added Example Rules

  • Sort tags (on save)
  • Change tags style from block to flow (on save)

@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity.

@github-actions github-actions bot added the stale label Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant