块编辑器开发文档

@wordpress/block-serialization-spec-parser

💡 云策文档标注

概述

该库包含 WordPress 文章的语法文件(grammar.pegjs),用于定义块序列化规范,并生成解析器。它支持通过 PEG 解析器生成器在不同语言中实现,并提供了安装和使用方法。

关键要点

  • 核心功能:提供 grammar.pegjs 文件,定义 WordPress 块的序列化规范,用于生成解析器。
  • 技术基础:基于 PEG(解析表达式语法),可通过 PEG.js 等工具在不同语言中实现。
  • 安装方式:通过 npm 安装包,命令为 npm install @wordpress/block-serialization-spec-parser --save。
  • 使用方法:导入 parse 函数,解析块序列化字符串,返回块对象数组。
  • 项目背景:作为 Gutenberg 项目的一部分,以 monorepo 形式组织,支持贡献。

代码示例

import { parse } from '@wordpress/block-serialization-spec-parser';

parse( '<!-- wp:core/more --><!--more--><!-- /wp:core/more -->' );
// [{"attrs": null, "blockName": "core/more", "innerBlocks": [], "innerHTML": "<!--more-->"}]

📄 原文内容

This library contains the grammar file (grammar.pegjs) for WordPress posts which is a block serialization specification which is used to generate the actual parser which is also bundled in this package.

PEG parser generators are available in many languages, though different libraries may require some translation of this grammar into their syntax. For more information see:

Installation

Install the module

npm install @wordpress/block-serialization-spec-parser --save

Usage

import { parse } from '@wordpress/block-serialization-spec-parser';

parse( '<!-- wp:core/more --><!--more--><!-- /wp:core/more -->' );
// [{"attrs": null, "blockName": "core/more", "innerBlocks": [], "innerHTML": "<!--more-->"}]

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.