mark/renderer/mkDocsAdmonition.go

117 lines
3.3 KiB
Go
Raw Normal View History

feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
package renderer
import (
"fmt"
2025-11-28 14:07:28 +01:00
"strconv"
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
parser "github.com/stefanfritsch/goldmark-admonitions"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
)
// HeadingAttributeFilter defines attribute names which heading elements can have
var MkDocsAdmonitionAttributeFilter = html.GlobalAttributeFilter
// A Renderer struct is an implementation of renderer.NodeRenderer that renders
// nodes as (X)HTML.
type ConfluenceMkDocsAdmonitionRenderer struct {
html.Config
}
// NewConfluenceMkDocsAdmonitionRenderer creates a new instance of the ConfluenceRenderer
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
func NewConfluenceMkDocsAdmonitionRenderer(opts ...html.Option) renderer.NodeRenderer {
return &ConfluenceMkDocsAdmonitionRenderer{
Config: html.NewConfig(),
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
}
}
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *ConfluenceMkDocsAdmonitionRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(parser.KindAdmonition, r.renderMkDocsAdmonition)
}
// Define MkDocsAdmonitionType enum
type MkDocsAdmonitionType int
const (
AInfo MkDocsAdmonitionType = iota
ANote
AWarn
ATip
ANone
)
func (t MkDocsAdmonitionType) String() string {
return []string{"info", "note", "warning", "tip", "none"}[t]
}
func ParseMkDocsAdmonitionType(node ast.Node) MkDocsAdmonitionType {
n, ok := node.(*parser.Admonition)
if !ok {
return ANone
}
switch string(n.AdmonitionClass) {
case "info":
return AInfo
case "note":
return ANote
case "warning":
return AWarn
case "tip":
return ATip
default:
return ANone
}
}
// renderMkDocsAdmonition renders an admonition node as a Confluence structured macro.
// All admonitions (including nested ones) are rendered as Confluence macros.
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
func (r *ConfluenceMkDocsAdmonitionRenderer) renderMkDocsAdmonition(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*parser.Admonition)
admonitionType := ParseMkDocsAdmonitionType(node)
if entering && admonitionType != ANone {
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
prefix := fmt.Sprintf("<ac:structured-macro ac:name=\"%s\"><ac:parameter ac:name=\"icon\">true</ac:parameter><ac:rich-text-body>\n", admonitionType)
if _, err := writer.Write([]byte(prefix)); err != nil {
return ast.WalkStop, err
}
2025-11-28 14:07:28 +01:00
title, _ := strconv.Unquote(string(n.Title))
if title != "" {
titleHTML := fmt.Sprintf("<p><strong>%s</strong></p>\n", title)
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
if _, err := writer.Write([]byte(titleHTML)); err != nil {
return ast.WalkStop, err
}
}
return ast.WalkContinue, nil
}
if !entering && admonitionType != ANone {
feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: add test files fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels fix: add tests for stripnewline and droph1 chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline feat: add Support for converting Material for MkDocs Admonitions to Confluence Info Panels chore: rename Admontion to MkDocsAdmonition, remove annoying comments fix: import parser instead of copying the file chore: rename mkDocs renderer function fix: fix bug and pipeline chore: remove test for droph1 and stripNewLines fix: add admonitions to StripNewLines and dropH1 tests feat: add mkdocsadmonition as opt-in renderer and parser fix: fix unit tests
2025-02-18 17:19:39 +01:00
suffix := "</ac:rich-text-body></ac:structured-macro>\n"
if _, err := writer.Write([]byte(suffix)); err != nil {
return ast.WalkStop, err
}
return ast.WalkContinue, nil
}
return r.renderMkDocsAdmon(writer, source, node, entering)
}
func (r *ConfluenceMkDocsAdmonitionRenderer) renderMkDocsAdmon(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*parser.Admonition)
if entering {
if n.Attributes() != nil {
_, _ = w.WriteString("<blockquote")
html.RenderAttributes(w, n, MkDocsAdmonitionAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("<blockquote>\n")
}
} else {
_, _ = w.WriteString("</blockquote>\n")
}
return ast.WalkContinue, nil
}