rich-text-transforms
这个Libary提供了两个功能,一个是取取取糖果XML,另一个是取取取取取取取取取取取取
一个字符串。 两者都以结构化的JSON形式还原出丰富的文本. 结构化的JSON使用
optimo 所选择的格式,参见 BBC 组合 cps/Optimo+Client+Text+Blocks 更多细节.
支持的块类型
Optimo格式由一组被嵌入的“块”组成,其中块有一个 “类型”(例如第1款)和“模式”。 该模型可以包含诸如 " 文本 " ,该文本的属性,如 " 粗体 " 。 该模型还可以包含 数组子块.
有许多种类的区块可供使用.
目前,此库仅支持以下块类型:
- 段 次 页 次
- 文本
- uRLLink 链接
- 碎片
安装
npm install @bbc/psammead-locales --save
使用量
candyXmlToRichText
const { candyXmlToRichText } = require("@bbc/psammead-rich-text-transforms")
const xml = `
<body>
<paragraph>
Read more:
<link>
<caption>foo</caption>
<url href="https://example.com/foo"/>
</link> bar <bold>baz</bold>
</paragraph>
</body>
`
candyXmlToRichText(xml)
{
"type": "text",
"model": {
"blocks": [
{
"type": "paragraph",
"model": {
"text": "Read more: foo bar baz",
"blocks": [
{
"type": "fragment",
"model": {
"text": "Read more: ",
"attributes": []
}
},
{
"type": "urlLink",
"model": {
"text": "foo",
"locator": "https://example.com/foo",
"blocks": [
{
"type": "fragment",
"model": {
"text": "foo",
"attributes": []
}
}
]
}
},
{
"type": "fragment",
"model": {
"text": " bar ",
"attributes": []
}
},
{
"type": "fragment",
"model": {
"text": "baz",
"attributes": [
"bold"
]
}
}
]
}
}
]
}
stringToRichText
const stringToRichText = require("@bbc/psammead-rich-text-transforms")
const string = "Hello world"
stringToRichText(string)
{
"type": "text",
"model": {
"blocks": [
{
"type": "paragraph",
"model": {
"text": "Hello world",
"blocks": [
{
"type": "fragment",
"model": {
"text": "Hello world",
"attributes": []
}
}
]
}
}
]
}
}
捐款
Psammead完全开源. 我们感谢任何贡献,无论是新的组件、错误修正还是总体改进。 请参看我们的主要贡献指南,可查阅: psammead 仓库的根。 。 。 .
行为守则
我们欢迎对这项工作的反馈和帮助。 通过参与这个项目,你同意遵守 行为守则请稍候再读一下.
许可证
萨姆米德 Apache 2.0 许可。 。 。 .