ext

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2020 License: MIT Imports: 5 Imported by: 11

README

goldmark-fenced_codeblock_extension

Sync to Gitee version

A extension for goldmark to enhance fenced codeblock

ext.RenderMap{
    Language:       []string{"red", "green"},
    RenderFunction: Renderer,
}
func Renderer(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
	n := node.(*ast.FencedCodeBlock)
	language := string(n.Language(source))

    buf := bytes.NewBuffer([]byte{})
	l := n.Lines().Len()
	for i := 0; i < l; i++ {
		line := n.Lines().At(i)
		buf.Write(line.Value(source))
	}
    raw := buf.String()

    switch language {
        case "red":
            w.WriteString(fmt.Sprintf("<span style=\"color:red\">%s</span>", raw))
        case "green":
            w.WriteString(fmt.Sprintf("<span style=\"color:green\">%s</span>", raw))
        default: 
            w.WriteString(fmt.Sprintf("<span>%s</span>", raw))
    }
       
	return ast.WalkContinue, nil
}

Then, you can use this in markdown

 ```red
 RED
 ```

and get <span style="color:red">RED</span>

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Ext = NewExt()

Ext default Extension

Functions

func GetFencedCodeBlockRendererFunc

func GetFencedCodeBlockRendererFunc(r renderer.NodeRenderer) renderer.NodeRendererFunc

GetFencedCodeBlockRendererFunc get the fenced codeblock render function from a goldmark renderer

func GetRenderFunc

func GetRenderFunc(target ast.NodeKind, r renderer.NodeRenderer) renderer.NodeRendererFunc

GetRenderFunc get the render function from a goldmark renderer

func NewExt

func NewExt(maps ...RenderMap) goldmark.Extender

NewExt initial an extension for goldmark fenced codeblock

Types

type RenderMap

type RenderMap struct {
	Languages      []string // language name, for all language use "*"
	RenderFunction renderer.NodeRendererFunc
}

RenderMap languages and these renderer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL