mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-10 21:17:07 +08:00
feat: 添加结构化数据支持
This commit is contained in:
@@ -1023,6 +1023,13 @@ Open_Graph_meta:
|
|||||||
# fb_admins:
|
# fb_admins:
|
||||||
# fb_app_id:
|
# fb_app_id:
|
||||||
|
|
||||||
|
# Structured Data
|
||||||
|
# https://developers.google.com/search/docs/guides/intro-structured-data
|
||||||
|
structured_data:
|
||||||
|
enable: true
|
||||||
|
# Choose: json-ld / microdata
|
||||||
|
format: 'json-ld'
|
||||||
|
|
||||||
# Add the vendor prefixes to ensure compatibility
|
# Add the vendor prefixes to ensure compatibility
|
||||||
css_prefix: true
|
css_prefix: true
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ meta(name="theme-color" content=themeColor)
|
|||||||
//- Open_Graph
|
//- Open_Graph
|
||||||
include ./head/Open_Graph.pug
|
include ./head/Open_Graph.pug
|
||||||
|
|
||||||
|
//- Structured Data
|
||||||
|
include ./head/structured_data.pug
|
||||||
|
|
||||||
!=favicon_tag(theme.favicon || config.favicon)
|
!=favicon_tag(theme.favicon || config.favicon)
|
||||||
link(rel="canonical" href=urlNoIndex(null,config.pretty_urls.trailing_index,config.pretty_urls.trailing_html))
|
link(rel="canonical" href=urlNoIndex(null,config.pretty_urls.trailing_index,config.pretty_urls.trailing_html))
|
||||||
|
|
||||||
|
|||||||
58
layout/includes/head/structured_data.pug
Normal file
58
layout/includes/head/structured_data.pug
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
if theme.structured_data.enable && page.layout === 'post'
|
||||||
|
-
|
||||||
|
const format = theme.structured_data.format || 'json-ld'
|
||||||
|
|
||||||
|
const title = page.title
|
||||||
|
const url = page.permalink
|
||||||
|
const imageVal = page.cover_type === 'img' ? page.cover : theme.avatar.img
|
||||||
|
const image = imageVal ? full_url_for(imageVal) : ''
|
||||||
|
const datePublished = page.date.toISOString()
|
||||||
|
const dateModified = (page.updated || page.date).toISOString()
|
||||||
|
const author = page.copyright_author || config.author
|
||||||
|
const authorHrefVal = page.copyright_author_href || theme.post_copyright.author_href || site.url;
|
||||||
|
const authorHref = full_url_for(authorHrefVal);
|
||||||
|
|
||||||
|
if (format === 'json-ld') {
|
||||||
|
const jsonLd = {
|
||||||
|
"@context": "https://schema.org",
|
||||||
|
"@type": "BlogPosting",
|
||||||
|
"headline": title,
|
||||||
|
"url": url,
|
||||||
|
"image": image,
|
||||||
|
"datePublished": datePublished,
|
||||||
|
"dateModified": dateModified,
|
||||||
|
"author": [{
|
||||||
|
"@type": "Person",
|
||||||
|
"name": author,
|
||||||
|
"url": authorHref
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
|
||||||
|
jsonLdScript = JSON.stringify(jsonLd, null, 2);
|
||||||
|
} else if (format === 'microdata') {
|
||||||
|
microdata = {
|
||||||
|
headline: title,
|
||||||
|
url: url,
|
||||||
|
image: image,
|
||||||
|
datePublished: datePublished,
|
||||||
|
dateModified: dateModified,
|
||||||
|
authorName: author,
|
||||||
|
authorUrl: authorHref
|
||||||
|
};
|
||||||
|
}
|
||||||
|
-
|
||||||
|
|
||||||
|
if (format === 'json-ld')
|
||||||
|
script(type="application/ld+json").
|
||||||
|
!{jsonLdScript}
|
||||||
|
else if (format === 'microdata')
|
||||||
|
div(itemscope itemtype="https://schema.org/BlogPosting")
|
||||||
|
meta(itemprop="headline" content=microdata.headline)
|
||||||
|
link(itemprop="url" href=microdata.url)
|
||||||
|
if microdata.image
|
||||||
|
img(itemprop="image" src=microdata.image alt=microdata.headline)
|
||||||
|
meta(itemprop="datePublished" content=microdata.datePublished)
|
||||||
|
meta(itemprop="dateModified" content=microdata.dateModified)
|
||||||
|
div(itemprop="author" itemscope itemtype="https://schema.org/Person")
|
||||||
|
meta(itemprop="name" content=microdata.authorName)
|
||||||
|
link(itemprop="url" href=microdata.authorUrl)
|
||||||
@@ -567,6 +567,10 @@ hexo.extend.filter.register('before_generate', () => {
|
|||||||
enable: true,
|
enable: true,
|
||||||
option: null
|
option: null
|
||||||
},
|
},
|
||||||
|
structured_data: {
|
||||||
|
enable: true,
|
||||||
|
format: 'json-ld',
|
||||||
|
},
|
||||||
css_prefix: true,
|
css_prefix: true,
|
||||||
inject: {
|
inject: {
|
||||||
head: null,
|
head: null,
|
||||||
|
|||||||
Reference in New Issue
Block a user