mirror of
https://github.com/status-im/markdown.git
synced 2025-02-22 08:08:18 +00:00
un-export some stuff from parser package
This commit is contained in:
parent
ec226b0d72
commit
832ef9be61
105
block_test.go
105
block_test.go
@ -1652,108 +1652,3 @@ func TestSpaceHeadings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
doTestsParam(t, tests, TestParams{extensions: parser.SpaceHeadings})
|
doTestsParam(t, tests, TestParams{extensions: parser.SpaceHeadings})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsFenceLine(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
tests := []struct {
|
|
||||||
data []byte
|
|
||||||
syntaxRequested bool
|
|
||||||
wantEnd int
|
|
||||||
wantMarker string
|
|
||||||
wantSyntax string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
data: []byte("```"),
|
|
||||||
wantEnd: 3,
|
|
||||||
wantMarker: "```",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: []byte("```\nstuff here\n"),
|
|
||||||
wantEnd: 4,
|
|
||||||
wantMarker: "```",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: []byte("```\nstuff here\n"),
|
|
||||||
syntaxRequested: true,
|
|
||||||
wantEnd: 4,
|
|
||||||
wantMarker: "```",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: []byte("stuff here\n```\n"),
|
|
||||||
wantEnd: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: []byte("```"),
|
|
||||||
syntaxRequested: true,
|
|
||||||
wantEnd: 3,
|
|
||||||
wantMarker: "```",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: []byte("``` go"),
|
|
||||||
syntaxRequested: true,
|
|
||||||
wantEnd: 6,
|
|
||||||
wantMarker: "```",
|
|
||||||
wantSyntax: "go",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
var syntax *string
|
|
||||||
if test.syntaxRequested {
|
|
||||||
syntax = new(string)
|
|
||||||
}
|
|
||||||
end, marker := parser.IsFenceLine(test.data, syntax, "```")
|
|
||||||
if got, want := end, test.wantEnd; got != want {
|
|
||||||
t.Errorf("got end %v, want %v", got, want)
|
|
||||||
}
|
|
||||||
if got, want := marker, test.wantMarker; got != want {
|
|
||||||
t.Errorf("got marker %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
if test.syntaxRequested {
|
|
||||||
if got, want := *syntax, test.wantSyntax; got != want {
|
|
||||||
t.Errorf("got syntax %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSanitizedAnchorName(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
text string
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
text: "This is a header",
|
|
||||||
want: "this-is-a-header",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "This is also a header",
|
|
||||||
want: "this-is-also-a-header",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "main.go",
|
|
||||||
want: "main-go",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "Article 123",
|
|
||||||
want: "article-123",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "<- Let's try this, shall we?",
|
|
||||||
want: "let-s-try-this-shall-we",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: " ",
|
|
||||||
want: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "Hello, 世界",
|
|
||||||
want: "hello-世界",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, test := range tests {
|
|
||||||
if got := parser.SanitizeAnchorName(test.text); got != test.want {
|
|
||||||
t.Errorf("SanitizedAnchorName(%q):\ngot %q\nwant %q", test.text, got, test.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -66,9 +66,9 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// SanitizeAnchorName returns a sanitized anchor name for the given text.
|
// sanitizeAnchorName returns a sanitized anchor name for the given text.
|
||||||
// Taken from https://github.com/shurcooL/sanitized_anchor_name/blob/master/main.go#L14:1
|
// Taken from https://github.com/shurcooL/sanitized_anchor_name/blob/master/main.go#L14:1
|
||||||
func SanitizeAnchorName(text string) string {
|
func sanitizeAnchorName(text string) string {
|
||||||
var anchorName []rune
|
var anchorName []rune
|
||||||
var futureDash = false
|
var futureDash = false
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
@ -303,7 +303,7 @@ func (p *Parser) prefixHeading(data []byte) int {
|
|||||||
}
|
}
|
||||||
if end > i {
|
if end > i {
|
||||||
if id == "" && p.extensions&AutoHeadingIDs != 0 {
|
if id == "" && p.extensions&AutoHeadingIDs != 0 {
|
||||||
id = SanitizeAnchorName(string(data[i:end]))
|
id = sanitizeAnchorName(string(data[i:end]))
|
||||||
}
|
}
|
||||||
block := &ast.Heading{
|
block := &ast.Heading{
|
||||||
HeadingID: id,
|
HeadingID: id,
|
||||||
@ -608,10 +608,10 @@ func (*Parser) isHRule(data []byte) bool {
|
|||||||
return n >= 3
|
return n >= 3
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data,
|
// sFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data,
|
||||||
// and returns the end index if so, or 0 otherwise. It also returns the marker found.
|
// and returns the end index if so, or 0 otherwise. It also returns the marker found.
|
||||||
// If syntax is not nil, it gets set to the syntax specified in the fence line.
|
// If syntax is not nil, it gets set to the syntax specified in the fence line.
|
||||||
func IsFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker string) {
|
func sFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker string) {
|
||||||
i, size := 0, 0
|
i, size := 0, 0
|
||||||
|
|
||||||
n := len(data)
|
n := len(data)
|
||||||
@ -712,7 +712,7 @@ func IsFenceLine(data []byte, syntax *string, oldmarker string) (end int, marker
|
|||||||
// If doRender is true, a final newline is mandatory to recognize the fenced code block.
|
// If doRender is true, a final newline is mandatory to recognize the fenced code block.
|
||||||
func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
|
func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
|
||||||
var syntax string
|
var syntax string
|
||||||
beg, marker := IsFenceLine(data, &syntax, "")
|
beg, marker := sFenceLine(data, &syntax, "")
|
||||||
if beg == 0 || beg >= len(data) {
|
if beg == 0 || beg >= len(data) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -725,7 +725,7 @@ func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
|
|||||||
// safe to assume beg < len(data)
|
// safe to assume beg < len(data)
|
||||||
|
|
||||||
// check for the end of the code block
|
// check for the end of the code block
|
||||||
fenceEnd, _ := IsFenceLine(data[beg:], nil, marker)
|
fenceEnd, _ := sFenceLine(data[beg:], nil, marker)
|
||||||
if fenceEnd != 0 {
|
if fenceEnd != 0 {
|
||||||
beg += fenceEnd
|
beg += fenceEnd
|
||||||
break
|
break
|
||||||
@ -1455,9 +1455,9 @@ func (p *Parser) paragraph(data []byte) int {
|
|||||||
// line: index of 1st char of current line
|
// line: index of 1st char of current line
|
||||||
// i: index of cursor/end of current line
|
// i: index of cursor/end of current line
|
||||||
var prev, line, i int
|
var prev, line, i int
|
||||||
tabSize := TabSizeDefault
|
tabSize := tabSizeDefault
|
||||||
if p.extensions&TabSizeEight != 0 {
|
if p.extensions&TabSizeEight != 0 {
|
||||||
tabSize = TabSizeDouble
|
tabSize = tabSizeDouble
|
||||||
}
|
}
|
||||||
// keep going until we find something to mark the end of the paragraph
|
// keep going until we find something to mark the end of the paragraph
|
||||||
for i < len(data) {
|
for i < len(data) {
|
||||||
@ -1504,7 +1504,7 @@ func (p *Parser) paragraph(data []byte) int {
|
|||||||
|
|
||||||
id := ""
|
id := ""
|
||||||
if p.extensions&AutoHeadingIDs != 0 {
|
if p.extensions&AutoHeadingIDs != 0 {
|
||||||
id = SanitizeAnchorName(string(data[prev:eol]))
|
id = sanitizeAnchorName(string(data[prev:eol]))
|
||||||
}
|
}
|
||||||
|
|
||||||
block := &ast.Heading{
|
block := &ast.Heading{
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
/*
|
|
||||||
Package parser implements parsing of markdown document into AST tree.
|
|
||||||
*/
|
|
||||||
package parser
|
|
@ -1,3 +1,6 @@
|
|||||||
|
/*
|
||||||
|
Package parser implements parser for markdown document generating AST (abstract syntax tree).
|
||||||
|
*/
|
||||||
package parser
|
package parser
|
||||||
|
|
||||||
// Callback functions for inline parsing. One such function is defined
|
// Callback functions for inline parsing. One such function is defined
|
||||||
@ -10,7 +13,7 @@ import (
|
|||||||
"github.com/gomarkdown/markdown/ast"
|
"github.com/gomarkdown/markdown/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Extensions is a bitmask of enabled Parser extensions.
|
// Extensions is a bitmask of enabled parser extensions.
|
||||||
type Extensions int
|
type Extensions int
|
||||||
|
|
||||||
// Bit flags representing markdown parsing extensions.
|
// Bit flags representing markdown parsing extensions.
|
||||||
@ -41,8 +44,8 @@ const (
|
|||||||
|
|
||||||
// The size of a tab stop.
|
// The size of a tab stop.
|
||||||
const (
|
const (
|
||||||
TabSizeDefault = 4
|
tabSizeDefault = 4
|
||||||
TabSizeDouble = 8
|
tabSizeDouble = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
// for each character that triggers a response when parsing inline data.
|
// for each character that triggers a response when parsing inline data.
|
||||||
|
110
parser/parser_test.go
Normal file
110
parser/parser_test.go
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsFenceLine(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
tests := []struct {
|
||||||
|
data []byte
|
||||||
|
syntaxRequested bool
|
||||||
|
wantEnd int
|
||||||
|
wantMarker string
|
||||||
|
wantSyntax string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
data: []byte("```"),
|
||||||
|
wantEnd: 3,
|
||||||
|
wantMarker: "```",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte("```\nstuff here\n"),
|
||||||
|
wantEnd: 4,
|
||||||
|
wantMarker: "```",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte("```\nstuff here\n"),
|
||||||
|
syntaxRequested: true,
|
||||||
|
wantEnd: 4,
|
||||||
|
wantMarker: "```",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte("stuff here\n```\n"),
|
||||||
|
wantEnd: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte("```"),
|
||||||
|
syntaxRequested: true,
|
||||||
|
wantEnd: 3,
|
||||||
|
wantMarker: "```",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte("``` go"),
|
||||||
|
syntaxRequested: true,
|
||||||
|
wantEnd: 6,
|
||||||
|
wantMarker: "```",
|
||||||
|
wantSyntax: "go",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
var syntax *string
|
||||||
|
if test.syntaxRequested {
|
||||||
|
syntax = new(string)
|
||||||
|
}
|
||||||
|
end, marker := sFenceLine(test.data, syntax, "```")
|
||||||
|
if got, want := end, test.wantEnd; got != want {
|
||||||
|
t.Errorf("got end %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
if got, want := marker, test.wantMarker; got != want {
|
||||||
|
t.Errorf("got marker %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if test.syntaxRequested {
|
||||||
|
if got, want := *syntax, test.wantSyntax; got != want {
|
||||||
|
t.Errorf("got syntax %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizedAnchorName(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "This is a header",
|
||||||
|
want: "this-is-a-header",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "This is also a header",
|
||||||
|
want: "this-is-also-a-header",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "main.go",
|
||||||
|
want: "main-go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Article 123",
|
||||||
|
want: "article-123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "<- Let's try this, shall we?",
|
||||||
|
want: "let-s-try-this-shall-we",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: " ",
|
||||||
|
want: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Hello, 世界",
|
||||||
|
want: "hello-世界",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
if got := sanitizeAnchorName(test.text); got != test.want {
|
||||||
|
t.Errorf("SanitizedAnchorName(%q):\ngot %q\nwant %q", test.text, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user