1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blackfriday
import (
"testing"
"github.com/spf13/viper"
"github.com/gohugoio/hugo/markup/converter"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
"github.com/russross/blackfriday"
)
func TestGetMarkdownExtensionsMasksAreRemovedFromExtensions(t *testing.T) {
b := blackfriday_config.Default
b.Extensions = []string{"headerId"}
b.ExtensionsMask = []string{"noIntraEmphasis"}
actualFlags := getMarkdownExtensions(b)
if actualFlags&blackfriday.EXTENSION_NO_INTRA_EMPHASIS == blackfriday.EXTENSION_NO_INTRA_EMPHASIS {
t.Errorf("Masked out flag {%v} found amongst returned extensions.", blackfriday.EXTENSION_NO_INTRA_EMPHASIS)
}
}
func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
type data struct {
testFlag int
}
b := blackfriday_config.Default
b.Extensions = []string{""}
b.ExtensionsMask = []string{""}
allExtensions := []data{
{blackfriday.EXTENSION_NO_INTRA_EMPHASIS},
{blackfriday.EXTENSION_TABLES},
{blackfriday.EXTENSION_FENCED_CODE},
{blackfriday.EXTENSION_AUTOLINK},
{blackfriday.EXTENSION_STRIKETHROUGH},
// {blackfriday.EXTENSION_LAX_HTML_BLOCKS},
{blackfriday.EXTENSION_SPACE_HEADERS},
// {blackfriday.EXTENSION_HARD_LINE_BREAK},
// {blackfriday.EXTENSION_TAB_SIZE_EIGHT},
{blackfriday.EXTENSION_FOOTNOTES},
// {blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK},
{blackfriday.EXTENSION_HEADER_IDS},
// {blackfriday.EXTENSION_TITLEBLOCK},
{blackfriday.EXTENSION_AUTO_HEADER_IDS},
{blackfriday.EXTENSION_BACKSLASH_LINE_BREAK},
{blackfriday.EXTENSION_DEFINITION_LISTS},
}
actualFlags := getMarkdownExtensions(b)
for _, e := range allExtensions {
if actualFlags&e.testFlag != e.testFlag {
t.Errorf("Flag %v was not found in the list of extensions.", e)
}
}
}
func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
b := blackfriday_config.Default
b.Extensions = []string{"definitionLists"}
b.ExtensionsMask = []string{""}
actualFlags := getMarkdownExtensions(b)
if actualFlags&blackfriday.EXTENSION_DEFINITION_LISTS != blackfriday.EXTENSION_DEFINITION_LISTS {
t.Errorf("Masked out flag {%v} found amongst returned extensions.", blackfriday.EXTENSION_DEFINITION_LISTS)
}
}
func TestGetFlags(t *testing.T) {
b := blackfriday_config.Default
flags := getFlags(false, b)
if flags&blackfriday.HTML_USE_XHTML != blackfriday.HTML_USE_XHTML {
t.Errorf("Test flag: %d was not found amongs set flags:%d; Result: %d", blackfriday.HTML_USE_XHTML, flags, flags&blackfriday.HTML_USE_XHTML)
}
}
func TestGetAllFlags(t *testing.T) {
c := qt.New(t)
b := blackfriday_config.Default
type data struct {
testFlag int
}
allFlags := []data{
{blackfriday.HTML_USE_XHTML},
{blackfriday.HTML_FOOTNOTE_RETURN_LINKS},
{blackfriday.HTML_USE_SMARTYPANTS},
{blackfriday.HTML_SMARTYPANTS_QUOTES_NBSP},
{blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES},
{blackfriday.HTML_SMARTYPANTS_FRACTIONS},
{blackfriday.HTML_HREF_TARGET_BLANK},
{blackfriday.HTML_NOFOLLOW_LINKS},
{blackfriday.HTML_NOREFERRER_LINKS},
{blackfriday.HTML_SMARTYPANTS_DASHES},
{blackfriday.HTML_SMARTYPANTS_LATEX_DASHES},
}
b.AngledQuotes = true
b.Fractions = true
b.HrefTargetBlank = true
b.NofollowLinks = true
b.NoreferrerLinks = true
b.LatexDashes = true
b.PlainIDAnchors = true
b.SmartDashes = true
b.Smartypants = true
b.SmartypantsQuotesNBSP = true
actualFlags := getFlags(false, b)
var expectedFlags int
//OR-ing flags together...
for _, d := range allFlags {
expectedFlags |= d.testFlag
}
c.Assert(actualFlags, qt.Equals, expectedFlags)
}
func TestConvert(t *testing.T) {
c := qt.New(t)
p, err := Provider.New(converter.ProviderConfig{
Cfg: viper.New(),
})
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte("testContent")})
c.Assert(err, qt.IsNil)
c.Assert(string(b.Bytes()), qt.Equals, "<p>testContent</p>\n")
}
func TestGetHTMLRendererAnchors(t *testing.T) {
c := qt.New(t)
p, err := Provider.New(converter.ProviderConfig{
Cfg: viper.New(),
})
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{
DocumentID: "testid",
ConfigOverrides: map[string]interface{}{
"plainIDAnchors": false,
"footnotes": true,
},
})
c.Assert(err, qt.IsNil)
b, err := conv.Convert(converter.RenderContext{Src: []byte(`# Header
This is a footnote.[^1] And then some.
[^1]: Footnote text.
`)})
c.Assert(err, qt.IsNil)
s := string(b.Bytes())
c.Assert(s, qt.Contains, "<h1 id=\"header:testid\">Header</h1>")
c.Assert(s, qt.Contains, "This is a footnote.<sup class=\"footnote-ref\" id=\"fnref:testid:1\"><a href=\"#fn:testid:1\">1</a></sup>")
c.Assert(s, qt.Contains, "<a class=\"footnote-return\" href=\"#fnref:testid:1\"><sup>[return]</sup></a>")
}
|