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

Patrick Kollitsch
13 hours ago f6db2ffa757d50f3167846bef57a9b9afd796fd1
fix(layouts): skip the home introduction when it has no content

An empty home page rendered an empty, padded article above the recent
posts. Render the article only when the home page has content, and add
fixture tests for both cases.

Closes #663

Signed-off-by: Patrick Kollitsch <davidsneighbourdev+gh@gmail.com>
1 files modified
7 files added
59 ■■■■■ changed files
layouts/home.html 10 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/empty/content/_index.md 4 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/empty/content/post/first.md 5 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/empty/hugo.toml 2 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/filled/content/_index.md 5 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/filled/content/post/first.md 5 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/filled/hugo.toml 2 ●●●●● patch | view | raw | blame | history
tests/fixtures/home-content/home-content.test.mjs 26 ●●●●● patch | view | raw | blame | history
layouts/home.html
@@ -8,9 +8,13 @@
    {{- $text_alignment = "tr-l" -}}
  {{- end -}}
  <article class="cf ph3 ph5-l pv3 pv4-l f4 {{ $text_alignment }} center measure-wide lh-copy nested-links {{ $.Param "text_color" | compare.Default "mid-gray" }}">
    {{ .Content }}
  </article>
  {{- /* Render the introduction only when the home page has content, so an
         empty home page has no blank space above the recent posts. */ -}}
  {{ with .Content }}
    <article class="cf ph3 ph5-l pv3 pv4-l f4 {{ $text_alignment }} center measure-wide lh-copy nested-links {{ $.Param "text_color" | compare.Default "mid-gray" }}">
      {{ . }}
    </article>
  {{ end }}
  {{/* Define a section to pull recent posts from. For Hugo 0.20 this will default to the section with the most number of pages. */}}
  {{ $mainSections := site.Params.mainSections | compare.Default (collections.Slice "post") }}
tests/fixtures/home-content/empty/content/_index.md
New file
@@ -0,0 +1,4 @@
---
title: Home
---
tests/fixtures/home-content/empty/content/post/first.md
New file
@@ -0,0 +1,5 @@
---
title: First post
---
Body.
tests/fixtures/home-content/empty/hugo.toml
New file
@@ -0,0 +1,2 @@
title = "Home content fixture"
theme = "ananke"
tests/fixtures/home-content/filled/content/_index.md
New file
@@ -0,0 +1,5 @@
---
title: Home
---
Home introduction.
tests/fixtures/home-content/filled/content/post/first.md
New file
@@ -0,0 +1,5 @@
---
title: First post
---
Body.
tests/fixtures/home-content/filled/hugo.toml
New file
@@ -0,0 +1,2 @@
title = "Home content fixture"
theme = "ananke"
tests/fixtures/home-content/home-content.test.mjs
New file
@@ -0,0 +1,26 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { load } from "cheerio";
import { buildFixtureSite } from "../../support/prepare-site.mjs";
const root = dirname(fileURLToPath(import.meta.url));
function home(name) {
  const output = buildFixtureSite({ source: join(root, name) });
  return load(readFileSync(join(output, "index.html"), "utf8"));
}
test("home page without content renders no introduction article (#663)", () => {
  const $ = home("empty");
  assert.equal($("main > article").length, 0);
  assert.equal($("main a[href$='/post/first/']").length > 0, true);
});
test("home page with content renders the introduction article (#663)", () => {
  const $ = home("filled");
  assert.equal($("main > article").length, 1);
  assert.equal($("main > article").text().trim(), "Home introduction.");
});