feat(layouts): separate single and summary dates
Add show_single_date and show_summary_date with show_date as the shared fallback. Resolve page values before site values, and retain Hugo cascade behaviour and the undated-page guard.
Check date precedence and independent visibility through single pages, all summary templates, and home page cards.
Closes #1112
| | |
| | | element with `ananke.<key> = false`. Sections can set the page value for |
| | | all of their pages with `cascade`. |
| | | |
| | | The page key, page fallback, site key, and site fallback are checked in that order. |
| | | |
| | | @param page The page context. |
| | | @param key The name of the setting, for example `show_date`. |
| | | |
| | | @param fallback Optional shared setting to use when the specific key is unset. |
| | | @return boolean True when the element should be displayed. |
| | | |
| | | */}} |
| | | {{- $site_value := "" -}} |
| | | {{- with site.Params.ananke -}} |
| | | {{- with .pages -}} |
| | | {{- $site_value = index . $.key -}} |
| | | {{- $keys := slice .key -}} |
| | | {{- with .fallback -}} |
| | | {{- $keys = $keys | append . -}} |
| | | {{- end -}} |
| | | {{- $show := true -}} |
| | | {{- $found := false -}} |
| | | {{- $page_values := .page.Params.ananke | default dict -}} |
| | | {{- $site_values := site.Params.ananke.pages | default dict -}} |
| | | {{- range slice $page_values $site_values -}} |
| | | {{- $values := . -}} |
| | | {{- range $keys -}} |
| | | {{- $value := index $values . -}} |
| | | {{- if and (not $found) (or (eq true $value) (eq false $value)) -}} |
| | | {{- $show = $value -}} |
| | | {{- $found = true -}} |
| | | {{- end -}} |
| | | {{- end -}} |
| | | {{- end -}} |
| | | {{- $page_value := "" -}} |
| | | {{- with .page.Params.ananke -}} |
| | | {{- $page_value = index . $.key -}} |
| | | {{- end -}} |
| | | {{- $show := not (eq false $site_value) -}} |
| | | {{- if eq true $page_value -}} |
| | | {{- $show = true -}} |
| | | {{- else if eq false $page_value -}} |
| | | {{- $show = false -}} |
| | | {{- end -}} |
| | | {{- return $show -}} |
| | |
| | | {{/* |
| | | ShowDate |
| | | |
| | | Decides whether a page's date should be rendered. |
| | | Returns false for pages without a date. Otherwise, resolves the requested |
| | | date setting with show_date as its shared fallback. |
| | | |
| | | Dates are shown by default. They are hidden when the site sets |
| | | `ananke.pages.show_date = false`, unless a page opts back in with |
| | | `ananke.show_date = true` in its front matter. A page can also hide its |
| | | own date with `ananke.show_date = false`. Pages without a date |
| | | (`.Date.IsZero`) never render one. |
| | | |
| | | This mirrors the date logic used in `single.html` so that single pages and |
| | | summary cards stay consistent. |
| | | |
| | | @param . The page context. |
| | | @param . A page, or a dictionary with page and key (show_single_date or show_summary_date). |
| | | @return boolean True when the date should be displayed. |
| | | |
| | | */}} |
| | | {{ return and |
| | | (not .Date.IsZero) |
| | | (partials.Include "func/IsShown.html" (dict "page" . "key" "show_date")) |
| | | }} |
| | | {{- $page := . -}} |
| | | {{- $key := "show_date" -}} |
| | | {{- if reflect.IsMap . -}} |
| | | {{- $page = .page -}} |
| | | {{- $key = .key -}} |
| | | {{- end -}} |
| | | {{- return and |
| | | (not $page.Date.IsZero) |
| | | (partials.Include "func/IsShown.html" (dict "page" $page "key" $key "fallback" "show_date")) |
| | | -}} |
| | |
| | | {{ $show_date := partials.Include "func/ShowDate.html" . }} |
| | | {{ $show_date := partials.Include "func/ShowDate.html" (dict "page" . "key" "show_summary_date") }} |
| | | <div class="mb3 pa4 {{ $.Param "text_color" | compare.Default "mid-gray" }} overflow-hidden"> |
| | | {{ if $show_date }} |
| | | <time class="f6 db" {{ fmt.Printf `datetime="%s"` (.Date.Format "2006-01-02T15:04:05Z07:00") | safe.HTMLAttr }}> |
| | |
| | | </p> |
| | | {{ end }} |
| | | {{/* Hugo uses Go's date formatting is set by example. Here are two formats */}} |
| | | {{ if partials.Include "func/ShowDate.html" . }} |
| | | {{ if partials.Include "func/ShowDate.html" (dict "page" . "key" "show_single_date") }} |
| | | <time class="f6 mv4 dib tracked" {{ fmt.Printf `datetime="%s"` (.Date.Format "2006-01-02T15:04:05Z07:00") | safe.HTMLAttr }}> |
| | | {{- .Date | time.Format (compare.Default "January 2, 2006" .Site.Params.date_format) -}} |
| | | </time> |
| | |
| | | {{ $featured_image := partials.Include "func/GetFeaturedImage.html" . }} |
| | | {{ $show_date := partials.Include "func/ShowDate.html" . }} |
| | | {{ $show_date := partials.Include "func/ShowDate.html" (dict "page" . "key" "show_summary_date") }} |
| | | <article class="bb b--black-10"> |
| | | <div class="db pv4 ph3 ph0-l dark-gray no-underline"> |
| | | <div class="flex-column flex-row-ns flex"> |
| | |
| | | {{ $show_date := partials.Include "func/ShowDate.html" . }} |
| | | {{ $show_date := partials.Include "func/ShowDate.html" (dict "page" . "key" "show_summary_date") }} |
| | | {{ $show_section_title := partials.Include "func/IsShown.html" (dict "page" . "key" "show_section_title") }} |
| | | <div class="w-100 mb4 nested-copy-line-height relative bg-white"> |
| | | <div class="mb3 pa4 gray overflow-hidden bg-white"> |
| | |
| | | await writeTextFile(join(sectionDir, "without-image.md"), withoutImage); |
| | | } |
| | | |
| | | /** Exercise date precedence through the actual single and summary templates. */ |
| | | async function assertDateSettings( |
| | | projectRoot: string, |
| | | themeName: string, |
| | | ): Promise<void> { |
| | | type Settings = Record<string, boolean>; |
| | | const cases: { |
| | | label: string; |
| | | site?: Settings; |
| | | page?: Settings; |
| | | cascade?: Settings; |
| | | undated?: boolean; |
| | | single: boolean; |
| | | summary: boolean; |
| | | }[] = [ |
| | | { label: "defaults", single: true, summary: true }, |
| | | { |
| | | label: "shared site fallback", |
| | | site: { show_date: false }, |
| | | single: false, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "specific site over shared site", |
| | | site: { show_date: false, show_single_date: true }, |
| | | single: true, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "summary site override", |
| | | site: { show_date: true, show_summary_date: false }, |
| | | single: true, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "page shared over specific site", |
| | | site: { show_single_date: false, show_summary_date: false }, |
| | | page: { show_date: true }, |
| | | single: true, |
| | | summary: true, |
| | | }, |
| | | { |
| | | label: "page shared false over site true", |
| | | site: { show_single_date: true, show_summary_date: true }, |
| | | page: { show_date: false }, |
| | | single: false, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "specific page over shared page", |
| | | page: { show_date: false, show_single_date: true }, |
| | | single: true, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "summary page over shared page", |
| | | page: { show_date: true, show_summary_date: false }, |
| | | single: true, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "opposite independent settings", |
| | | page: { show_single_date: false, show_summary_date: true }, |
| | | single: false, |
| | | summary: true, |
| | | }, |
| | | { |
| | | label: "cascade over site", |
| | | site: { show_summary_date: true }, |
| | | cascade: { show_summary_date: false }, |
| | | single: true, |
| | | summary: false, |
| | | }, |
| | | { |
| | | label: "page over cascade", |
| | | cascade: { show_single_date: false, show_summary_date: false }, |
| | | page: { show_single_date: true, show_summary_date: true }, |
| | | single: true, |
| | | summary: true, |
| | | }, |
| | | { |
| | | label: "page map over cascade map", |
| | | cascade: { show_summary_date: false }, |
| | | page: { show_date: true }, |
| | | single: true, |
| | | summary: true, |
| | | }, |
| | | { |
| | | label: "undated page", |
| | | page: { show_single_date: true, show_summary_date: true }, |
| | | undated: true, |
| | | single: false, |
| | | summary: false, |
| | | }, |
| | | ]; |
| | | const settings = (values: Settings = {}): string[] => |
| | | Object.entries(values).map(([key, value]) => `${key} = ${String(value)}`); |
| | | for (const variant of ["compact", "image", "post"]) { |
| | | for (const test of cases) { |
| | | const label = `${variant}: ${test.label}`; |
| | | await writeTextFile( |
| | | join(projectRoot, "hugo.toml"), |
| | | [ |
| | | "baseURL = 'https://example.com/'", |
| | | `theme = '${themeName}'`, |
| | | "[params]", |
| | | "mainSections = ['cards']", |
| | | "[params.ananke.pages]", |
| | | `show_list_images = ${String(variant === "image")}`, |
| | | ...settings(test.site), |
| | | "", |
| | | ].join("\n"), |
| | | ); |
| | | await writeTextFile( |
| | | join(projectRoot, "content/cards/_index.md"), |
| | | [ |
| | | "+++", |
| | | "title = 'Cards'", |
| | | ...(variant === "post" ? ["type = 'post'"] : []), |
| | | ...(test.cascade |
| | | ? ["[cascade.params.ananke]", ...settings(test.cascade)] |
| | | : []), |
| | | "+++", |
| | | "", |
| | | ].join("\n"), |
| | | ); |
| | | for (const name of ["with-image", "without-image"]) { |
| | | await writeTextFile( |
| | | join(projectRoot, `content/cards/${name}.md`), |
| | | [ |
| | | "+++", |
| | | `title = '${name}'`, |
| | | ...(test.undated ? [] : ["date = 2024-01-15T00:00:00Z"]), |
| | | ...(variant === "post" ? ["type = 'post'"] : []), |
| | | ...(name === "with-image" |
| | | ? ["featured_image = '/images/card-hero.jpg'"] |
| | | : []), |
| | | ...(test.page ? ["[params.ananke]", ...settings(test.page)] : []), |
| | | "+++", |
| | | "", |
| | | "Date setting fixture.", |
| | | "", |
| | | ].join("\n"), |
| | | ); |
| | | } |
| | | await executeHugoBuildStep( |
| | | { name: label, command: "hugo", args: [], cwd: projectRoot }, |
| | | projectRoot, |
| | | ); |
| | | const single = await readTextFile( |
| | | join(projectRoot, "public/cards/with-image/index.html"), |
| | | ); |
| | | if (single.includes('class="f6 mv4 dib tracked"') !== test.single) { |
| | | throw new Error(`Single date assertion failed: ${label}`); |
| | | } |
| | | for (const path of ["cards/index.html", "index.html"]) { |
| | | const html = await readTextFile(join(projectRoot, "public", path)); |
| | | const count = (html.match(/datetime="2024-01-15T00:00:00Z"/g) ?? []) |
| | | .length; |
| | | if (count !== (test.summary ? 2 : 0)) { |
| | | throw new Error( |
| | | `Summary date assertion failed: ${label}, ${path}, found ${count}`, |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Assert that a list page renders summary cards with the expected image and |
| | | * date behaviour. |
| | |
| | | "[OK ] Hide summary card dates and section titles (issue #1109)", |
| | | ); |
| | | |
| | | console.log("\n[RUN] Independent single and summary dates (issue #1112)"); |
| | | await assertDateSettings(projectRoot, options.themeName); |
| | | console.log("[OK ] Independent single and summary dates (issue #1112)"); |
| | | |
| | | console.log("\nResult: PASS"); |
| | | |
| | | if (options.keepOnSuccess) { |