Proof of concept: Custom functions #11085
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I got the software equivalent of an earworm and decided to turn it into a proof of concept.
👀 I'm seeking feedback of any kind, which could include "this isn't something Hugo is interested in"; I'll be happy to turn this sorry state of affairs into quality, tested code, or to close this line of thinking down.
What is it?
Code similar to this would allow Hugo users to write and use custom Typescript (or Javascript) functions for use inside their templates.
See the
external.md
at the top of the PR for more detail.Can I test it out?
Yes!
layouts/index.html
andfunctions/*.ts
../path/to/built/hugo
in the site rootpublic/index.html
to see the outputWhy is this useful?
A lot of Hugo's Github Issues seem to be about changing or adding new functions to Hugo. Though contributing to Hugo's codebase is an admirable, it's a long-term solution to these requests and likely isn't available option to many.
Writing and sharing function files, storing them in the site files (eg. in
functions/*.ts
), is a cross-platform, simple, accessible way to be able to write and share helper functions.Some important benefits:
fetch
, but there are lots of new challenges there.import
-able*, so custom code writers can build on the shoulders of the JS community. (*Caveat: I haven't tried this, it'd likely be a little complex right now)Why this PR isn't at all ready
I've thrown this together to understand if it's interesting to the community. There are lots of sharp edges. Some of the ones I've found/considered but not addressed:
{{ fn "filename.FunctionName" "args" }}
instead of{{ filename.FunctionName "args" }}
), as Hugo seems to require namespaced functions to be methods in Go and, because these are dynamically generated from JS, I can't do that (without substantial codechange)<FunctionName>Examples
, expecting it to be in the[][2]string
format used for other function examples, is useless right now.Thanks for reading this far! Let me know what you think.