Skip to content

Commit

Permalink
feat: apply markdownify to post title (#183)
Browse files Browse the repository at this point in the history
closes #179

* First approach to markdownify title

* refactor: use title.html

* apply htmlUnescpae to title and rawTitle

Co-authored-by: reuixiy <reuixiy@gmail.com>
  • Loading branch information
Syralist and reuixiy authored Jun 2, 2020
1 parent aed76cc commit 1322fdd
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion layouts/index.sectionsatom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{ $page := . }}
{{- $author := partial "utils/author.html" $page -}}
<entry>
<title type="text">{{ .Title }}</title>
<title type="text">{{ (partial "utils/title.html" (dict "$" . "title" .Title)).rawTitle }}</title>
<link rel="alternate" type="text/html" href="{{ .Permalink }}" />
<id>{{ .Permalink }}</id>
<updated>{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" }}</updated>
Expand Down
2 changes: 1 addition & 1 deletion layouts/index.sectionsrss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{{ $page := . }}
{{- $author := partial "utils/author.html" $page -}}
<item>
<title>{{ .Title }}</title>
<title>{{ (partial "utils/title.html" (dict "$" . "title" .Title)).rawTitle }}</title>
<link>{{ .Permalink }}</link>
<guid isPermaLink="true">{{ .Permalink }}</guid>
<pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate>
Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/components/post-nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<ul class="post-nav">
{{ if $next }}
<li class="post-nav-prev">
<a href="{{ $next.RelPermalink }}" rel="prev">&lt; {{$next.LinkTitle}}</a>
<a href="{{ $next.RelPermalink }}" rel="prev">&lt; {{ (partial "utils/title.html" (dict "$" $next "title" $next.LinkTitle)).htmlTitle }}</a>
</li>
{{ end }}
{{ if $prev }}
<li class="post-nav-next">
<a href="{{ $prev.RelPermalink }}" rel="next">{{ $prev.LinkTitle }} &gt;</a>
<a href="{{ $prev.RelPermalink }}" rel="next">{{ (partial "utils/title.html" (dict "$" $prev "title" $prev.LinkTitle)).htmlTitle }} &gt;</a>
</li>
{{ end }}
</ul>
Expand Down
16 changes: 9 additions & 7 deletions layouts/partials/components/post-share.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{ if and .Site.Params.enablePostShare (.Params.share | default .Site.Params.displayPostShare) }}

{{- $title := (partial "utils/title.html" (dict "$" . "title" .Title)).rawTitle -}}

{{- $description := .Description | default (partial "utils/summary.html" .) | default .Site.Params.siteDescription | plainify -}}

{{- $images := partial "utils/images.html" . -}}
Expand Down Expand Up @@ -31,7 +33,7 @@

{{ if .Site.Params.shareOnTwitter }}
<div class="share-item twitter">
{{ $url := (printf `https://twitter.com/share?url=%s&text=%s&hashtags=%s&via=%s` .Permalink .Title ($hashtags.Get "tags" | default "") .Site.Params.siteTwitter) }}
{{ $url := (printf `https://twitter.com/share?url=%s&text=%s&hashtags=%s&via=%s` .Permalink $title ($hashtags.Get "tags" | default "") .Site.Params.siteTwitter) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "twitter" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "twitter" "class" "twitter-icon") -}}
</a>
Expand All @@ -49,7 +51,7 @@

{{ if .Site.Params.shareOnLinkedIn }}
<div class="share-item linkedin">
{{ $url := (printf `https://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=%s&source=%s` .Permalink .Title $description .Site.Title) }}
{{ $url := (printf `https://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=%s&source=%s` .Permalink $title $description .Site.Title) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "linkedin" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "linkedin" "class" "linkedin-icon") -}}
</a>
Expand All @@ -58,7 +60,7 @@

{{ if .Site.Params.shareOnTelegram }}
<div class="share-item telegram">
{{ $url := (printf `https://t.me/share/url?url=%s&text=%s` .Permalink .Title) }}
{{ $url := (printf `https://t.me/share/url?url=%s&text=%s` .Permalink $title) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "telegram" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "telegram" "class" "telegram-icon") -}}
</a>
Expand All @@ -67,7 +69,7 @@

{{ if .Site.Params.shareOnWeibo }}
<div class="share-item weibo">
{{ $url := (printf `https://service.weibo.com/share/share.php?&url=%s&title=%s&pic=%s&searchPic=false` .Permalink .Title $images) }}
{{ $url := (printf `https://service.weibo.com/share/share.php?&url=%s&title=%s&pic=%s&searchPic=false` .Permalink $title $images) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "weibo" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "weibo" "class" "weibo-icon") -}}
</a>
Expand All @@ -76,7 +78,7 @@

{{ if .Site.Params.shareOnDouban }}
<div class="share-item douban">
{{ $url := (printf `https://www.douban.com/share/service?href=%s&name=%s&text=%s` .Permalink .Title $description) }}
{{ $url := (printf `https://www.douban.com/share/service?href=%s&name=%s&text=%s` .Permalink $title $description) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "douban" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "douban" "class" "douban-icon") -}}
</a>
Expand All @@ -85,7 +87,7 @@

{{ if .Site.Params.shareOnQQ }}
<div class="share-item qq">
{{ $url := (printf `https://connect.qq.com/widget/shareqq/index.html?url=%s&title=%s&summary=%s&pics=%s&site=%s` .Permalink .Title $description $images .Site.Title) }}
{{ $url := (printf `https://connect.qq.com/widget/shareqq/index.html?url=%s&title=%s&summary=%s&pics=%s&site=%s` .Permalink $title $description $images .Site.Title) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "qq" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "qq" "class" "qq-icon") -}}
</a>
Expand All @@ -94,7 +96,7 @@

{{ if .Site.Params.shareOnQzone }}
<div class="share-item qzone">
{{ $url := (printf `https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=%s&title=%s&summary=%s&pics=%s&site=%s` .Permalink .Title $description $images .Site.Title) }}
{{ $url := (printf `https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=%s&title=%s&summary=%s&pics=%s&site=%s` .Permalink $title $description $images .Site.Title) }}
<a href="{{ $url }}" title="{{ i18n "shareOnTitle" }}{{ i18n "qzone" }}" target="_blank" rel="noopener">
{{- partial "utils/icon.html" (dict "$" . "name" "qzone" "class" "qzone-icon") -}}
</a>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/components/related-posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h2 class="related-title">{{ i18n "relatedPosts" }}{{ partial "utils/icon.html"
<ul class="related-list">
{{ range . }}
<li class="related-item">
<a href="{{ .RelPermalink }}" class="related-link">{{ .LinkTitle }}</a>
<a href="{{ .RelPermalink }}" class="related-link">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
</li>
{{ end }}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<!-- Avoid Baidu Mobile Search Transcoding -->
<meta http-equiv="Cache-Control" content="no-siteapp" />

<title>{{ (partial "utils/title.html" .).title }}</title>
<title>{{ (partial "utils/title.html" (dict "$" . "title" .Title)).title }}</title>

<!-- CSS -->
{{- partial "style.html" . }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/pages/home-posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ range $paginator.Pages }}
<article class="content post home">
<h2 class="post-title">
<a href="{{ .RelPermalink }}" class="summary-title-link">{{ .LinkTitle }}</a>
<a href="{{ .RelPermalink }}" class="summary-title-link">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
</h2>
{{ if $.Site.Params.enablePostMetaInHome }}
{{ partial "components/post-meta.html" (dict "$" . "isHome" true) }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/pages/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{{- if $attrs.indent }} data-indent="true"{{ end }}
{{- if $attrs.tocNum }} data-toc-num="true"{{ end }}>

<h1 class="post-title">{{ .Title }}</h1>
<h1 class="post-title">{{ (partial "utils/title.html" (dict "$" $ "title" $.Title)).htmlTitle }}</h1>

{{ with .Params.subtitle }}
{{- $raw := . -}}
Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/pages/tree-categories.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ $category := $.Scratch.Get "category" }}
{{ if eq $context $category }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ $page.RelPermalink }}" class="category-post">{{ $page.LinkTitle }}</a>
<a href="{{ $page.RelPermalink }}" class="category-post">{{ (partial "utils/title.html" (dict "$" $page "title" $page.LinkTitle)).htmlTitle }}</a>
</p>
{{ end }}
{{ end }}
Expand All @@ -55,4 +55,4 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
</div>
</div>
</div>
</main>
</main>
4 changes: 2 additions & 2 deletions layouts/partials/pages/tree-sections.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
{{ $pages := $.Scratch.Get "pages" }}
{{ range $pages }}
<p style="padding-left: {{ add $padding (mul 2 1.2) }}em">
<a href="{{ .RelPermalink }}" class="category-post">{{ .LinkTitle }}</a>
<a href="{{ .RelPermalink }}" class="category-post">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
</p>
{{ end }}
{{ end }}
Expand All @@ -45,4 +45,4 @@ <h1 class="list-title">{{ .Title | default (.Type | title) }}</h1>
</div>
</div>
</div>
</main>
</main>
2 changes: 1 addition & 1 deletion layouts/partials/third-party/disqus.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- $rawTitle := (partial "utils/title.html" .).rawTitle -}}
{{- $rawTitle := (partial "utils/title.html" (dict "$" . "title" .Title)).rawTitle -}}
<script>
function loadComments() {
if (typeof DISQUS === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/utils/json-ld.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{- $description := .description -}}
{{- $baseURLWithLangFix := print `/` | absLangURL -}}
<!-- Title -->
{{- $rawTitle := (partial "utils/title.html" $).rawTitle -}}
{{- $rawTitle := (partial "utils/title.html" (dict "$" $ "title" $.Title)).rawTitle -}}
<!-- Date -->
{{- $dates := partial "utils/date.html" $ -}}
<!-- Author -->
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/utils/list-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ul class="list-part">
{{ range . }}
<li class="list-item">
<a href="{{ .RelPermalink }}" class="list-item-title">{{ .LinkTitle }}</a>
<a href="{{ .RelPermalink }}" class="list-item-title">{{ (partial "utils/title.html" (dict "$" . "title" .LinkTitle)).htmlTitle }}</a>
<time datetime="{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" }}" class="list-item-time">{{ .PublishDate.Format .Site.Params.listDateFormat }}</time>
</li>
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/utils/open-graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- Images -->
{{- $images := partial "utils/images.html" $ -}}

<meta property="og:title" content="{{ (partial "utils/title.html" $).rawTitle }}" />
<meta property="og:title" content="{{ (partial "utils/title.html" (dict "$" $ "title" $.Title)).rawTitle }}" />
<meta property="og:description" content="{{ $description }}" />
<meta property="og:url" content="{{ $.Permalink }}" />
<meta property="og:site_name" content="{{ $.Site.Title }}" />
Expand Down
35 changes: 19 additions & 16 deletions layouts/partials/utils/title.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
{{- $ := index . "$" -}}
{{- $rawTitle := .title -}}
<!-- Home -->
{{- $rawTitle := .Title -}}
{{- if eq .Kind "home" -}}
{{- $rawTitle = $rawTitle | default .Site.Title -}}
{{- if eq $.Kind "home" -}}
{{- $rawTitle = $rawTitle | default $.Site.Title -}}
<!-- Taxonomy -->
{{- else if eq .Kind "taxonomyTerm" -}}
{{- $rawTitle = $rawTitle | default (.Type | title) -}}
{{- else if eq $.Kind "taxonomyTerm" -}}
{{- $rawTitle = $rawTitle | default ($.Type | title) -}}
<!-- Taxonomy Term -->
{{- else if eq .Kind "taxonomy" -}}
{{- $taxonomyTermTitle := $rawTitle | default .Data.Term | default (delimit (last 1 (split (strings.TrimSuffix "/_index.md" .Path) "/")) " ") -}}
{{- with .Site.GetPage (printf "/%s" .Data.Plural) -}}
{{- $rawTitle = printf "%s: %s" (.Title | default (.Type | title)) $taxonomyTermTitle -}}
{{- else if eq $.Kind "taxonomy" -}}
{{- $taxonomyTermTitle := $rawTitle | default $.Data.Term | default (delimit (last 1 (split (strings.TrimSuffix "/_index.md" $.Path) "/")) " ") -}}
{{- with $.Site.GetPage (printf "/%s" $.Data.Plural) -}}
{{- $rawTitle = printf "%s: %s" ($.Title | default ($.Type | title)) $taxonomyTermTitle -}}
{{- end -}}
<!-- Section -->
{{- else if eq .Kind "section" -}}
{{- $rawTitle = $rawTitle | default (delimit (last 1 (split (strings.TrimSuffix "/_index.md" .Path) "/")) " ") | plainify -}}
{{- else if eq $.Kind "section" -}}
{{- $rawTitle = $rawTitle | default (delimit (last 1 (split (strings.TrimSuffix "/_index.md" $.Path) "/")) " ") -}}
<!-- Page -->
{{- else -}}
{{- $rawTitle = $rawTitle | plainify -}}
{{- $rawTitle = $rawTitle -}}
{{- end -}}

{{- $title := $rawTitle -}}
{{- if ne .Kind "home" -}}
{{- $title = printf "%s | %s" $title .Site.Title -}}
{{- $htmlTitle := partial "utils/markdownify.html" (dict "$" $ "raw" $rawTitle "isContent" false) -}}

{{- $title := $htmlTitle -}}
{{- if ne $.Kind "home" -}}
{{- $title = printf "%s | %s" $title $.Site.Title -}}
{{- end -}}

{{- return dict "rawTitle" $rawTitle "title" $title -}}
{{- return dict "rawTitle" ($htmlTitle | plainify | htmlUnescape) "title" ($title | plainify | htmlUnescape) "htmlTitle" $htmlTitle -}}

0 comments on commit 1322fdd

Please sign in to comment.