Content Adaptor To Update Existing Markdown Key Value Pairs

I need to calculate a few Frontmatter keys at build time.

Background:

I need to calculate certain frontmatter key values at build time so they can be referenced across multiple pages. For example, I’d like to:

  • Turn a “launched on” frontmatter key into a calculated “product_age.”
  • Set a default image if the frontmatter lacks an “image” field.
  • Define an “is_upcoming” key for events where “event_start_date” is in the future.

Problem:

While I know how to calculate these values using partials, I’m looking for a way to reduce code repetition. Specifically, I want to avoid recalling the same partial multiple times across different pages, other partials, or sections.

Potential Solution 1: Content Adapters

Could this be achieved using content adapters to manipulate the params key? My main concern is whether content adapters can use their own directory as a source, rather than requiring an external resource. I want to avoid consolidating all markdown files into a single JSON data file, as maintaining such a file would be a nightmare given the sheer volume of files.

Potential Solution 2 (Scrappy, but Works):

Another approach, which I’m currently leaning away from due to its “scrappy” nature, involves pushing all markdown files to a pre_build folder. In this folder, I’d use Hugo to build markdown files using transform unmarshal (instead of HTML), and then mount this public directory into the parent site’s Hugo configuration. This method works well for me, but I’m trying to avoid adding an extra build step to my work flow, so seeing if there’s any other approach I can account for.

ps: I understand that these approaches would require routine site rebuilds, and I’ve already factored that into my considerations.

Not commenting on your other challenges, but for the above, see

1 Like

Depending on how often you run Hugo to create your site and what units product_age uses, this might not be the best approach. I’d probably just do something like
<span id="product_age" data-launched="{{.params.launched}}"></span>
in my template and use JavaScript to calculate the product_age as well as to set the span’s innerHTML property to the correct value. That ensures that people always see the current product_age, not then one calculated when you last run Hugo.

Something similar might be possible for your upcoming events.

We did this for the longest time, and using time ago or other plugins on the front end. But the issue is that some of the other post outputs like JSON which required this same data, using Javascript wasn’t an option.

Eg: The JSON needed an “is_concluded” key. So the only solution we had was to rebuild the website whenever a post is to be archived, and having a prebuild step to ensure the values are assigned in front matter. [guide for rebuilding the site] (Scheduling Hugo Builds with AppScript and Node | BusinessAddons.com)

I would say you won’t get lucky with trying to patch in that dynamic content using a contentAdapter. you don’t have access to the pages and would need to get them using os.ReadFile …

at least you have to call these values wherever you need them, so “repetition” cannot be avoided.

{{ .product_age }} <-- what you want

{{ partialCached "product_age" .launched_on }} <-- doesn't look that bad to me

depending on your type of values you may also play with the cache key to speed up things

let’s say your launched_on is just a date without any variants in time,offset or timezone"

{{ partialCached "product_age" .launched_on .launched_on }} 

in a separate build step you could, for frontmatter in YAML or JSON, play with yq - portable yaml processor.


You comment on JSON looks like you have something before and/or after the Hugo step, so maybe reworking that interface could be an option.