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

feat: add parents property on page #10567

Merged
merged 2 commits into from Dec 23, 2022
Merged

feat: add parents property on page #10567

merged 2 commits into from Dec 23, 2022

Conversation

septs
Copy link
Contributor

@septs septs commented Dec 22, 2022

Add parents property on page, simplify breadcrumb navigation implementation complexity

community tricks see: https://discourse.gohugo.io/search?q=breadcrumb


in HTML:

{{- $page := . -}
<nav class="breadcrumb">
<ul>
{{- range .Parents.Reverse }}
<li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
{{- end }}
<li class="active">{{ .LinkTitle }}</li>
</ul>
</nav>

in JSON-LD:

see https://developers.google.com/search/docs/appearance/structured-data/breadcrumb

{{- $breadcrumbItems := slice -}}
{{- range $index, $page := append .Parents.Reverse . -}}
  {{-
  $item := dict
    "@type" "ListItem"
    "position" (add $index 1)
    "name" $page.Title
    "item" $page.Permalink
  -}}
  {{- $breadcrumbItems = append $breadcrumbItems $item -}}
{{- end -}}
<script type="application/ld+json">
{{-
dict
  "@context" "https://schema.org"
  "@type" "BreadcrumbList"
  "itemListElement" $breadcrumbItems
-}}
</script>

@bep
Copy link
Member

bep commented Dec 22, 2022

I agree that the idea is good, but Parents isn't a good name (a Page has never more than 1 Parent).

I suggest we use .Ancestors.

Also, could you:

  • Squash the commits and add one commit message that follows the commit message guidelines in CONTRIBUTION.md
  • Add a short description in the Page variables section about this new method.

func (pt pageTree) Ancestors() (parents page.Pages) {
parent := pt.Parent()
for parent != nil {
parents = append(parents, parent)
Copy link
Member

@bep bep Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but

  • parents should be named something else
  • we only use named return values when really needed (e.g. when we need to capture a panic and assign to err), so I would prefer Ancestors() page.Pages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I can do it. I need to merge this soonish, as I'm releasing today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I got COVID-19 and I'm fine now

@bep
Copy link
Member

bep commented Dec 22, 2022

@jmooring for me this is an obvious improvement, agree?

@jmooring
Copy link
Member

this is an obvious improvement, agree?

Yes. Took it for a spin. 👍

@bep bep merged commit eb0c8f9 into gohugoio:master Dec 23, 2022
bep pushed a commit that referenced this pull request Dec 23, 2022
@septs septs deleted the feat/parents branch December 23, 2022 10:03
Copy link

This pull request 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 Dec 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants