Skip to content

Commit 4db36c7

Browse files
committed
[impl] support MarkDown
1 parent 57c9bd2 commit 4db36c7

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
# pastebin
22
PasteBin implement, written in Go.
3+
4+
5+
## 0 Quick Start
6+
7+
提供多语法解析为 `HTML` 的接口实现。
8+
9+
10+
## 1 MarkDown
11+
12+
引用包 [blackfriday.v2](https://godoc.org/gopkg.in/russross/blackfriday.v2) ,其 `GitHub` 地址在 [这里](https://github.com/russross/blackfriday)
13+
14+
> `bytes.Replace([]byte(input), []byte("\r"), nil, -1)` 消除 `\r` 换行带来的格式影响;
15+
>
16+
> `blackfriday.HardLineBreak` 参数旨在消除两行连成一行的情况;
17+

example/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
7+
"github.com/binacsgo/pastebin"
8+
)
9+
10+
func main() {
11+
bytes, err := ioutil.ReadFile("../README.md")
12+
if err != nil {
13+
panic(err)
14+
}
15+
output, _ := pastebin.ParseContentMD(string(bytes))
16+
fmt.Println(output)
17+
}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/binacsgo/pastebin
2+
3+
go 1.14
4+
5+
require (
6+
github.com/pmezard/go-difflib v1.0.0 // indirect
7+
github.com/russross/blackfriday/v2 v2.0.1
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
9+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
4+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
6+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=

interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pastebin
2+
3+
// PasteBin the interface def.
4+
type PasteBin interface {
5+
Syntax() string
6+
ParseContent(string) (string, error)
7+
}

markdown.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pastebin
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/russross/blackfriday/v2"
7+
)
8+
9+
var gMarkDownImpl *MarkDownImpl
10+
11+
func init() {
12+
gMarkDownImpl = new(MarkDownImpl)
13+
}
14+
15+
// ParseContentMD parse the markdown syntax directly
16+
func ParseContentMD(input string) (string, error) {
17+
return gMarkDownImpl.ParseContent(input)
18+
}
19+
20+
// MarkDownImpl the markdown impl.
21+
type MarkDownImpl struct{}
22+
23+
// Syntax return the syntax
24+
func (impl *MarkDownImpl) Syntax() string {
25+
return "markdown"
26+
}
27+
28+
// ParseContent return the
29+
func (impl *MarkDownImpl) ParseContent(input string) (string, error) {
30+
output := blackfriday.Run(
31+
bytes.Replace([]byte(input), []byte("\r"), nil, -1),
32+
blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.HardLineBreak),
33+
)
34+
return string(output), nil
35+
}

types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pastebin
2+
3+
import "time"
4+
5+
// PasteObj the definition
6+
type PasteObj struct {
7+
Poster string
8+
Syntax string // enum
9+
Content string
10+
TinyURL string
11+
Expiration time.Duration
12+
}

0 commit comments

Comments
 (0)