Go bindings for mq — a jq-like query language for Markdown processing.
This package wraps the mq-ffi C library using cgo, providing a safe, idiomatic Go API for querying and transforming Markdown, MDX, HTML, and plain text content.
- Go 1.21+
mq-ffiC library installed on your system
go get github.com/harehare/mq-goengine, err := mq.New()
if err != nil {
log.Fatal(err)
}
defer engine.Close()
result, err := engine.Run(".h1", "# Hello World\n\nSome text")
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text()) // "# Hello World"// Plain text (split by lines)
result, err := engine.RunWithFormat(`select(contains("foo"))`, "foo\nbar\nbaz", mq.FormatText)
// MDX content
result, err := engine.RunWithFormat("select(is_mdx())", "# Title\n\n<Component />", mq.FormatMDX)
// HTML content (auto-converted to Markdown)
result, err := engine.RunWithFormat(`select(contains("Hello"))`, "<h1>Hello</h1>", mq.FormatHTML)markdown, err := mq.HTMLToMarkdown("<h1>Hello</h1><p>World</p>")
// With options
markdown, err := mq.HTMLToMarkdownWithOptions(html, mq.ConversionOptions{
UseTitleAsH1: true,
GenerateFrontMatter: true,
ExtractScriptsAsCodeBlocks: true,
})result, err := engine.Run(".h2", content)
result.Text() // all values joined by newline
result.Values() // []string of non-empty values
result.Len() // total number of values
result.Get(0) // value at index| Constant | Description |
|---|---|
FormatMarkdown |
Standard Markdown (CommonMark/GFM) — default |
FormatMDX |
Markdown with JSX support |
FormatText |
Plain text, split by lines |
FormatHTML |
HTML, auto-converted to Markdown |
.h1 # extract H1 headings
.h2 # extract H2 headings
.code # extract code blocks
.[] # iterate over all nodes
.h2 | select(contains("x")) # filter by content