From 3ea138178db8f37705f43eae6467f537e6531e1f Mon Sep 17 00:00:00 2001 From: DeepChirp <66902050+DeepChirp@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:55:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=8C=96=E6=95=B0=E6=8D=AE=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _config.yml | 7 +++ layout/includes/head.pug | 3 ++ layout/includes/head/structured_data.pug | 58 ++++++++++++++++++++++++ scripts/events/merge_config.js | 4 ++ 4 files changed, 72 insertions(+) create mode 100644 layout/includes/head/structured_data.pug diff --git a/_config.yml b/_config.yml index cab31b5..a59d593 100644 --- a/_config.yml +++ b/_config.yml @@ -1023,6 +1023,13 @@ Open_Graph_meta: # fb_admins: # 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 css_prefix: true diff --git a/layout/includes/head.pug b/layout/includes/head.pug index 56c29f0..59f3782 100644 --- a/layout/includes/head.pug +++ b/layout/includes/head.pug @@ -25,6 +25,9 @@ meta(name="theme-color" content=themeColor) //- Open_Graph include ./head/Open_Graph.pug +//- Structured Data +include ./head/structured_data.pug + !=favicon_tag(theme.favicon || config.favicon) link(rel="canonical" href=urlNoIndex(null,config.pretty_urls.trailing_index,config.pretty_urls.trailing_html)) diff --git a/layout/includes/head/structured_data.pug b/layout/includes/head/structured_data.pug new file mode 100644 index 0000000..d89eab8 --- /dev/null +++ b/layout/includes/head/structured_data.pug @@ -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) \ No newline at end of file diff --git a/scripts/events/merge_config.js b/scripts/events/merge_config.js index fe04871..d263094 100644 --- a/scripts/events/merge_config.js +++ b/scripts/events/merge_config.js @@ -567,6 +567,10 @@ hexo.extend.filter.register('before_generate', () => { enable: true, option: null }, + structured_data: { + enable: true, + format: 'json-ld', + }, css_prefix: true, inject: { head: null, From e5a52d5621e5a43514efd46cf5159d60fe531435 Mon Sep 17 00:00:00 2001 From: DeepChirp <66902050+DeepChirp@users.noreply.github.com> Date: Mon, 11 Nov 2024 16:29:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E4=B8=AD=E4=B8=8D=E5=90=88=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E7=9A=84=E5=BE=AE=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _config.yml | 2 - layout/includes/head/structured_data.pug | 60 +++++++----------------- scripts/events/merge_config.js | 3 +- 3 files changed, 19 insertions(+), 46 deletions(-) diff --git a/_config.yml b/_config.yml index a59d593..25c4d82 100644 --- a/_config.yml +++ b/_config.yml @@ -1027,8 +1027,6 @@ Open_Graph_meta: # 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 css_prefix: true diff --git a/layout/includes/head/structured_data.pug b/layout/includes/head/structured_data.pug index d89eab8..72173ea 100644 --- a/layout/includes/head/structured_data.pug +++ b/layout/includes/head/structured_data.pug @@ -1,6 +1,6 @@ if theme.structured_data.enable && page.layout === 'post' - - const format = theme.structured_data.format || 'json-ld' + // use json-ld to add structured data const title = page.title const url = page.permalink @@ -12,47 +12,23 @@ if theme.structured_data.enable && page.layout === 'post' 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 - }] - }; + 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 - }; - } + jsonLdScript = JSON.stringify(jsonLd, null, 2); - - 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) \ No newline at end of file + script(type="application/ld+json"). + !{jsonLdScript} diff --git a/scripts/events/merge_config.js b/scripts/events/merge_config.js index d263094..2bb6f74 100644 --- a/scripts/events/merge_config.js +++ b/scripts/events/merge_config.js @@ -568,8 +568,7 @@ hexo.extend.filter.register('before_generate', () => { option: null }, structured_data: { - enable: true, - format: 'json-ld', + enable: true }, css_prefix: true, inject: { From 648ca6eb4f8465798f6080ac3324f2a1470ab047 Mon Sep 17 00:00:00 2001 From: DeepChirp <66902050+DeepChirp@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:42:46 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E7=AE=80=E5=8C=96=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=8C=96=E6=95=B0=E6=8D=AE=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E5=B5=8C?= =?UTF-8?q?=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _config.yml | 3 +-- layout/includes/head/structured_data.pug | 2 +- scripts/events/merge_config.js | 4 +--- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/_config.yml b/_config.yml index 25c4d82..ad0c20c 100644 --- a/_config.yml +++ b/_config.yml @@ -1025,8 +1025,7 @@ Open_Graph_meta: # Structured Data # https://developers.google.com/search/docs/guides/intro-structured-data -structured_data: - enable: true +structured_data: true # Add the vendor prefixes to ensure compatibility css_prefix: true diff --git a/layout/includes/head/structured_data.pug b/layout/includes/head/structured_data.pug index 72173ea..cacb02c 100644 --- a/layout/includes/head/structured_data.pug +++ b/layout/includes/head/structured_data.pug @@ -1,4 +1,4 @@ -if theme.structured_data.enable && page.layout === 'post' +if theme.structured_data && page.layout === 'post' - // use json-ld to add structured data diff --git a/scripts/events/merge_config.js b/scripts/events/merge_config.js index 2bb6f74..bff1125 100644 --- a/scripts/events/merge_config.js +++ b/scripts/events/merge_config.js @@ -567,9 +567,7 @@ hexo.extend.filter.register('before_generate', () => { enable: true, option: null }, - structured_data: { - enable: true - }, + structured_data: true, css_prefix: true, inject: { head: null,