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

Work in progress: Externalized page metadata #5455

Closed
bep opened this issue Nov 20, 2018 · 10 comments
Closed

Work in progress: Externalized page metadata #5455

bep opened this issue Nov 20, 2018 · 10 comments

Comments

@bep
Copy link
Member

bep commented Nov 20, 2018

Also see #5510 -- a concurrent batch variant (whatever that would look like) could also be considered.

This issue describes a way to add or modify Page metadata in batches from external file(s). This is the information you currently only set in Page front matter.

Some use cases:

  • The default page sort is ByWeight (with fall back to date and then title). Setting weight in front matter to get a particular order can be cumbersome if you have thousands of articles. With this implemented, you can re-arrange the pages by assigning weight in batches by Glob pattern matching.
  • Tag all pages with both funny and cat in the path with the tag Funny Cats
  • Add category Other to pages with no category set.

Some examples in TOML:

# Re-arrange the blog posts so dog articles come first.
# Note that the `:counter` will restart at each `meta` element.
[[meta]]
match = "/*/blog/**"
matchFirst = """
**dog*
**cat*
**
"""
counterStart = 10 # default is 1
counterIncrement =  10 # default is 1
replace = true # default is false
[[meta.data]]
weight = ":counter"

# Add a category and a title to blog posts with none set.
[[meta]]
match = "/*/blog/**"
[[meta.data]]
title = "Blog article #:counter"
categories = ["Other"]
  • There can be many meta definitions, and all pages will be tested against all of them.
  • A page that matches both the Glob pattern in match and matchFirst will be assigned the given metadata. if replace = true any existing value will be replaced.
  • The match pattern is primarily "motivated by speed", but it also allows for simple and coarse-grained patterns in matchFirst.
  • The values in meta.data map directly to what you would normally set in page front matter.
  • The patterns in matchFirst sets the order of the 1-based :counter placeholder. In the example above, the articles about dog will get weight 10, 20, 30, and then come the cats and finally all the other.
  • In the example, matchFirst is a multiline string, but we will accept string arrays, too.
    We will match against the canonical path* (always prefixed by the current language, so one can do /{en,jp}/blog/** etc).

*: It would be tempting to match against .RelPermalink, but that will typically change across languages. We need to define the term canonical path as part of this issue.

Some references:

@bep bep added the Proposal label Nov 20, 2018
@bep bep added this to the v0.53 milestone Nov 20, 2018
@regisphilibert
Copy link
Member

Great vision for the future here! :)

@moorereason
Copy link
Contributor

This is cool.

My brain wants to do

weight = “{{ mul .Counter 10 }}”

Or is that a bridge too far?

@regisphilibert
Copy link
Member

I think we'll eventually need template functions and context like @moorereason suggests here when we need to assign value to pages built from external data source:

title = "{{ .Data.name }}"
subtitle = “{{ .Data.fields.sub_title }}”

@bep
Copy link
Member Author

bep commented Nov 23, 2018

My brain wants to do

My brain wants to do that, too. And we will eventually get there, but I'm not sure "Go templates" is the correct medicine -- as they are basically text templates (e.g. presentational), and also not very fast.

See https://discourse.gohugo.io/t/tools-libraries-that-can-drive-new-hugo-features/7891/48?u=bep

@bep
Copy link
Member Author

bep commented Nov 23, 2018

title = "{{ .Data.name }}"
subtitle = “{{ .Data.fields.sub_title }}”

OK, so I suspect the above would be highly ineffective at scale. But, it got me thinking a little.

[[meta]]
match = "/*/blog/**"
script = """
.Scratch.Add "counter" 1
$data := index .Site.Data .Page.Section
.Set "name" $data.name
.Set "weight" (.Scratch.Get "counter")
"""

The .Scratch would be scoped per matchFirst (or if not set: match).

The above would probably only work for TOML? I.e. the multiline stuff. But it would be pretty flexible, a.

Note that the field names above are very temporary (meta? maybe frontmatters).

@regisphilibert
Copy link
Member

regisphilibert commented Nov 23, 2018

The script value could ideally be stored in a separate file to improve coder's experience.

Or better yet one of those new custom function being discussed.(#5222). The func could even return an object built from the "page/data/source" context to allow setting multiple key/value pair to be consumed by meta.data.

# Add a category and a title to blog posts with none set.
[[meta]]
match = "/*/blog/**"
[[meta.data]]
script = "func getBlogMeta ."

I'm really bad at writing toml.

@moorereason
Copy link
Contributor

Dood. I missed to golua library. Hugo+Lua would be very interesting. I’ll have to ponder the implications.

@bep
Copy link
Member Author

bep commented Nov 23, 2018

But looking at the above attempts of "scripting in config" looks ... ugly. We risk ending up ...

https://gitlab.com/gitlab-org/gitlab-ce/blob/master/.gitlab-ci.yml

@stale
Copy link

stale bot commented Jun 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Jun 1, 2019
@bep bep modified the milestones: v0.56, v0.57 Jun 14, 2019
@stale stale bot removed the Stale label Jun 14, 2019
@bep bep modified the milestones: v0.57, v0.58 Jul 31, 2019
@bep bep modified the milestones: v0.58, v0.59 Aug 13, 2019
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.114.0, v0.115.0 Jun 8, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep closed this as completed Jan 31, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants