Ghost博客修改添加JSON_LD数据,接入百度熊掌号

生活 Mar 09, 2020
1

申请到百度熊掌号后,第一步就是要对页面进行改造,以符合JSON_LD数据格式,便于内容提交和展现。

对于使用Ghost博客的用户来说,Ghost博客本身已经使用JSON_LD数据格式支持Google的AMP格式,我们只需要对相应内容进行修改即可满足百度熊掌号需求。

  1. 在熊掌号-搜索资源平台-内容提交-页面格式校验中,提交任意文章页面url和网页源码。页面格式选择非MIP。
  2. 通过校验可知,Ghost生成的页面有三处不符合百度JSON_LD数据格式
  • @id不存在
  • pubdate不存在
  • title不存在

ghost专门定义了一个data层,model属性全在里面,我们修改添加相应内容即可。

/var/www/ghost/versions/1.16.0/core/server/data/meta

找到/var/www/ghost/versions/1.16.0/core/server/data/meta,找到schema.js,查到到“function getPostSchema(metaData, data)”段落,

function getPostSchema(metaData, data) {
// CASE: metaData.excerpt for post context is populated by either the custom excerpt, the meta description,
// or the automated excerpt of 50 words. It is empty for any other context.
var description = metaData.excerpt ? escapeExpression(metaData.excerpt) : null,
schema;

schema = {
'@context': 'https://schema.org',
'@type': 'Article',
publisher: {
'@type': 'Organization',
name: escapeExpression(metaData.blog.title),
logo: schemaImageObject(metaData.blog.logo) || null
},
author: {
'@type': 'Person',
name: escapeExpression(data.post.author.name),
image: schemaImageObject(metaData.authorImage),
url: metaData.authorUrl,
sameAs: trimSameAs(data, 'post'),
description: data.post.author.metaDescription ?
escapeExpression(data.post.author.metaDescription) :
null
},
headline: escapeExpression(metaData.metaTitle),
url: metaData.url,
datePublished: metaData.publishedDate,
dateModified: metaData.modifiedDate,
image: schemaImageObject(metaData.coverImage),
keywords: metaData.keywords && metaData.keywords.length > 0 ?
metaData.keywords.join(', ') : null,
description: description,
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
}
};
schema.author = trimSchema(schema.author);
return trimSchema(schema);

}

headling对应title,url对应'@id',datepushed对应pubdate,添加百度熊掌号id。

function getPostSchema(metaData, data) {
// CASE: metaData.excerpt for post context is populated by either the custom excerpt, the meta description,
// or the automated excerpt of 50 words. It is empty for any other context.
var description = metaData.excerpt ? escapeExpression(metaData.excerpt) : null,
schema;

schema = {
'@context': 'https://schema.org',
'@type': 'Article',
publisher: {
'@type': 'Organization',
name: escapeExpression(metaData.blog.title),
logo: schemaImageObject(metaData.blog.logo) || null
},
author: {
'@type': 'Person',
name: escapeExpression(data.post.author.name),
image: schemaImageObject(metaData.authorImage),
url: metaData.authorUrl,
sameAs: trimSameAs(data, 'post'),
description: data.post.author.metaDescription ?
escapeExpression(data.post.author.metaDescription) :
null
},
 	// 熊掌号 headling- title
title: escapeExpression(metaData.metaTitle),
// 熊掌号 url- '@id'
 	'@id': metaData.url,
 	// 熊掌号 - appid
 	'appid': "1537258473247122",
     // 熊掌号 搜索出图
      "images": ["{{img_url feature_image absolute='true'}}"],
 	// 熊掌号 datepushed- pubdate
pubDate: metaData.publishedDate,
dateModified: metaData.modifiedDate,
image: schemaImageObject(metaData.coverImage),
keywords: metaData.keywords && metaData.keywords.length > 0 ?
metaData.keywords.join(', ') : null,
description: description,
mainEntityOfPage: {
'@type': 'WebPage',
'@id': metaData.blog.url || null
}
};
schema.author = trimSchema(schema.author);
return trimSchema(schema);

}

Ghost toISOString() 方法返回ISO格式的字符串,格式为:"YYYY-MM-DDTHH:mm:ss.sssZ" 所以为达到MIP "yyyy-MM-ddTHH:mm:ss",格式要求,需要将后5个字符去掉。修改/var/www/ghost/versions/1.16.0/core/server/data/meta中published_date.js中的

return new Date(data[context].published_at).toISOString();

  return new Date(data[context].published_at).toISOString().slice(0, -5);

重启ghost系统。就符合了百度熊掌号的内容格式标准。

席一舟

四野八荒,苦茶清禅。渔樵江渚,浊酒西风。 螽斯蓝寿,稀禾芡菱。嬉笑怒骂,行藏自由。 策马悬崖,雕弓满月。惊涛拍岸,毕力遐方。 银鞍白马,一剑寒霜。此生谁料,咸是妙明。

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.