Java bindings for mq — a jq-like command-line tool for Markdown processing.
- Java 11+
- Maven
- Rust toolchain (for building the native library)
Clone and build the native library, then compile the Java bindings:
make buildOr step by step:
make setup # Clone the mq repository
make build-rust # Build the mq-ffi native library
mvn compile # Compile the Java sourcesmake testtry (Mq mq = new Mq()) {
MqResult result = mq.run(".h1", "# Hello World\n\nSome text");
System.out.println(result.text()); // "# Hello World"
}try (Mq mq = new Mq()) {
// Markdown (default)
MqResult md = mq.run(".h2", content, InputFormat.MARKDOWN);
// MDX
MqResult mdx = mq.run("select(is_mdx())", content, InputFormat.MDX);
// HTML (auto-converted to Markdown)
MqResult html = mq.run(".h1", "<h1>Title</h1>", InputFormat.HTML);
// Plain text (split by lines)
MqResult text = mq.run("select(contains(\"foo\"))", content, InputFormat.TEXT);
}String markdown = Mq.htmlToMarkdown("<h1>Hello</h1><p>World</p>");With options:
ConversionOptions options = new ConversionOptions();
options.useTitleAsH1 = true;
options.generateFrontMatter = true;
options.extractScriptsAsCodeBlocks = true;
String markdown = Mq.htmlToMarkdown(html, options);MqResult result = mq.run(".h2", content);
// Get all values joined by newline
String text = result.text();
// Get as a list
List<String> values = result.values();
// Access by index
String first = result.get(0);
// Iterate
for (String value : result) {
System.out.println(value);
}| Method | Description |
|---|---|
run(String code, String content) |
Run a query on Markdown content |
run(String code, String content, InputFormat format) |
Run a query with a specified input format |
static htmlToMarkdown(String html) |
Convert HTML to Markdown |
static htmlToMarkdown(String html, ConversionOptions options) |
Convert HTML to Markdown with options |
close() |
Release native resources (AutoCloseable) |
| Value | Description |
|---|---|
MARKDOWN |
CommonMark / GFM (default) |
MDX |
Markdown with JSX |
HTML |
HTML (auto-converted to Markdown) |
TEXT |
Plain text, split by lines |
RAW |
Raw input, no parsing |
| Field | Type | Description |
|---|---|---|
extractScriptsAsCodeBlocks |
boolean |
Extract <script> tags as code blocks |
generateFrontMatter |
boolean |
Generate front matter from HTML <head> metadata |
useTitleAsH1 |
boolean |
Use <title> as an H1 heading |