From 10479a0204a2ad01b9aec8ab19fcc4d7c1b6a5c2 Mon Sep 17 00:00:00 2001 From: Carson Date: Mon, 26 Sep 2022 18:25:30 +0800 Subject: [PATCH] init Extender{renderer} (#2) --- v2/go.mod | 5 +++++ v2/main.go | 25 +++++++++++++++++++++++++ v2/renderer.go | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 v2/go.mod create mode 100644 v2/main.go create mode 100644 v2/renderer.go diff --git a/v2/go.mod b/v2/go.mod new file mode 100644 index 0000000..b29917e --- /dev/null +++ b/v2/go.mod @@ -0,0 +1,5 @@ +module github.com/CarsonSlovoka/goldmark-highlighting/v2 + +go 1.19 + +require github.com/yuin/goldmark v1.4.14 diff --git a/v2/main.go b/v2/main.go new file mode 100644 index 0000000..561b098 --- /dev/null +++ b/v2/main.go @@ -0,0 +1,25 @@ +package highlighting + +import ( + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" +) + +type highlightingExtender struct { +} + +func NewHighlightingExtender() goldmark.Extender { + return &highlightingExtender{} +} + +// Extend adds a hashtag parser to a Goldmark parser +func (e *highlightingExtender) Extend(m goldmark.Markdown) { + // m.Parser().AddOptions() // 如果您的擴展不需要再額外自己定義ast,那就不需要再定義Parser。 Parser只是在Parse的時候,會把相關屬性塞給該ast罷了,而codeblock運用的ast用已定義的ast.KindFencedCodeBlock即可,所以不需額外定義 + + m.Renderer().AddOptions( + renderer.WithNodeRenderers( + util.Prioritized(NewHighlightingHTMLRenderer(), 200), + ), + ) +} diff --git a/v2/renderer.go b/v2/renderer.go new file mode 100644 index 0000000..6ceb569 --- /dev/null +++ b/v2/renderer.go @@ -0,0 +1,22 @@ +package highlighting + +import ( + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" +) + +type highlightingHTMLRenderer struct{} + +func NewHighlightingHTMLRenderer() renderer.NodeRenderer { + return &highlightingHTMLRenderer{} +} + +func (r *highlightingHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { + reg.Register(ast.KindFencedCodeBlock, r.nodeRendererFunc) // codeblock不需要再定義額外的ast去描述,基於原始的KindFencedCodeBlock增加額外的判斷即可 +} + +// nodeRendererFunc 此函數可以把結果直接寫到writer之中,就能對輸出產生影響 +func (r *highlightingHTMLRenderer) nodeRendererFunc(writer util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { + return 0, nil +}