-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c20a6e1
commit 10479a0
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/CarsonSlovoka/goldmark-highlighting/v2 | ||
|
||
go 1.19 | ||
|
||
require github.com/yuin/goldmark v1.4.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |