Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/icelab/draft-js-ast-exporter
Export content from draft-js into an abstract syntax tree.
https://github.com/icelab/draft-js-ast-exporter
Last synced: 3 months ago
JSON representation
Export content from draft-js into an abstract syntax tree.
- Host: GitHub
- URL: https://github.com/icelab/draft-js-ast-exporter
- Owner: icelab
- License: mit
- Created: 2016-04-29T07:02:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-04T04:08:46.000Z (almost 2 years ago)
- Last Synced: 2024-04-13T19:12:42.206Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 194 KB
- Stars: 37
- Watchers: 9
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-draft-js - Draft.js AST Exporter - Export content into an abstract syntax tree (AST). (Common Utilities)
README
# Draft.js AST Exporter
Allows you to export the content from [Facebook’s Draft.js editor](https://facebook.github.io/draft-js/) to an abstract syntax tree (AST). Together with the companion [`draft-js-ast-importer`](https://github.com/icelab/draft-js-ast-importer) it forms the full cycle of exporting content from a Draft.js editor instance and then re-importing it.
## Why?
Draft.js supports exporting its content JSON, but this format is a little awkward. Blocks of text are disconnected from their style and entity ranges, and the depth of blocks isn’t implicit. So when it comes to rendering that content in contexts outside a Draft.js editor, you need to have an understanding of how those ranges should be applied and how blocks fit together.
The AST generated by `draft-js-ast-exporter` mitigates this issue by joining common ranges together into marked `inline` or `entity` sections, and by allowing blocks to be nested within one another based on their depth.
## Installation
```
npm install --save draft-js-ast-exporter
```## Usage
```js
import exporter from 'draft-js-ast-exporter'
const ast = exporter(editorState)
```### Entity modification
You can modify the entity data as it’s being exported by passing in an `options.entityModifiers` object with a functions to modify that entity-type’s data:
```js
import exporter from 'draft-js-ast-exporter'
const options = {
entityModifiers: {
'LINK': (data) => {
let copy = Object.assign({}, data)
// Strip protocols from `url` keys
copy.url = copy.url.replace(/^https?:/, '')
return copy
}
},
}
const ast = exporter(editorState, options)
```This would be run for _every_ entity type of `LINK`.
## Output
[A simple example of the AST output](docs/output.md) is included in the `docs`.