How to create custom template tag or similar

I’m trying to create a plugin that inserts some content into my daily notes. I’d like to have a {{custom tag}} replaced on creation, or similar. Is that possible?

1 Like

Moved from Help to Developers & API.

I’m just barely getting started so something easier may exist, but it sounds like you want to implement a basic parser.

You will want to do some more research but I will quickly explain (I am on mobile)

Let’s say your plugin is just a function right now. It will receive a chunk of text and iterate over each character in the string, looking for your specified opening delimiter “{{“. When it is found, it makes note of the index of the first character in the delimiter within the text.

From there, iterate until you discover the next closing delimiter and make note of the last delimiter characters index.

Finally, substring the text with those indices. Maybe you can parse out a path to a file and inject that — this is how Hugo implemented “Shortcodes”. Apologies if this isn’t what you wanted, please let me know if I can help further.

EDIT:

I forgot to mention, I’d maybe perform the above steps in a loop to capture all find/replace operations. Make a function that returns “true” if your opening and closing delimiters are found and an object with the starting/closing delimiter indices, or “false” if they are not found.

If “true” was returned, pass the indices to another function that actually does the parsing and replaces the text. The loop will then begin again, this time with the new chunk of text resulted from the previous loop. You can make it more efficient but just an idea — I just woke up.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.