mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
20 hours ago 50646e4496d0888da9b3edef79c0f5cc456bb3f8
fix(layouts): allow hiding the section title on summary cards

Summary cards always showed the section title above the post title. Add
the `ananke.pages.show_section_title` site setting and the
`ananke.show_section_title` page setting to hide it. Sections can set the
page setting for all of their pages with `cascade`.

Move the shared show/hide logic of `ShowDate.html` into the new
`func/IsShown.html` partial, so that both settings use the same rules.

Extend the quickstart test to check that dates and section titles can be
hidden on list pages.

Closes #1109
3 files modified
1 files added
106 ■■■■■ changed files
layouts/_partials/func/IsShown.html 33 ●●●●● patch | view | raw | blame | history
layouts/_partials/func/ShowDate.html 11 ●●●●● patch | view | raw | blame | history
layouts/summary.html 3 ●●●●● patch | view | raw | blame | history
scripts/test-hugo-quickstart.ts 59 ●●●●● patch | view | raw | blame | history
layouts/_partials/func/IsShown.html
New file
@@ -0,0 +1,33 @@
{{/*
    IsShown
    Decides whether an optional page element should be rendered.
    The element is shown by default. It is hidden when the site sets
    `ananke.pages.<key> = false`, unless a page opts back in with
    `ananke.<key> = true` in its front matter. A page can also hide the
    element with `ananke.<key> = false`. Sections can set the page value for
    all of their pages with `cascade`.
    @param page The page context.
    @param key The name of the setting, for example `show_date`.
    @return boolean True when the element should be displayed.
*/}}
{{- $site_value := "" -}}
{{- with site.Params.ananke -}}
    {{- with .pages -}}
        {{- $site_value = index . $.key -}}
    {{- 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 -}}
layouts/_partials/func/ShowDate.html
@@ -18,14 +18,5 @@
*/}}
{{ return and
    (not .Date.IsZero)
    (or
        (and
            (not (eq false site.Params.ananke.pages.show_date))
            (not (eq false .Params.ananke.show_date))
        )
        (and
            (eq false site.Params.ananke.pages.show_date)
            (eq true .Params.ananke.show_date)
        )
    )
    (partials.Include "func/IsShown.html" (dict "page" . "key" "show_date"))
}}
layouts/summary.html
@@ -1,7 +1,10 @@
{{ $show_date := partials.Include "func/ShowDate.html" . }}
{{ $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">
    {{ if $show_section_title }}
    {{with .CurrentSection.Title }}<span class="f6 db">{{ . }}</span>{{end}}
    {{ end }}
    {{ if $show_date }}
      <time class="f6 db" {{ 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) -}}
scripts/test-hugo-quickstart.ts
@@ -820,6 +820,12 @@
const SUMMARY_CARD_DATE_MARKER = "datetime=";
/**
 * Markup emitted by `summary.html` when the section title of a card is
 * rendered for the `cards` fixture section.
 */
const SUMMARY_CARD_SECTION_MARKER = '<span class="f6 db">Cards</span>';
/**
 * Create a content section whose list page exercises both the image and the
 * date behaviour of summary cards: one page has a featured image, the other
 * does not, and both carry an explicit date.
@@ -870,11 +876,12 @@
function assertListCardSummaries(
  listHtml: string,
  label: string,
  expectations: { images: boolean; dates: boolean },
  expectations: { images: boolean; dates: boolean; sectionTitles?: boolean },
): void {
  const failures: string[] = [];
  const hasImage = listHtml.includes(LIST_CARD_IMAGE_MARKER);
  const hasDate = listHtml.includes(SUMMARY_CARD_DATE_MARKER);
  const hasSectionTitle = listHtml.includes(SUMMARY_CARD_SECTION_MARKER);
  if (expectations.images && !hasImage) {
    failures.push(
@@ -900,6 +907,18 @@
    );
  }
  if (expectations.sectionTitles === true && !hasSectionTitle) {
    failures.push(
      "- expected summary card section titles but none were rendered by default",
    );
  }
  if (expectations.sectionTitles === false && hasSectionTitle) {
    failures.push(
      "- summary card section titles were rendered when 'ananke.pages.show_section_title' was false",
    );
  }
  if (failures.length > 0) {
    throw new Error(
      [
@@ -1368,6 +1387,7 @@
    assertListCardSummaries(await readTextFile(cardsListPath), "defaults", {
      images: false,
      dates: true,
      sectionTitles: true,
    });
    // Opt in to image cards and disable summary dates: images appear and the
@@ -1404,6 +1424,43 @@
    console.log("[OK ] List page image cards and summary dates (issue #217)");
    console.log(
      "\n[RUN] Hide summary card dates and section titles (issue #1109)",
    );
    await writeTextFile(
      configPath,
      [
        "baseURL = 'https://example.com/'",
        "title = 'Ananke Test Quickstart'",
        `theme = '${options.themeName}'`,
        "[params.ananke.pages]",
        "show_date = false",
        "show_section_title = false",
        "",
      ].join("\n"),
    );
    const cardsPlainBuildStep: StepDefinition = {
      name: "Build site with summary dates and section titles disabled",
      command: "hugo",
      args: [],
      cwd: projectRoot,
      expectedFiles: ["public/cards/index.html"],
    };
    const cardsPlainBuildReport = await executeHugoBuildStep(
      cardsPlainBuildStep,
      projectRoot,
    );
    reports.push(cardsPlainBuildReport);
    assertListCardSummaries(
      await readTextFile(cardsListPath),
      "dates and section titles disabled",
      { images: false, dates: false, sectionTitles: false },
    );
    console.log(
      "[OK ] Hide summary card dates and section titles (issue #1109)",
    );
    console.log("\nResult: PASS");
    if (options.keepOnSuccess) {