Skip to content

sivukhin/godjot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Mar 2, 2025
2d5cac0 · Mar 2, 2025

History

93 Commits
Dec 21, 2024
Jan 6, 2024
Mar 2, 2025
Mar 2, 2025
Jan 13, 2024
Feb 12, 2024
Feb 12, 2024
Mar 2, 2025
Feb 8, 2024
Feb 12, 2024
Mar 26, 2024
Mar 2, 2025
Mar 2, 2025
Jan 21, 2024

Repository files navigation

godjot

Go Report Card

Djot markup language parser implemented in Go language

Installation

You can install godjot as a standalone binary:

$> go install github.com/sivukhin/godjot@latest
$> echo '*Hello*, _world_' | godjot
<p><strong>Hello</strong>, <em>world</em></p>

Usage

godjot provides API to parse AST from djot string

var djot []byte
ast := djot_parser.BuildDjotAst(djot)

AST is loosely typed and described with following simple struct:

type TreeNode[T ~int] struct {
    Type       T                     // one of DjotNode options
    Attributes tokenizer.Attributes  // string attributes of node
    Children   []TreeNode[T]         // list of child
    Text       []byte                // not nil only for TextNode
}

You can transform AST to HTML with predefined set of rules:

content := djot_parser.NewConversionContext(
    "html", 
    djot_parser.DefaultConversionRegistry,
    map[djot_parser.DjotNode]djot_parser.Conversion{
        /*
            You can overwrite default conversion rules with custom map
            djot_parser.ImageNode: func(state djot_parser.ConversionState, next func(c djot_parser.Children)) {
                state.Writer.
                    OpenTag("figure").
                    OpenTag("img", state.Node.Attributes.Entries()...).
                    OpenTag("figcaption").
                    WriteString(state.Node.Attributes.Get(djot_parser.ImgAltKey)).
                    CloseTag("figcaption").
                    CloseTag("figure")
            }
        */
    }
).ConvertDjotToHtml(&html_writer.HtmlWriter{}, ast...)

This implementation passes all examples provided in the spec but can diverge from original javascript implementation in some cases.