From f1c9fa5622ad986f43ba8641bbfe1bf8e1232e4f Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Thu, 8 May 2025 13:48:23 +0200 Subject: [PATCH] refactor: internal and external links --- layouts/_default/single.html | 52 +----------- layouts/blog/single.html | 80 ++----------------- .../partials/single/main-body/appendices.html | 32 ++++++++ .../partials/single/main-body/comments.html | 33 ++++++++ .../partials/single/main-body/content.html | 28 +++++++ .../single/main-body/corrigendum.html | 37 +++++++++ layouts/partials/single/main-body/links.html | 36 +++++++++ .../main-body/links/external-links.html | 40 ++++++++++ .../main-body/links/internal-links.html | 46 +++++++++++ .../main-body/links/link/basic-link.html | 23 ++++++ .../main-body/links/link/page-link.html | 35 ++++++++ .../links/link/page-resource-link.html | 37 +++++++++ .../main-body/links/link/title-link.html | 32 ++++++++ layouts/partials/text/item-title.html | 2 +- 14 files changed, 388 insertions(+), 125 deletions(-) create mode 100644 layouts/partials/single/main-body/appendices.html create mode 100644 layouts/partials/single/main-body/comments.html create mode 100644 layouts/partials/single/main-body/content.html create mode 100644 layouts/partials/single/main-body/corrigendum.html create mode 100644 layouts/partials/single/main-body/links.html create mode 100644 layouts/partials/single/main-body/links/external-links.html create mode 100644 layouts/partials/single/main-body/links/internal-links.html create mode 100644 layouts/partials/single/main-body/links/link/basic-link.html create mode 100644 layouts/partials/single/main-body/links/link/page-link.html create mode 100644 layouts/partials/single/main-body/links/link/page-resource-link.html create mode 100644 layouts/partials/single/main-body/links/link/title-link.html diff --git a/layouts/_default/single.html b/layouts/_default/single.html index ef3c935..4a15d6b 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -78,58 +78,10 @@ {{ define "main-body" }}
{{- if ( or .Params.internal_links .Params.external_links ) -}} -
- {{- with .Params.internal_links -}} -

Internal Links

-
    - {{- range . -}} - {{ if eq ( printf "%T" . ) "map[string]interface {}" }} - {{- if .src -}} - {{- $src := $.Page.Resources.GetMatch .src -}} - {{- if ( not $src ) -}} - {{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" ( $.Get .src ) $.Page.File.Path $.Page.Resources -}} - {{- end -}} -
  • {{ .title | $.RenderString | safeHTML }}
  • - {{- else -}} -
  • {{ .title | $.RenderString | safeHTML }}
  • - {{- end -}} - {{ else }} -
  • Link
  • - {{ end }} - {{- end -}} -
- {{- end -}} - - {{- with .Params.external_links -}} -

External Links

-
    - {{- range . -}} - {{ if eq ( printf "%T" . ) "map[string]interface {}" }} -
  • - {{ .title | safeHTML }} -
  • - {{ else }} -
  • - Link -
  • - {{ end }} - {{- end -}} -
- {{- end -}} -
+ {{- partial "single/main-body/links.html" ( dict "pc" . "sc" $ ) -}} {{- end -}} - {{ .Content }} + {{- partial "single/main-body/content.html" ( dict "pc" . "sc" $ ) -}}
{{ end }} diff --git a/layouts/blog/single.html b/layouts/blog/single.html index 94b9edc..1cb9d1c 100644 --- a/layouts/blog/single.html +++ b/layouts/blog/single.html @@ -76,10 +76,7 @@ {{ end }} {{ define "main-body" }} -
+
{{ with .Summary }}
- {{- with .Params.internal_links -}} -

Internal Links

-
    - {{- range . -}} - {{ if eq ( printf "%T" . ) "map[string]interface {}" }} -
  • {{ .title | safeHTML }}
  • - {{ else }} -
  • Link
  • - {{ end }} - {{- end -}} -
- {{- end -}} - - {{- with .Params.external_links -}} -

External Links

-
    - {{- range . -}} - {{ if eq ( printf "%T" . ) "map[string]interface {}" }} -
  • {{ .title | safeHTML }}
  • - {{ else }} -
  • Link
  • - {{ end }} - {{- end -}} -
- {{- end -}} -
+ {{- partial "single/main-body/links.html" ( dict "pc" . "sc" $ ) -}} {{- end -}} - -
- {{ .Content }} -
+ {{- partial "single/main-body/content.html" ( dict "pc" . "sc" $ ) -}} {{ if fileExists ( path.Join $.Page.File.Dir "appendices.md" ) }} -
-

- Appendices -

- {{ $file := path.Join $.Page.File.Dir "appendices.md" | readFile }} - {{ $file | .RenderString }} -
+ {{- partial "single/main-body/appendices.html" ( dict "pc" . "sc" $ ) -}} {{ end }} {{ if fileExists ( path.Join $.Page.File.Dir "corrigendum.md" ) }} -
-

- Corrigendum -

- {{ $file := path.Join $.Page.File.Dir "corrigendum.md" | readFile }} - {{ $file | .RenderString }} -
+ {{- partial "single/main-body/appendices.html" ( dict "pc" . "sc" $ ) -}} {{ end }}
{{ with .Page.Resources.Get "comments.md" }} -
-

- Comments -

-
    - {{ .RenderShortcodes }} -
-
+ {{- partial "single/main-body/comments.html" ( dict "pc" . "sc" $ ) -}} {{ end }} {{ end }} diff --git a/layouts/partials/single/main-body/appendices.html b/layouts/partials/single/main-body/appendices.html new file mode 100644 index 0000000..e121330 --- /dev/null +++ b/layouts/partials/single/main-body/appendices.html @@ -0,0 +1,32 @@ +{{- /* + Displays appendices for an item + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + + + + +
+

+ Appendices +

+ {{ $file := path.Join $.sc.Page.File.Dir "appendices.md" | readFile }} + {{ $file | .pc.RenderString }} +
diff --git a/layouts/partials/single/main-body/comments.html b/layouts/partials/single/main-body/comments.html new file mode 100644 index 0000000..9c96538 --- /dev/null +++ b/layouts/partials/single/main-body/comments.html @@ -0,0 +1,33 @@ +{{- /* + Displays comments for an item + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + + + + +
+

+ Comments +

+
    + {{ .pc.RenderShortcodes }} +
+
diff --git a/layouts/partials/single/main-body/content.html b/layouts/partials/single/main-body/content.html new file mode 100644 index 0000000..20dd194 --- /dev/null +++ b/layouts/partials/single/main-body/content.html @@ -0,0 +1,28 @@ +{{- /* + Displays main content for an item + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + + + + +
+ {{ .pc.Content }} +
diff --git a/layouts/partials/single/main-body/corrigendum.html b/layouts/partials/single/main-body/corrigendum.html new file mode 100644 index 0000000..c35377c --- /dev/null +++ b/layouts/partials/single/main-body/corrigendum.html @@ -0,0 +1,37 @@ +{{- /* + Displays corrigenda for an item + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + + + + +
+

+ Corrigendum +

+ {{ $file := path.Join $.sc.Page.File.Dir "corrigendum.md" | readFile }} + {{ $file | .pc.RenderString }} +
diff --git a/layouts/partials/single/main-body/links.html b/layouts/partials/single/main-body/links.html new file mode 100644 index 0000000..4ceef4f --- /dev/null +++ b/layouts/partials/single/main-body/links.html @@ -0,0 +1,36 @@ +{{- /* + Displays internal and external links for an item + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- $params := . -}} + + + + +{{- if ( not ( isset $params "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset $params "pc" ) ) -}} + {{- errorf "No page context received (%q)" $params.sc.Page.File.Dir -}} +{{- end -}} + + + + +
+ {{- if $params.pc.Params.internal_links -}} + {{- partial "single/main-body/links/internal-links.html" $params -}} + {{- end -}} + + {{- if $params.pc.Params.external_links -}} + {{- partial "single/main-body/links/external-links.html" $params -}} + {{- end -}} +
diff --git a/layouts/partials/single/main-body/links/external-links.html b/layouts/partials/single/main-body/links/external-links.html new file mode 100644 index 0000000..09f80d4 --- /dev/null +++ b/layouts/partials/single/main-body/links/external-links.html @@ -0,0 +1,40 @@ +{{- /* + Displays a list of external links + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- $params := . -}} + + + + +{{- if ( not ( isset $params "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset $params "pc" ) ) -}} + {{- errorf "No page context received (%q)" $params.sc.Page.File.Dir -}} +{{- end -}} + + + + +

External Links

+ diff --git a/layouts/partials/single/main-body/links/internal-links.html b/layouts/partials/single/main-body/links/internal-links.html new file mode 100644 index 0000000..df3f894 --- /dev/null +++ b/layouts/partials/single/main-body/links/internal-links.html @@ -0,0 +1,46 @@ +{{- /* + Displays a list of internal links + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- $params := . -}} + + + + +{{- if ( not ( isset $params "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset $params "pc" ) ) -}} + {{- errorf "No page context received (%q)" $params.sc.Page.File.Dir -}} +{{- end -}} + + + + +

Internal Links

+ diff --git a/layouts/partials/single/main-body/links/link/basic-link.html b/layouts/partials/single/main-body/links/link/basic-link.html new file mode 100644 index 0000000..185b986 --- /dev/null +++ b/layouts/partials/single/main-body/links/link/basic-link.html @@ -0,0 +1,23 @@ +{{- /* + Displays an internal link without a title + + @params pc Page context + @params sc Site context + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + + + + +
  • Link
  • diff --git a/layouts/partials/single/main-body/links/link/page-link.html b/layouts/partials/single/main-body/links/link/page-link.html new file mode 100644 index 0000000..0b070bd --- /dev/null +++ b/layouts/partials/single/main-body/links/link/page-link.html @@ -0,0 +1,35 @@ +{{- /* + Displays an internal link from another Page on the site. + + @params sc Site context + @params ref Page ref + */ +-}} + + + +{{- $page := $.sc.GetPage .ref -}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + +{{- if ( not $page ) -}} + {{- errorf "No Page found for ref '%q' (%q)." .ref $.sc.Page.File.Path -}} +{{- end -}} + + + + +
  • + {{- default $page.Title $page.Params.markup_title | $page.Page.RenderString -}} + +
  • diff --git a/layouts/partials/single/main-body/links/link/page-resource-link.html b/layouts/partials/single/main-body/links/link/page-resource-link.html new file mode 100644 index 0000000..0516b94 --- /dev/null +++ b/layouts/partials/single/main-body/links/link/page-resource-link.html @@ -0,0 +1,37 @@ +{{- /* + Displays an internal link from a page resource + + @params pc Page context + @params sc Site context + @params src Page resource source + */ +-}} + + + +{{- $src := $.sc.Page.Resources.GetMatch .src -}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + +{{- if ( not $src ) -}} + {{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" .src $.sc.Page.File.Path $.sc.Page.Resources -}} +{{- end -}} + + + + +
  • + {{ .title | $.sc.RenderString | safeHTML }} + (.{{- delimit ( last 1 ( split $src.RelPermalink "." ) ) "" -}}) +
  • diff --git a/layouts/partials/single/main-body/links/link/title-link.html b/layouts/partials/single/main-body/links/link/title-link.html new file mode 100644 index 0000000..c15d4ec --- /dev/null +++ b/layouts/partials/single/main-body/links/link/title-link.html @@ -0,0 +1,32 @@ +{{- /* + Displays an internal link with a title + + @params pc Page context + @params sc Site context + @params link Link URL + */ +-}} + + + + +{{- if ( not ( isset . "sc" ) ) -}} + {{- errorf "No site context received" -}} +{{- end -}} + +{{- if ( not ( isset . "pc" ) ) -}} + {{- errorf "No page context received (%q)" .sc.Page.File.Dir -}} +{{- end -}} + +{{- if ( or ( not ( isset . "title" ) ) ( eq .title "" ) ) -}} + {{- warnf "No title found (%q)" $.sc.Page.File.Path -}} +{{- end -}} + +{{- if not ( hasPrefix .link "http" ) -}} + {{- warnf "Link '%q' is not a URL, did you mean to use 'src'? (%q)" .link $.sc.Page.File.Path -}} +{{- end -}} + + + + +
  • {{ .title | $.sc.RenderString | safeHTML }}
  • diff --git a/layouts/partials/text/item-title.html b/layouts/partials/text/item-title.html index bdc569b..7b0bd1b 100644 --- a/layouts/partials/text/item-title.html +++ b/layouts/partials/text/item-title.html @@ -13,7 +13,7 @@ itemprop="name" >

    - {{- default .pc.Title .pc.Params.markup_title | .pc.Page.RenderString -}} + {{- default .pc.Title .pc.Params.markup_title | .pc.Page.RenderString -}}

    {{ with .pc.Params.subtitle }}

    {{ . | $.pc.Page.RenderString }}