adf2html
A Rust library for converting Atlassian Document Format (ADF) documents into HTML. ADF is the rich-text format used by Atlassian products (Jira, Confluence) and is returned by the Atlassian v3 REST API.
Features
Supports the following ADF node types:
Block nodes: blockquote, bulletList, codeBlock, expand, heading, mediaGroup, mediaSingle, orderedList, panel, paragraph, rule, table
Inline nodes: date, emoji, hardBreak, inlineCard, mention, status, text
Marks: code, em, link, strike, strong, subsup, textColor, underline
Installation
Add this to your Cargo.toml:
[]
= "1.0"
Usage
Basic conversion
Deserialize an ADF JSON payload into a Document and call to_html:
use Document;
let adf_json = r#"{
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello, world!",
"marks": [{ "type": "strong" }]
}
]
}
]
}"#;
let document: Document = from_str.unwrap;
// Pass an optional timezone for rendering date nodes, and a base URL used
// for rendering inline mention/card links.
let html = document.to_html;
println!;
// <p><strong>Hello, world!</strong></p>
Specifying a timezone for date nodes
ADF date nodes store a UTC timestamp. Pass a chrono_tz::Tz value to render dates in a specific timezone:
use Document;
use New_York;
let document: Document = from_str.unwrap;
let html = document.to_html;
Replacing media URLs
When an ADF document contains media attachments, the Atlassian API returns relative /rest/api/3/attachment/content/<id> paths. After fetching the rendered HTML you can rewrite those paths to absolute URLs pointing to your instance:
use Document;
let mut document: Document = from_str.unwrap;
let html = document.to_html;
// Rewrites all attachment paths found in `html` to absolute URLs.
document.replace_media_urls;
// Render again with the updated URLs.
let html_with_media = document.to_html;
License
MIT