htmltoadf is an HTML to Atlassian Document Format (ADF) converter written in Rust.
The library can be used in several different ways: * As a command line binary (either directly on a compatible host or using Docker) * Included as a library within a Rust project * Called from a different language or environment (e.g. C, JavaScript, Ruby, PHP, .NET etc.) using FFI * Called as a Web Assembly (wasm) module
See demo of the tool running as a WASM library entirely in the client here: https://wouterken.github.io/htmltoadf/
cargo install
$ cargo install htmltoadf
installing htmltoadf v0.1.5 (/usr/src/html2adf)
Updating crates.io index
Downloading crates ...
Downloaded lock_api v0.4.6
--snip--
Compiling htmltoadf v0.1.5
Finished release [optimized] target(s) in 1m 42s
Installing ~/.cargo/bin/htmltoadf
Installed package `htmltoadf v0.1.5` (executable `html2adf`)
Pre-built binaries can be downloaded from here: https://github.com/wouterken/htmltoadf/releases/tag/0.1.5
Docker Repo:
https://hub.docker.com/r/wouterken/html2adf
Usage
```bash $ echo "
Test
" | docker run --rm -i wouterken/html2adf:0.1.5 {"version":1,"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Hello world"},{"type":"text","text":"Test"}]}]}$ echo "
Test
" | docker run --rm -i wouterken/html2adf:0.1.5 | jq { "version": 1, "type": "doc", "content": [ { "type": "heading", "attrs": { "level": 1 }, "content": [ { "type": "text", "text": "Hello world" }, { "type": "text", "text": "Test" } ] } ] } ```Cargo.toml
toml
[dependencies]
htmltoadf = "0.1.5"
Code
```rust use htmltoadf::converthtmlstrtoadfstr; use serdejson::json;
let converted = converthtmlstrtoadfstr("Hello World
".tostring());
let expected = json!({
"version": 1,
"type": "doc",
"content": [
{
"type": "heading",
"attrs": {
"level": 1
},
"content": [
{
"type": "text",
"text": "Hello World"
}
]
}
]
}).to_string();
assert_eq!(expected, converted); ```
Install package from npm
javascript
import init, {convert} from "htmltoadf";
init()
.then(() => {
alert(convert("<h1>Hello from WebAssembly</h1>"))
});
First compile the code as a library, e.g.:
bash
cargo build --lib --release
Then you can link to the library dynamic from any environment that supports a FFI. It is important to copy the dynamic library from the release directory, and provide a relative link to the library file from the FFI E.g.
```ruby require 'ffi'
module HTMLToADF extend FFI::Library ffilib 'libhtmltoadf' attachfunction :convert, [ :string ], :string end
puts HTMLToADF.convert("
```javascript var ffi = require('ffi-napi');
var htmltoadf = ffi.Library('libhtmltoadf', { 'convert': [ 'string', [ 'string' ] ] }); console.log(htmltoadf.convert("
```
```python from cffi import FFI ffi = FFI() lib = ffi.dlopen("libhtmltoadf") ffi.cdef("char * convert(char *);") print(ffi.string(lib.convert(b"
```
This converter only implements a subset of possible mappings between HTML and ADF. The following features are implemented: - [x] Headings - [x] Images - [x] Lists (ordered and unordered) - [x] Tables - [x] Text and Paragraphs - [x] Code - [ ] Fuzz Tests - [ ] Support for named CSS colors - [ ] Smart image sizing - [ ] Inline Cards - [ ] Panels - [ ] Emoji - [ ] In built JSON Schema Validation
Run cargo test
from the repository root.
Bug reports and pull requests are welcome on GitHub at https://github.com/wouterken/htmltoadf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.