SoFunction
Updated on 2025-03-02

Example of method to implement markdown parsing library in go language

Blackfriday is a Markdown processor implemented in Go. You can safely enter user-provided data, fast, support for universal extensions (tables, smart punctuation replacement, etc.), and is a safe input for all utf-8 (unicode).

Currently supports HTML output and Smartypants extension.

use

First of all, of course we need to introduce:

import /russross/blackfriday

Then

output := (input)

Here input is []byte type, you can force the string of markdown type to []byte, that is, input = []byte(string)

If you want to filter content you don't trust, use the following method:

Code:

package main

import (
  "fmt"

  "/microcosm-cc/bluemonday"
  "/russross/blackfriday"
)

func main() {
  input := []byte("### is a good go document website")  unsafe := (input)
  html := ().SanitizeBytes(unsafe)
  (string(html))
}

Basically these operations

My usage method is to directly convert the data submitted by the form through the above method when adding a new article, and store both the markdown and the converted content into the database.

However, when I rendered on the front end, there was another problem, that is, the html tag in the converted content will be displayed directly on the web page. To avoid this situation, I used a custom template function

  // Define template functions  func unescaped(x string) interface{} { return (x)}

  // Register template function  t := ("")
  t = ({"unescaped": unescaped})
  t, _ = ("templates/")
  (w, post)

  // Use template functions
  {{ .Content|unescaped }}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.