Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.38 KB

File metadata and controls

51 lines (38 loc) · 1.38 KB

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>