Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adf-builder

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adf-builder

Atlassian Document Format Builder

  • 3.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.3K
decreased by-24.64%
Maintainers
1
Weekly downloads
 
Created
Source

Atlassian Document Format Builder (JavaScript)

npm npm

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

import { Document } from 'adf-builder'; // For TypeScript or ES6
// const { Document } = require('adf-builder'); // For node/commonjs

const doc = new Document();
doc.paragraph()
  .text('See the ')
  .link('documention', 'https://example.com')
  .text(' ')
  .emoji(':smile:');

Tagged Template Literals

import { document, emoji, link } from 'adf-builder'; // For TypeScript or ES6
// const { document, emoji, link } = require('adf-builder'); // For node/commonjs

const doc = document`See the ${link('documentation', 'https://example.com')} ${emoji(':smile:')}`;

Serialization

A document can be serialized in different ways:

const doc = new Document();
doc.toJSON();        // Returns the Atlassian Document Format structure
doc.toString();      // Returns a compact JSON string
JSON.stringify(doc); // 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 } = require('adf-builder');

const doc = new Document();
doc.paragraph()
  .text('Hello ')
  .mention(id, 'joe')
  .text(', please ')
  .em('carefully')
  .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 } = require('adf-builder');

const doc = new Document();
doc.paragraph()
  .text('Formatted', marks().color('#f0f0f0').strong());
Application Card
doc.applicationCard('Title')
  .background('https://example.com/bg.png')
  .link('https://example.com/something')
  .description('Some description')
  .detail()
    .title('Status')
    .text('Open')
    .icon({url: 'https://example.com/open.png', title: 'Not resolved yet'});
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.bulletList()
  .textItem('Do this first')
  .textItem('Do this second');

Similarly for lists of links:

doc.orderedList()
  .linkItem('Do this first', 'https://example.com/1')
  .linkItem('Do this second', 'https://example.com/1');

For more complex use cases, use:

const list = doc.bulletList();
list.item().paragraph().text('a'); // add more to the paragraph
list.item().paragraph().text('b'); // add more to the paragraph

FAQs

Package last updated on 26 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc