@bbc/psammead-rich-text-transforms

中文文档
2.0.7发布于 2022/03/11Apache-2.0ESM + CJS近 30 天下载 3641

A utility library to transform to structured rich text.

原始使用文档
文档目录8 节
  1. Supported block types
  2. Installation
  3. Usage
  4. candyXmlToRichText
  5. stringToRichText
  6. Contributing
  7. Code of Conduct
  8. License

rich-text-transforms

This libary provides two functions, one that takes candyXML, and one that takes a string. Both return rich text as structured JSON. The structured JSON uses the format that Optimo have opted for, see BBC confluence cps/Optimo+Client+Text+Blocks for more details.

Supported block types

The Optimo format consists of an array of nested ‘blocks’, where a block has a “type” (such as paragraph), and “model”. The model can contain fields such as ‘text’, attributes on that text - such as ‘bold’. The model can also contain an array of child blocks.

There are many types of block available.

Currently, this library only supports the following block types:

  • paragraph
  • text
  • urlLink
  • fragment

Installation

npm install @bbc/psammead-locales --save

Usage

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": []
              }
            }
          ]
        }
      }
    ]
  }
}

Contributing

Psammead is completely open source. We are grateful for any contributions, whether they be new components, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Psammead respository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Psammead is Apache 2.0 licensed.

版本与发布记录

@bbc/psammead-rich-text-transforms 共发布 15 个版本,首次发布于 2019/10/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.7latest2022/03/114 年前
2.0.62021/03/105 年前
2.0.52021/03/025 年前
2.0.42021/02/225 年前
2.0.32021/02/045 年前
2.0.22020/06/056 年前

关于 @bbc/psammead-rich-text-transforms 的常见问题

@bbc/psammead-rich-text-transforms 是什么?

A utility library to transform to structured rich text.

如何安装 @bbc/psammead-rich-text-transforms?

在项目目录运行 npm install @bbc/psammead-rich-text-transforms,或使用 pnpm add / yarn add / bun add。

@bbc/psammead-rich-text-transforms 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bbc__psammead-rich-text-transforms 是否存在。

@bbc/psammead-rich-text-transforms 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbc/psammead-rich-text-transforms 可以商用吗?

@bbc/psammead-rich-text-transforms 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/psammead-rich-text-transforms 的下载量有多少?

根据 npm 官方下载统计,@bbc/psammead-rich-text-transforms 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,641 次,日均约 121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbc/psammead-rich-text-transforms 还在维护吗?

最新版本 2.0.7 发布于 2022/03/11(4 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/10/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。