Skip to content

Commit

Permalink
feat: article support JSON-LD
Browse files Browse the repository at this point in the history
  • Loading branch information
tomowang committed May 7, 2024
1 parent 25c009b commit 88ad924
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Check [https://hugo-theme-tailwind.tomo.dev/](https://hugo-theme-tailwind.tomo.d
* Default image process for lazy load and srcset
* Search (by using [fuse.js](https://fusejs.io/) and [mark.js](https://markjs.io/))
* Image support for article (using `image` param in front matter)
* Structured data [JSON-LD](https://json-ld.org/) for article (enable `jsonLD` in config file or front matter)

## Installation

Expand Down Expand Up @@ -99,6 +100,8 @@ Some of the configuration options are:
* `params.giscus`: giscus settings
* `params.social_media`: social media links shown in the footer
* `params.search`: search settings
* `params.author`: author used in the JSON-LD
* `params.jsonLD`: enable or disable JSON-LD (default disabled)

For social media link data, you can refer entries in `params.social_media.items`
(You can add more or disable existing entries in `params.social_media.items`).
Expand Down
2 changes: 1 addition & 1 deletion config/_default/hugo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
baseURL = 'https://example.org/'
title = 'My New Hugo Site'
author = ""
copyright = ""
languageCode = 'en'
defaultContentLanguage = "en"
Expand All @@ -11,6 +10,7 @@ enableInlineShortcodes = true
ignoreErrors = ["error-remote-getjson"]
enableRobotsTXT = true
enableEmoji = true
enableGitInfo = true

[markup]
[markup.highlight]
Expand Down
7 changes: 7 additions & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ toc = true

showReadingTime = true

# use structure json-ld for articles
jsonLD = false

[author]
name = "Hugo Author"
email = "user@example.com"

[search]
summaryInclude = 180 # show only part of content in search result.
minMatchCharLength = 2 # https://www.fusejs.io/api/options.html#minmatchcharlength
Expand Down
2 changes: 1 addition & 1 deletion exampleSite/config/_default/hugo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
baseURL = "https://gohugo.io"
title = "Hugo Theme Tailwind Example Site"
author = "Xiaoliang Wang"
copyright = "Xiaoliang Wang"
paginate = 3 # for demonstration of pagination
languageCode = "en"
Expand All @@ -10,6 +9,7 @@ enableInlineShortcodes = true
# See https://github.com/gohugoio/hugo/issues/7228#issuecomment-714490456
ignoreErrors = ["error-remote-getjson"]
theme = "hugo-theme-tailwind"
enableGitInfo = true

[markup]
_merge = "deep"
Expand Down
6 changes: 6 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ subtitle = "Example site for hugo-theme-tailwind"
# the list of set content will show up on your index page (baseurl).
contentTypeName = "post"

jsonLD = true

[author]
name = "Xiaoliang Wang"
email = "user@example.com"

[header]
logo = "logo.svg"

Expand Down
4 changes: 4 additions & 0 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{ define "head" }}
{{ partial "jsonld.html" . }}
{{ end }}

{{ define "main" }}
<div class="w-full max-w-4xl lg:max-w-5xl">
<div class="flex flex-col mt-6 mx-2 md:mx-0 rounded-lg overflow-hidden shadow-md bg-white dark:bg-gray-700">
Expand Down
26 changes: 26 additions & 0 deletions layouts/partials/jsonld.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{- if or .Site.Params.jsonLD .Params.jsonLD -}}
{{- $ld := (dict "@context" "https://schema.org" "@type" "Article") -}}
{{- $ld = merge $ld (dict "name" .Title "url" .Permalink) -}}
{{- $ld = merge $ld (dict "description" (.Summary | plainify)) -}}
{{- $ld = merge $ld (dict "datePublished" .Date "dateModified" .Lastmod) -}}
{{- $author := default $.Site.Params.Author.Name $.Params.author -}}

{{- with $author -}}
{{- $ld = merge $ld (dict "author" (dict "@type" "Person" "name" .)) -}}
{{- end -}}

{{- if .Params.image -}}
{{- $dest := .Params.image | safeURL -}}
{{- $dest = path.Join (path.Dir $dest) (path.Base $dest) -}}
{{- with .Resources.Get $dest -}}
{{- $ld = merge $ld (dict "image" .Permalink) -}}
{{- else -}}
{{- /* direct image url */ -}}
{{- $ld = merge $ld (dict "image" (.Params.image | safeURL)) -}}
{{- end -}}
{{- end -}}

<script type="application/ld+json">
{{ $ld }}
</script>
{{- end -}}
2 changes: 2 additions & 0 deletions theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ features = [
"tabler icons",
"multilingual",
"search",
"image process",
"json-ld",
]


Expand Down

0 comments on commit 88ad924

Please sign in to comment.