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

iNote-

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

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

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

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

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

  1. 找到/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);
    

    }

  2. 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);
    

    }

  3. 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系统。就符合了百度熊掌号的内容格式标准。

生活
发表于 2017-12-05,更新于 2024-04-25 阅读量: