ttree / linkeddata
Neos CMS 辅助生成 JSON-LD (LinkedData)
Requires
- neos/neos: >=7.0
README
此包扩展了 Neos 内容仓库节点类型配置,以将节点或节点集合转换为 JSON-LD。
安装
composer require ttree/linkeddata
功能
- 支持简单文档
- 使用 EEL 查询
- 支持文档(s)之间的关系
- 生成 LinkedData 的验证
将预设添加到您的节点类型配置中
首先,您需要编辑您的节点类型配置(NodeTypes.yaml),以下示例是为一个课程(研讨会)而设计的,该课程可以包含许多课程(Schema.org 术语中的课程实例)。每个课程都可以有一个专门的地点
'Your.Package:Workshop':
options:
TtreeLinkedData:Generator:
default:
context:
sessions: "${q(node).children('sessions').children('[instanceof Your.Package:Session]').sort('begin', 'ASC').get()}"
nextSessions: "${sessions ? q(sessions).filterByDate('end', Date.now()).get() : null}"
description: "${q(node).children('main').find('[instanceof Neos.NodeTypes:Text]').get(0)}"
fragment:
'@context': http://schema.org
'@type': Course
name: "${q(node).property('title')}"
description: "${String.stripTags(String.cropAtSentence(q(description).property('text'), 180, '...'))}"
hasCourseInstance: "${nextSessions ? LinkedData.List(nextSessions, preset, false) : null}"
'Your.Package:Session':
options:
TtreeLinkedData:Generator:
default:
context:
ISO8601: "Y-m-d\TH:i:sO"
where: "${q(node).property('where')}"
course: "${q(node).closest('[instanceof Neos.Neos:Document]').get(0)}"
description: "${q(course).children('main').find('[instanceof Neos.NodeTypes:Text]').get(0)}"
fragment:
'@context': http://schema.org
'@type': CourseInstance
name: "${q(course).property('title')}"
description: "${String.stripTags(String.cropAtSentence(q(description).property('text'), 180, '...'))}"
startDate: "${Date.format(q(node).property('begin'), ISO8601)}"
endDate: "${Date.format(q(node).property('end'), ISO8601)}"
location: "${LinkedData.item(where, preset, false)}"
'Your.Package:Location':
options:
TtreeLinkedData:Generator:
default:
fragment:
'@context': http://schema.org
'@type': Place
name: "${q(node).property('title')}"
address:
@type: PostalAddress
addressLocality: "${q(node).property('addressLocality')}"
addressRegion: "${q(node).property('addressRegion')}"
postalCode: "${q(node).property('postalCode')}"
streetAddress: "${q(node).property('streetAddress')}"
您可以使用多个预设(default
是预设名称)。大多数LinkedData
EEL 辅助函数都接受一个预设参数。
每个预设包含两个部分,即context
配置和链接数据fragment
。
理解context
上下文包含一系列键值对。所有值都在表达式解析期间在 EEL 上下文中可用。
理解fragment
框架包含 JSON-LD 图的模板。模板可以是嵌套的。每个键的值可以是静态字符串或 EEL 表达式(请参阅以下包中可用的 EEL 辅助函数列表)。
在 HEAD 部分渲染 JSON-LD 图
渲染所有研讨会页面的 JSON-LD 图
prototype(Your.Package:WorkshopDocument) {
head.linkedData = Neos.Fusion:Array {
document = ${LinkedData.render(documentNode, 'default')}
}
}
使用此代码片段,Neos 将自动在您的文档 HEAD 部分渲染 JSON-LD 内容。
在文档体内部渲染 JSON-LD 图
您也可以在文档体中渲染 JSON-LD,在这种情况下,原型Ttree.LinkedData:Decorator
可能很有用。
假设您有一个用于渲染研讨会页面内容的原型,命名为Your.Package:WorkshopDocument
,以下代码片段将在您的标题后添加 JSON-LD。
Your.Package:WorkshopDocument.@process.jsonld = Ttree.LinkedData:Decorator
默认情况下,此原型使用默认预设和当前文档,但您可以配置它
Your.Package:WorkshopDocument.@process.jsonld = Ttree.LinkedData:Decorator {
preset = 'myCustomPreset'
node = ${node}
}
从设置或自定义 EEL 辅助函数渲染 JSON-LD
在某些情况下,您可以渲染静态 JSON-LD(如组织或网站)并需要使用自定义 EEL 辅助函数来准备数据。
Your:
Package:
linkedData:
website:
'@context': http://schema.org
'@type': WebSite
'@id': '#website'
url: http://yourdomain.com/
name: Your website name
potentialAction:
'@type': SearchAction
target: http://yourdomain.com/?s={search_term_string}
query-input: "required name=search_term_string"
organization:
'@context': http://schema.org
'@type': Organization
'@id': "#organization"
url: http://yourdomain.com/
name: Your organization name
Website
和Organization
需要仅在主页上渲染
prototype(Your.Package:HomeDocument) {
head.linkedData = Neos.Fusion:Array {
website = ${LinkedData.renderRaw(Configuration.setting('Your.Package.linkedData.website'))}
organization = ${LinkedData.renderRaw(Configuration.setting('Your.Package.linkedData.organization'))}
}
}
如果需要动态数据,请用您自己的替换Configuration
EEL 辅助函数。
可用的 EEL 辅助函数
LinkedData.render
LinkedData.render(NodeInterface $node, $preset = 'default'): string
此辅助函数接受一个NodeInterface
实例和预设名称。辅助函数将渲染完整的 JSON-LD 图并输出一个有效的 HTML5 脚本标签。
LinkedData.renderRaw
LinkedData.renderRaw(array $data): string
此辅助函数接受一个数组。辅助函数将渲染完整的 JSON-LD 图并输出一个有效的 HTML5 脚本标签。
LinkedData.list
LinkedData.list(array $collection, $preset = 'default', bool $withContext = true): array
此辅助函数接受一个NodeInterface
实例集合、预设名称和布尔开关,用于打印或不打印@context
键。辅助函数将返回一个 JSON-LD 图的数组。
您可以在预设配置中使用此辅助函数来渲染一对一/多对一关系(例如,在默认预设中的 Your.Package:Workshop 节点类型中的 hasCourseInstance)。
LinkedData.item
LinkedData.item(NodeInterface $node, $preset = 'default', bool $withContext = true): array
这个助手是一个 NodeInterface
实例,一个预设名称和布尔开关来控制是否打印 @context
键。助手将返回一个包含 JSON-LD 图的数组。
您可以在预设配置中使用此助手来渲染一对一的关系。
致谢
开发由ttree 有限公司 - Neos 解决方案提供商赞助。
我们尽力用大量的爱来制作这个包,我们欢迎赞助、支持请求等,请联系我们。
许可证
根据 MIT 许可证授权,请参阅LICENSE