Atlassian Document Format Builder (JavaScript)
A library that simplifies building documents that follow the Atlassian Document Format structure.
Installation
Install the package using
npm i -S adf-builder
or
yarn add adf-builder
Usage
This package offers two ways of building documents:
- A fluent document builder interface with support for all node types
- A tag to be used with ES6 template literals for single-paragraph documents
Fluent Interface
; // For TypeScript or ES6// const { Document } = require('adf-builder'); // For node/commonjs const doc = ;doc text'See the ' link'documention' 'https://example.com' text' ' ;
Tagged Template Literals
; // For TypeScript or ES6// const { document, emoji, link } = require('adf-builder'); // For node/commonjs const doc = document`See the `;
Serialization
A document can be serialized in different ways:
const doc = ;doc; // Returns the Atlassian Document Format structuredoc; // Returns a compact JSON stringJSON; // Equivalent to 'doc.toString()', but can be used for pretty printing
Code completion
If you use the library with TypeScript or with an IDE that interprets the index.d.ts
file declared
in package.json
for JavaScript projects, you will get automatic code completion.
Examples
Simple paragraphs
In order to get an output like:
Hello @joe, please carefully read this contract
You would use:
const Document = ; const doc = ;doc text'Hello ' text', please ' text' read ' link'this contract' 'https://www.example.com/contract';
Text
The Paragraph
class has some convenience methods for text with a single mark like strong
, link
, etc.
If you need more than one mark, you can use the marks builder:
const Document marks = ; const doc = ;doc text'Formatted' color'#f0f0f0';
Application Card
doc background'https://example.com/bg.png' link'https://example.com/something' description'Some description' title'Status' text'Open' ;
Lists
For lists, there are some convenience methods that cover the simple cases. Consider a list like the following:
- Do this first
- Do this second
In order to create that list, you can use:
doc ;
Similarly for lists of links:
doc ;
For more complex use cases, use:
const list = doc;listtext'a'; // add more to the paragraphlisttext'b'; // add more to the paragraph