Skip to content

Commit

Permalink
fix: Get rid of unnecessary safeHTML usage (#135)
Browse files Browse the repository at this point in the history
Fixes #134.

* Simplify post-meta.html template logic

* Fixed indentation in previous commit

* Use icon.html template for zodiac icons in lists

* Remove unnecessary safeHTML usage in footer.html (busuanzi functionality unchanged, cannot test)

* Improved previous commit, no unnecessary markup for the copyright symbol

* Remove unnecessary safeHTML usage in style.html

* Use icon.html partial in the header

* Remove unnecesary safeHTML usage in script.html

* Removed unnecessary safeHTML usage in menu.html

* Remove unnecessary safeHTML use in header.html

* Use icon.html partial in back-to-top and port-share components

* Use icon.html partial in related-posts and socials components

* Remove unnecessary safeHTML use in feed generation

* Simplify minimal-footer-about component

* Removed safeHTML usage from post-updated-badge component

* Remove unnecessary safeHTML usage in post-copyright component

* Remove unnecessary safeHTML usage in post-nav component

* Simplify minimal-footer component and remove safeHTML usage

* Simplify post-tags component and remove safeHTML usage

* Made summary.html produce its result directly rather than via scratch, removed unnecessary safeHTML usage in home-posts.html

* Use icon.html partial on home-footage page

* Remove unnecessary safeHTML usage on post.html page

* Remove unnecessary safeHTML usage on home-poetry.html page

* Remove unnecessary safeHTML usage from OpenGraph data

* Remove unnecessary safeHTML usage from utils/list-item.html

* Improve image autodetection regex and remove unnecessary unsafeHTML usage in images.html

* Remove unnecessary safeHTML usage from relative-url.html

* Remove unnecessary safeHTML usage in content.html

* Revert image auto-detection changes, these aren't useful

* Delete the missing line in post-share

* Revert to using urls.Parse on tag and category names passed to .GetPage again

* Fix error caused by wrong context in list.html

* Replace hyphen with en dash

* Remove unnecessary safeHTML usage in post-gitinfo.html

* Remove unnecessary safeHTML usage of busuanzi in footer.html

Co-authored-by: reuixiy <reuixiy@gmail.com>
  • Loading branch information
palant and reuixiy committed May 14, 2020
1 parent 6cc1621 commit 91e84a1
Show file tree
Hide file tree
Showing 32 changed files with 215 additions and 270 deletions.
4 changes: 2 additions & 2 deletions layouts/_default/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h1 class="list-title">{{ .Title | default .Data.Term | default (delimit (last 1
{{ $.Scratch.Set "year" .Key }}
{{ end }}
{{ $year := $.Scratch.Get "year" }}
<h2 class="list-year">{{ $year }}{{ if $.Site.Params.chineseZodiac }}{{ replace (index $.Site.Data.SVG $zodiacName) "icon" "icon chinese-zodiac" | safeHTML }}{{ end }}</h2>
<h2 class="list-year">{{ $year }}{{ if $.Site.Params.chineseZodiac }}{{ partial "utils/icon.html" (dict "Deliver" $ "name" $zodiacName "class" "chinese-zodiac") }}{{ end }}</h2>
{{ if $.Site.Params.groupByMonth }}
{{ range .Pages.GroupByDate "January" }}
{{ $.Scratch.Delete "month" }}
Expand All @@ -94,4 +94,4 @@ <h3 class="list-month">{{ $month }}</h3>
</div>
</main>
{{ end }}
{{ end }}
{{ end }}
17 changes: 8 additions & 9 deletions layouts/index.sectionsatom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- printf `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }}
{{ `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }}
<!-- Reference: https://tools.ietf.org/html/rfc4287 -->
<!-- Reference: https://github.com/kaushalmodi/hugo-atom-feed/blob/master/layouts/_default/list.atom.xml -->
<!-- Reference: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml -->
Expand All @@ -10,11 +10,11 @@
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="{{ .Site.LanguageCode }}">
<title type="text">{{ .Site.Title }}</title>
<subtitle type="html">{{ .Site.Params.siteDescription }}</subtitle>
<updated>{{ now.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</updated>
<updated>{{ now.Format "2006-01-02T15:04:05-07:00" }}</updated>
<id>{{ .Permalink }}</id>
<link rel="alternate" type="text/html" href="{{ .Permalink }}" />
{{ with .OutputFormats.Get "SectionsAtom" -}}
{{ printf `<link rel="self" type="%s" href="%s" />` .MediaType .Permalink | safeHTML }}
<link rel="self" type="{{ .MediaType }}" href="{{ .Permalink }}" />
{{ end -}}
{{ with .Site.Author.name -}}
<author>
Expand All @@ -37,8 +37,8 @@
<title type="text">{{ .Title }}</title>
<link rel="alternate" type="text/html" href="{{ .Permalink }}" />
<id>{{ .Permalink }}</id>
<updated>{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</updated>
<published>{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</published>
<updated>{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" }}</updated>
<published>{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" }}</published>
{{ with $author -}}
<author>
{{ with .name -}}
Expand All @@ -55,11 +55,10 @@
{{ with $author.copyright -}}
<rights>{{ . | plainify }}</rights>
{{- end }}
{{- partial "utils/summary.html" $page -}}
{{- $summary := .Description | default ($page.Scratch.Get "summary") -}}
{{ printf `<summary type="html"><![CDATA[%s]]></summary>` $summary | safeHTML }}
{{- $summary := .Description | default (partial "utils/summary.html" $page) -}}
<summary type="html">{{ $summary | html }}</summary>
{{ if $.Site.Params.includeContent }}
{{ printf `<content type="html"><![CDATA[%s]]></content>` .Content | safeHTML }}
<content type="html">{{ .Content | html }}</content>
{{ end }}
<!-- Sections -->
{{ if eq $.Site.Params.categoryBy "sections" }}
Expand Down
15 changes: 7 additions & 8 deletions layouts/index.sectionsrss.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- printf `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }}
{{- `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }}
<!-- Reference: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml -->
<!-- Reference: https://github.com/kaushalmodi/hugo-atom-feed/blob/master/layouts/_default/list.atom.xml -->
<!-- Reference: https://validator.w3.org/feed/docs/rss2.html -->
Expand Down Expand Up @@ -28,9 +28,9 @@
{{ with .Site.Copyright }}
<copyright>{{ . | plainify }}</copyright>
{{ end }}
<lastBuildDate>{{ now.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
<lastBuildDate>{{ now.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</lastBuildDate>
{{ with .OutputFormats.Get "SectionsRSS" }}
{{ printf `<atom:link rel="self" type=%q href=%q />` .MediaType .Permalink | safeHTML }}
<atom:link rel="self" type="{{ .MediaType }}" href="{{ .Permalink }}" />
{{ end }}
{{ range (where $pages "Section" "in" .Site.Params.mainSections) }}
{{ $page := . }}
Expand All @@ -40,19 +40,18 @@
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
<guid isPermaLink="true">{{ .Permalink }}</guid>
<pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
<pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate>
{{ with $author.email }}
<author>{{ . }}{{ with $author.name }} ({{ . }}){{ end }}</author>
{{ end }}
{{ with $author.copyright -}}
<copyright>{{ . | plainify }}</copyright>
{{- end }}
{{ if $.Site.Params.includeContent }}
{{ printf `<description><![CDATA[%s]]></description>` .Content | safeHTML }}
<description>{{ .Content | html }}</description>
{{ else }}
{{- partial "utils/summary.html" $page -}}
{{- $summary := .Description | default ($page.Scratch.Get "summary") -}}
{{ printf `<description><![CDATA[%s]]></description>` $summary | safeHTML }}
{{- $summary := .Description | default (partial "utils/summary.html" $page) -}}
<description>{{ $summary | html }}</description>
{{ end }}
<!-- Sections -->
{{ if eq $.Site.Params.categoryBy "sections" }}
Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/components/back-to-top.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ if or (and .IsHome .Site.Params.displayBackToTopInHome) (and (not .IsHome) .Site.Params.enableBackToTop) }}
<div id="back-to-top" class="back-to-top">
<a href="#">{{ index .Site.Data.SVG .Site.Params.backToTopIcon | safeHTML }}</a>
<a href="#">{{ partial "utils/icon.html" (dict "Deliver" . "name" .Site.Params.backToTopIcon) }}</a>
</div>
{{ end }}
{{ end }}
14 changes: 3 additions & 11 deletions layouts/partials/components/minimal-footer-about.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,24 @@
<footer class="minimal-footer-about">
{{ with $.Site.Menus.socials }}
<div class="about-socials">
{{ $length := sub (len .) 1 }}
{{ range $index, $value := . }}
{{- $linkType := (string .Pre) -}}
{{- $iconName := (string .Post) -}}
{{- $icon := (index $.Site.Data.SVG $iconName) -}}
<a href="{{ .URL }}"{{ if eq $linkType "external" }} target="_blank" rel="external noopener"{{ end }}>{{ replace $icon "icon" (printf `icon %s` .Identifier) | safeHTML }}{{ .Name }}</a>
{{- if lt $index $length -}}
{{- print " " -}}
{{- end -}}
<a href="{{ .URL }}"{{ if eq $linkType "external" }} target="_blank" rel="external noopener"{{ end }}>{{ partial "utils/icon.html" (dict "Deliver" $ "name" $iconName "class" .Identifier) }}{{ .Name }}</a>
{{ end }}
</div>
{{ end }}
{{ with $.Site.Menus.links }}
<div class="about-links">
{{ $length := sub (len .) 1 }}
{{ range $index, $value := . }}
{{- $linkType := (string .Pre) -}}
{{- $iconName := (string .Post) -}}
{{- $icon := (index $.Site.Data.SVG $iconName) -}}
<a href="{{ .URL }}"{{ if eq $linkType "external" }} target="_blank" rel="external noopener"{{ end }}>{{ replace $icon "icon" (printf `icon %s` .Identifier) | safeHTML }}{{ .Name }}</a>
{{- if lt $index $length -}}
{{- print " " -}}
{{- end -}}
<a href="{{ .URL }}"{{ if eq $linkType "external" }} target="_blank" rel="external noopener"{{ end }}>{{ partial "utils/icon.html" (dict "Deliver" $ "name" $iconName "class" .Identifier) }}{{ .Name }}</a>
{{ end }}
</div>
{{ end }}
</footer>
{{ end }}
{{ end }}
{{ end }}
104 changes: 38 additions & 66 deletions layouts/partials/components/minimal-footer.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{{ if and .Site.Params.enableMinimalFooter (in .Site.Params.mainSections .Section) }}
<footer class="minimal-footer">
{{ with .Params.tags }}
{{ $length := sub (len .) 1 }}
<div class="post-tag">
{{- range $index, $tag := . -}}
{{- $url := urls.Parse ($tag | urlize) -}}
{{- $path := $url.Path -}}
<!-- Work-around for https://github.com/gohugoio/hugo/issues/6546 -->
{{- $path := (urls.Parse ($tag | urlize)).Path -}}
{{- with $.Site.GetPage (printf `/tags/%s` $path) -}}
<a href="{{ .RelPermalink }}" rel="tag" class="post-tag-link">#{{ .Slug | default .LinkTitle | default $tag | lower | anchorize }}</a>
{{- if lt $index $length -}}
{{- print " " -}}
{{- if gt $index 0 -}}
{{- " " -}}
{{- end -}}
<a href="{{ .RelPermalink }}" rel="tag" class="post-tag-link">#{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- end -}}
</div>
Expand All @@ -21,48 +20,32 @@
{{- with .Site.GetPage .Section -}}
<a href="{{ .RelPermalink }}" class="post-category-link active">{{ .Slug | default .Params.url | default .LinkTitle | default $.Section | lower | anchorize }}</a>
{{- end -}}
{{- if gt (len .Site.Params.mainSections) 1 -}}
{{- printf `%s` " | " | safeHTML -}}
{{- $sections := (.Site.Params.mainSections | symdiff (slice .Section)) -}}
{{- $length := sub (len $sections) 1 -}}
{{- range $index, $section := $sections -}}
{{- with $.Site.GetPage $section -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize }}</a>
{{- if lt $index $length -}}
{{- printf `%s` " | " | safeHTML -}}
{{- end -}}
{{- end -}}
{{- $sections := .Site.Params.mainSections | symdiff (slice .Section) -}}
{{- range $index, $section := $sections -}}
{{- " | " -}}
{{- with $.Site.GetPage $section -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize }}</a>
{{- end -}}
{{- end -}}
{{ else }}
{{ $sections := split (strings.TrimSuffix "/" ($.File.Dir | default $.Section)) "/" }}
{{ with $sections }}
{{ $.Scratch.Delete "sectionsDirMeta" }}
{{ $.Scratch.Delete "sectionsMeta" }}
{{ $.Scratch.Set "index" 0 }}
{{ range $sections }}
{{ $section := . }}
{{ $.Scratch.Add "sectionsDirMeta" (printf `/%s` $section) }}
{{ with $.Site.GetPage ($.Scratch.Get "sectionsDirMeta") }}
{{ $pathParts := split (strings.TrimSuffix "/" ($.File.Dir | default $.Section)) "/" }}
{{ $sections := dict }}
{{ with $pathParts }}
{{ range $index, $section := $pathParts }}
{{ with $.Site.GetPage (printf `/%s` $section) }}
{{ if (eq .Kind "section") }}
{{ $.Scratch.SetInMap "sectionsMeta" (printf `%s/%s` (string ($.Scratch.Get "index")) .RelPermalink) (.Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize) }}
{{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
{{ $sections = merge $sections (dict (printf `%s/%s` (string $index) .RelPermalink) (.Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize)) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ $sections := .Scratch.Get "sectionsMeta" }}
{{ with $sections }}
{{ $length := (sub (len $sections) 1) }}
{{- range $link, $title := $sections -}}
{{- $index := $link | replaceRE `(\d+)/.+` `$1` | int -}}
{{- if lt $index $length -}}
{{- $.Scratch.Set "delimiter" ($.Site.Params.categoryDelimiter | default "/") -}}
{{- else -}}
{{- $.Scratch.Set "delimiter" "" -}}
{{- if gt $index 0 -}}
{{- $.Site.Params.categoryDelimiter | default "/" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- printf `<a href="%s" class="post-category-link">%s</a>%s` ($link | replaceRE `\d+/(.+)` `$1`) ($title | lower) $delimiter | safeHTML -}}
<a href="{{ $link | replaceRE `\d+/(.+)` `$1` }}" class="post-category-link">{{ $title | lower }}</a>
{{- end -}}
{{ end }}
{{ end }}
Expand All @@ -72,53 +55,42 @@
{{ with .Params.categories }}
<div class="post-category">
{{ if $.Site.Params.enableVerticalBarStructure }}
{{- $currentTopLevelCategory := (delimit (first 1 .) " ") -}}
{{- $url := urls.Parse ($currentTopLevelCategory | urlize) -}}
{{- $path := $url.Path -}}
{{- $currentTopLevelCategory := (index . 0) -}}
<!-- Work-around for https://github.com/gohugoio/hugo/issues/6546 -->
{{- $path := (urls.Parse ($currentTopLevelCategory | urlize)).Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
<a href="{{ .RelPermalink }}" class="post-category-link active">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- $.Scratch.Delete "topLevelCategories" -}}
{{- $categories := slice -}}
{{- range $.Site.RegularPages -}}
{{- with .Param "categories" -}}
{{- $topLevelCategory := ((delimit (first 1 .) " ") | urlize) -}}
{{- $.Scratch.Add "topLevelCategories" (slice $topLevelCategory) -}}
{{- $topLevelCategory := (index . 0) -}}
{{- $categories = union $categories (slice $topLevelCategory) -}}
{{- end -}}
{{- end -}}
{{- $categories := uniq ($.Scratch.Get "topLevelCategories") -}}
{{- if gt (len $categories) 1 -}}
{{- printf `%s` " | " | safeHTML -}}
{{- $categories := ($categories | symdiff (slice ($currentTopLevelCategory | urlize))) -}}
{{- $length := sub (len $categories) 1 -}}
{{- range $index, $category := $categories -}}
{{- $category := urls.Parse $category -}}
{{- $path := $category.Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- if lt $index $length -}}
{{- printf `%s` " | " | safeHTML -}}
{{- end -}}
{{- end -}}
{{- $categories = uniq $categories | symdiff (slice $currentTopLevelCategory) -}}
{{- range $index, $category := $categories -}}
{{- " | " -}}
<!-- Work-around for https://github.com/gohugoio/hugo/issues/6546 -->
{{- $path := (urls.Parse ($category | urlize)).Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- end -}}
{{ else }}
{{ $length := sub (len .) 1 }}
{{- range $index, $category := . -}}
{{- if lt $index $length -}}
{{- $.Scratch.Set "delimiter" ($.Site.Params.categoryDelimiter | default "/") -}}
{{- else -}}
{{- $.Scratch.Set "delimiter" "" -}}
{{- if gt $index 0 -}}
{{- $.Site.Params.categoryDelimiter | default "/" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- $url := urls.Parse ($category | urlize) -}}
{{- $path := $url.Path -}}
<!-- Work-around for https://github.com/gohugoio/hugo/issues/6546 -->
{{- $path := (urls.Parse ($category | urlize)).Path -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
{{- printf `<a href="%s" class="post-category-link">%s</a>%s` .RelPermalink (.Slug | default .LinkTitle | default $category | lower | anchorize) $delimiter | safeHTML -}}
<a href="{{ .RelPermalink }}" class="post-category-link">{{ .Slug | default .LinkTitle | default $path | lower | anchorize }}</a>
{{- end -}}
{{- end -}}
{{ end }}
</div>
{{ end }}
{{ end }}
</footer>
{{ end }}
{{ end }}

0 comments on commit 91e84a1

Please sign in to comment.