import React from 'react';
import { md, code, Props } from '@atlaskit/docs';
const _default_1: React.ReactElement = md`
## Why?
Working with ADF might be tricky at times, especially extracting data or manipulating existing documents.
This package provides a set of utilities to traverse, modify and create ADF documents.
## Traverse
**@atlaskit/adf-utils/traverse** provides methods to extract and manipulate data inside
an ADF document.
${(
)}
### Example:
${code`
import { traverse } from '@atlaskit/adf-utils/traverse.es'; // .es for ES2015
const doc = {/* some ADF doc */};
traverse(doc, {
// emoji visitor, matches all nodes with type === 'emoji'
emoji: (node, parent) => {
// do something with the node
},
mention: (node, parent) => {
// do something with mention
},
taskList: (node, parent) => {
// do something with taskList
}
})
`}
${(
',
},
},
parameters: [
{
kind: 'param',
type: null,
value: {
kind: 'generic',
value: {
kind: 'id',
name: 'ADFEntity',
},
},
},
{
kind: 'param',
type: null,
value: {
kind: 'function',
returnType: {
kind: 'generic',
value: {
kind: 'id',
name: 'any',
},
},
parameters: [
{
kind: 'param',
type: null,
value: {
kind: 'generic',
value: {
kind: 'id',
name: 'ADFEntity',
},
},
},
],
},
},
],
},
},
leadingComments: [
{
type: 'commentBlock',
value:
"[Array like](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) method to map over ADF nodes. Non-destructive – meaning that ADF can't be changed from the callback.",
},
],
},
],
},
},
],
}}
/>
)}
### Example:
${code`
import { map } from '@atlaskit/adf-utils/traverse.es'; // .es for ES2015
const doc = {/* some ADF doc */};
const allNodeTypesFromTheDoc = map(doc, node => node.type);
`}
${(
',
},
},
parameters: [
{
kind: 'param',
type: null,
value: {
kind: 'generic',
value: {
kind: 'id',
name: 'ADFEntity',
},
},
},
{
kind: 'param',
type: null,
value: {
kind: 'function',
returnType: {
kind: 'generic',
value: {
kind: 'id',
name: 'boolean',
},
},
parameters: [
{
kind: 'param',
type: null,
value: {
kind: 'generic',
value: {
kind: 'id',
name: 'ADFEntity',
},
},
},
],
},
},
],
},
},
leadingComments: [
{
type: 'commentBlock',
value:
"[Array like](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) method to filter out nodes that don't match a predicate. Non-destructive – meaning that ADF can't be changed from the callback.",
},
],
},
],
},
},
],
}}
/>
)}
### Example:
${code`
import { filter } from '@atlaskit/adf-utils/traverse.es'; // .es for ES2015
const doc = {/* some ADF doc */};
const links = filter(
doc,
node =>
(node.marks || []).some(mark => mark.type === 'link')
);
`}
${(
)}
### Example:
${code`
import { reduce } from '@atlaskit/adf-utils/traverse.es'; // .es for ES2015
const doc = {/* some ADF doc */};
const emojiString = reduce(
doc,
(acc, node) =>
node.type === 'emoji'
? (acc += '| ' + node.attrs.text + ' – ' + node.attrs.shortName)
: acc,
'',
);
// Example output: | 😀 – :grinning:| 🤦♂️ – :man_facepalming:| 🇷🇺 – :flag_ru:
`}
## Builders
Builders are the set of composable functions that help with creating ADF documents.
> **@atlaskit/adf-utils** provides builders for all nodes and marks listed in [Atlassian Document Format](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/)
specification.
### Usage:
${code`
import { doc, p, emoji } from '@atlaskit/adf-utils/builders.es'; // .es for ES2015
const adfDoc = doc(
p('My favourite emoji is ', emoji({ text: '🤦♂️', shortName: ':man_facepalming:' }), '. What is yours?'),
);
/**
* Produces following output:
*
* {
* "type": "doc",
* "version": 1,
* "content": [
* {
* "type": "paragraph",
* "content": [
* {
* "type": "text",
* "text": "My favourite emoji is "
* },
* {
* "type": "emoji",
* "attrs": {
* "text": "🤦♂️",
* "shortName": ":man_facepalming:"
* }
* },
* {
* "type": "text",
* "text": ". What is yours?"
* }
* ]
* }
* ]
* }
*/
`}
#### Aliases
There are aliases for some of the nodes and marks that match html tag names:
##### Nodes:
* paragraph -> p
* bulletList -> ul
* orderedList -> ol
* listItem -> li
* tableCell -> td
* tableHeader -> th
* tableRow -> tr
* hardBreak -> br
* rule -> hr
##### Marks:
* link -> a
* strong -> b
* underline -> u
`;
export default _default_1;