Documentation
¶
Overview ¶
Package adf provides a goldmark renderer for Atlassian Document Format (ADF).
This package implements a custom renderer for the goldmark Markdown parser that outputs ADF JSON instead of HTML. ADF is the native document format used by Atlassian products like Jira Cloud and Confluence Cloud.
Build Requirements ¶
This package requires Go 1.25+ with the experimental json/v2 package:
GOEXPERIMENT=jsonv2 go build ./... GOEXPERIMENT=jsonv2 go test ./...
Basic Usage ¶
Use New to create a reusable goldmark instance:
md := adf.New()
var buf bytes.Buffer
if err := md.Convert([]byte("# Hello World"), &buf); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String())
With GFM Extensions ¶
Use NewWithGFM to enable GitHub Flavored Markdown extensions including tables, strikethrough, autolinks, and task lists:
md := adf.NewWithGFM()
Convenience Functions ¶
For simple one-off conversions, use Convert or ConvertWithGFM:
output, err := adf.Convert([]byte("# Hello"))
output, err := adf.ConvertWithGFM([]byte("| A | B |\n|---|---|"))
Configuration ¶
The renderer can be configured using functional options:
md := adf.New(
adf.WithTableLayout("wide"),
adf.WithImageHandler(customHandler),
)
Schema Validation ¶
The adfschema subpackage provides JSON Schema validation for ADF documents:
import "github.com/ajbeck/goldmark-adf/adfschema"
if err := adfschema.Validate(jsonBytes); err != nil {
log.Printf("Invalid ADF: %v", err)
}
Supported Markdown Features ¶
Block elements: headings, paragraphs, blockquotes, code blocks (fenced and indented), bullet lists, ordered lists, horizontal rules, and hard breaks.
Inline elements: bold, italic, inline code, links, and autolinks. Images are converted to links with the image URL.
GFM extensions (with NewWithGFM): tables, strikethrough, autolinks, and task lists (rendered as "[x]" or "[ ]" text prefixes).
Raw HTML is skipped as ADF does not support arbitrary HTML content.
Example ¶
This example demonstrates basic Markdown to ADF conversion using the convenience function adf.Convert.
output, err := adf.Convert([]byte("Hello **world**"))
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(string(output))
Output: { "version": 1, "type": "doc", "content": [ { "type": "paragraph", "content": [ { "type": "text", "text": "Hello " }, { "type": "text", "marks": [ { "type": "strong" } ], "text": "world" } ] } ] }
Example (ReusableInstance) ¶
This example demonstrates creating a reusable goldmark instance with adf.New. This approach is more efficient when converting multiple documents.
md := adf.New()
var buf bytes.Buffer
if err := md.Convert([]byte("# Title"), &buf); err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(buf.String())
Output: { "version": 1, "type": "doc", "content": [ { "type": "heading", "attrs": { "level": 1 }, "content": [ { "type": "text", "text": "Title" } ] } ] }
Example (WithGFM) ¶
This example demonstrates GFM (GitHub Flavored Markdown) support with adf.NewWithGFM, which enables tables, strikethrough, autolinks, and task lists.
md := adf.NewWithGFM()
var buf bytes.Buffer
if err := md.Convert([]byte("Hello ~~world~~"), &buf); err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(buf.String())
Output: { "version": 1, "type": "doc", "content": [ { "type": "paragraph", "content": [ { "type": "text", "text": "Hello " }, { "type": "text", "marks": [ { "type": "strike" } ], "text": "world" } ] } ] }
Index ¶
- func Convert(source []byte) ([]byte, error)
- func ConvertWithGFM(source []byte) ([]byte, error)
- func New(opts ...Option) goldmark.Markdown
- func NewRenderer(opts ...Option) renderer.NodeRenderer
- func NewWithGFM(opts ...Option) goldmark.Markdown
- type Config
- type Document
- type ImageHandler
- type Mark
- type Node
- func NewBlockCardNode(url string) *Node
- func NewBlockquote() *Node
- func NewBulletList() *Node
- func NewCaption() *Node
- func NewCodeBlock(language string) *Node
- func NewDateNode(timestamp string) *Node
- func NewDecisionItemNode(state string) *Node
- func NewDecisionListNode() *Node
- func NewEmbedCardNode(url string) *Node
- func NewEmojiNode(shortName string) *Node
- func NewExternalMedia(url, alt string) *Node
- func NewHardBreak() *Node
- func NewHeading(level int) *Node
- func NewInlineCardNode(url string) *Node
- func NewListItem() *Node
- func NewMediaSingle(layout string) *Node
- func NewMentionNode(id, displayName string) *Node
- func NewOrderedList(start int) *Node
- func NewPanelNode(panelType string) *Node
- func NewParagraph() *Node
- func NewPlaceholderNode(text string) *Node
- func NewRule() *Node
- func NewStatusNode(text, color string) *Node
- func NewTable() *Node
- func NewTableCell() *Node
- func NewTableHeader() *Node
- func NewTableRow() *Node
- func NewTaskItem(state string) *Node
- func NewTaskList() *Node
- func NewText(text string) *Node
- func NewTextWithMarks(text string, marks []Mark) *Node
- type Option
- type Renderer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
Convert is a convenience function that converts Markdown to ADF JSON. It creates a new goldmark instance for each call, which is suitable for simple use cases. For better performance with multiple conversions, create a goldmark instance with New() and reuse it.
func ConvertWithGFM ¶
ConvertWithGFM is like Convert but with GFM extensions enabled.
func NewRenderer ¶
func NewRenderer(opts ...Option) renderer.NodeRenderer
NewRenderer creates a new ADF renderer with the given options.
func NewWithGFM ¶
NewWithGFM creates a new goldmark.Markdown instance with GFM extensions enabled. This enables parsing of tables, strikethrough, autolinks, and task lists.
Types ¶
type Config ¶
type Config struct {
// ImageHandler specifies how to handle image nodes.
// By default, images are converted to links.
ImageHandler ImageHandler
// TableLayout specifies the default table layout.
// Valid values: "default", "center", "wide", "full-width"
TableLayout string
// ExternalMedia enables external media image handling.
// When true, images are rendered as mediaSingle nodes with external media.
// When false (default), images are converted to text with link marks.
ExternalMedia bool
// ImageLayout specifies the default layout for mediaSingle nodes.
// Valid values: "center", "wide", "full-width", "wrap-left", "wrap-right", "align-start", "align-end"
// Defaults to "center" if not specified.
ImageLayout string
}
Config holds configuration options for the ADF renderer.
type Document ¶
type Document struct {
Version int `json:"version"`
Type string `json:"type"`
Content []Node `json:"content"`
}
Document represents the root ADF document node.
Every ADF document has a version (always 1), a type of "doc", and a content array containing the top-level block nodes. Use NewDocument to create a properly initialized Document.
type ImageHandler ¶
ImageHandler is a function that handles image rendering.
type Mark ¶
Mark represents a mark applied to a text node.
Marks are used for inline formatting such as bold (NewStrongMark), italic (NewEmMark), code (NewCodeMark), strikethrough (NewStrikeMark), and links (NewLinkMark). Multiple marks can be applied to a single text node.
func NewLinkMark ¶
NewLinkMark creates a link mark with the given URL and optional title.
func NewSubSupMark ¶
NewSubSupMark creates a subscript or superscript mark. subType should be "sub" or "sup".
func NewTextColorMark ¶
NewTextColorMark creates a text color mark with a hex color code.
type Node ¶
type Node struct {
Type string `json:"type"`
Attrs map[string]any `json:"attrs,omitempty"`
Content []Node `json:"content,omitempty"`
Marks []Mark `json:"marks,omitempty"`
Text string `json:"text,omitempty"`
}
Node represents an ADF node. It uses a flexible structure that can represent any node type in ADF, including block nodes (paragraph, heading, codeBlock), inline nodes (hardBreak), and text nodes.
Block and inline nodes use the Content field to hold child nodes. Text nodes use the Text field for their content and may have Marks applied. The Attrs field holds type-specific attributes like heading level or link href.
Use the constructor functions (e.g., NewParagraph, NewHeading, NewText) to create properly initialized nodes.
func NewBlockCardNode ¶
NewBlockCardNode creates a new blockCard node.
func NewCaption ¶
func NewCaption() *Node
NewCaption creates a new caption node for use within mediaSingle.
func NewCodeBlock ¶
NewCodeBlock creates a new code block node with an optional language.
func NewDateNode ¶
NewDateNode creates a new date inline node.
func NewDecisionItemNode ¶
NewDecisionItem creates a new decisionItem node.
func NewDecisionListNode ¶
func NewDecisionListNode() *Node
NewDecisionList creates a new decisionList node.
func NewEmbedCardNode ¶
NewEmbedCardNode creates a new embedCard node.
func NewEmojiNode ¶
NewEmojiNode creates a new emoji inline node.
func NewExternalMedia ¶
NewExternalMedia creates a new media node for external URLs.
func NewHeading ¶
NewHeading creates a new heading node with the specified level (1-6).
func NewInlineCardNode ¶
NewInlineCardNode creates a new inlineCard node.
func NewMediaSingle ¶
NewMediaSingle creates a new mediaSingle container node with the specified layout. Valid layouts: "center", "wide", "full-width", "wrap-left", "wrap-right", "align-start", "align-end"
func NewMentionNode ¶
NewMentionNode creates a new mention inline node.
func NewOrderedList ¶
NewOrderedList creates a new ordered list node with an optional start number.
func NewPanelNode ¶
NewPanel creates a new panel block node.
func NewPlaceholderNode ¶
NewPlaceholderNode creates a new placeholder inline node.
func NewStatusNode ¶
NewStatus creates a new status inline node.
func NewTableHeader ¶
func NewTableHeader() *Node
NewTableHeader creates a new table header cell node.
func NewTextWithMarks ¶
NewTextWithMarks creates a new text node with marks.
func (*Node) AppendChild ¶
AppendChild appends a child node to this node's content.
func (*Node) AppendMark ¶
AppendMark appends a mark to this node's marks.
type Option ¶
type Option interface {
SetADFOption(*Config)
}
Option is a functional option for configuring the ADF renderer.
func WithExternalMedia ¶
WithExternalMedia enables or disables external media image handling. When enabled, images are rendered as mediaSingle nodes containing external media. When disabled (default), images are converted to text with link marks.
func WithImageHandler ¶
func WithImageHandler(handler ImageHandler) Option
WithImageHandler sets a custom image handler.
func WithImageLayout ¶
WithImageLayout sets the default layout for mediaSingle nodes. Valid values: "center", "wide", "full-width", "wrap-left", "wrap-right", "align-start", "align-end" Defaults to "center" if not specified.
func WithTableLayout ¶
WithTableLayout sets the default table layout. Valid values: "default", "center", "wide", "full-width"
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer is a goldmark renderer.NodeRenderer that outputs Atlassian Document Format (ADF) JSON.
The Renderer maintains internal state during the AST walk, using a node stack to track the current position in the ADF document tree and a mark stack to accumulate active text marks (bold, italic, links, etc.). This state is reset for each new document.
Use NewRenderer to create a Renderer, or use the higher-level New and NewWithGFM functions which configure a complete goldmark instance.
func (*Renderer) RegisterFuncs ¶
func (r *Renderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)
RegisterFuncs implements renderer.NodeRenderer.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package adfschema provides JSON Schema validation for Atlassian Document Format.
|
Package adfschema provides JSON Schema validation for Atlassian Document Format. |
|
Package astext defines AST node types for ADF round-trip markdown extensions.
|
Package astext defines AST node types for ADF round-trip markdown extensions. |
|
Command migrate-schema normalizes the vendored ADF schema to draft-07.
|
Command migrate-schema normalizes the vendored ADF schema to draft-07. |