Skip to content

Commit 0b11961

Browse files
fix linter warnings and spelling errors
1 parent d7c29f3 commit 0b11961

11 files changed

+50
-28
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ go get github.com/JohannesKaufmann/html-to-markdown
1818
## Usage
1919

2020
```go
21-
import "github.com/JohannesKaufmann/html-to-markdown"
21+
import md "github.com/JohannesKaufmann/html-to-markdown"
2222

2323
converter := md.NewConverter("", true, nil)
2424

commonmark.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
var multipleSpacesR = regexp.MustCompile(` +`)
1717

1818
var commonmark = []Rule{
19-
Rule{
19+
{
2020
Filter: []string{"ul", "ol"},
2121
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
2222
parent := selec.Parent()
@@ -42,7 +42,7 @@ var commonmark = []Rule{
4242
return &content
4343
},
4444
},
45-
Rule{
45+
{
4646
Filter: []string{"li"},
4747
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
4848
if strings.TrimSpace(content) == "" {
@@ -68,7 +68,7 @@ var commonmark = []Rule{
6868
return String(prefix + content + "\n")
6969
},
7070
},
71-
Rule{
71+
{
7272
Filter: []string{"#text"},
7373
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
7474
text := selec.Text()
@@ -85,7 +85,7 @@ var commonmark = []Rule{
8585
return &text
8686
},
8787
},
88-
Rule{
88+
{
8989
Filter: []string{"p", "div"},
9090
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
9191
parent := goquery.NodeName(selec.Parent())
@@ -101,7 +101,7 @@ var commonmark = []Rule{
101101
return &content
102102
},
103103
},
104-
Rule{
104+
{
105105
Filter: []string{"h1", "h2", "h3", "h4", "h5", "h6"},
106106
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
107107
if strings.TrimSpace(content) == "" {
@@ -141,7 +141,7 @@ var commonmark = []Rule{
141141
return &text
142142
},
143143
},
144-
Rule{
144+
{
145145
Filter: []string{"strong", "b"},
146146
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
147147
// only use one bold tag if they are nested
@@ -162,7 +162,7 @@ var commonmark = []Rule{
162162
return &trimmed
163163
},
164164
},
165-
Rule{
165+
{
166166
Filter: []string{"i", "em"},
167167
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
168168
// only use one italic tag if they are nested
@@ -183,7 +183,7 @@ var commonmark = []Rule{
183183
return &trimmed
184184
},
185185
},
186-
Rule{
186+
{
187187
Filter: []string{"img"},
188188
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
189189
alt := selec.AttrOr("alt", "")
@@ -209,7 +209,7 @@ var commonmark = []Rule{
209209
return &text
210210
},
211211
},
212-
Rule{
212+
{
213213
Filter: []string{"a"},
214214
AdvancedReplacement: func(content string, selec *goquery.Selection, opt *Options) (AdvancedResult, bool) {
215215
// if there is no href, no link is used. So just return the content inside the link
@@ -270,7 +270,7 @@ var commonmark = []Rule{
270270
return AdvancedResult{Markdown: replacement, Footer: reference}, false
271271
},
272272
},
273-
Rule{
273+
{
274274
Filter: []string{"code"},
275275
Replacement: func(_ string, selec *goquery.Selection, opt *Options) *string {
276276
content := selec.Text()
@@ -280,7 +280,7 @@ var commonmark = []Rule{
280280
return &text
281281
},
282282
},
283-
Rule{
283+
{
284284
Filter: []string{"pre"},
285285
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
286286
codeElement := selec.Find("code")
@@ -301,20 +301,20 @@ var commonmark = []Rule{
301301
return &text
302302
},
303303
},
304-
Rule{
304+
{
305305
Filter: []string{"hr"},
306306
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
307307
text := "\n\n" + opt.HorizontalRule + "\n\n"
308308
return &text
309309
},
310310
},
311-
Rule{
311+
{
312312
Filter: []string{"br"},
313313
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
314314
return String("\n\n")
315315
},
316316
},
317-
Rule{
317+
{
318318
Filter: []string{"blockquote"},
319319
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
320320
content = strings.TrimSpace(content)
@@ -331,7 +331,7 @@ var commonmark = []Rule{
331331
return &text
332332
},
333333
},
334-
Rule{
334+
{
335335
Filter: []string{"noscript"},
336336
Replacement: func(content string, selec *goquery.Selection, opt *Options) *string {
337337
// for now remove the contents of noscript. But in the future we could

examples/add_rules/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
func main() {
13-
html := `Good sountrack <span class="bb_strike"> and cake</span>.`
14-
// -> `Good sountrack ~and cake~.`
13+
html := `Good soundtrack <span class="bb_strike"> and cake</span>.`
14+
// -> `Good soundtrack ~~and cake~~.`
1515

1616
/*
1717
We want to add a rule when a `span` tag has a class of `bb_strike`.
@@ -31,7 +31,7 @@ func main() {
3131
// Because of the space it is not recognized as strikethrough.
3232
// -> trim spaces at begin&end of string when inside strong/italic/...
3333
content = strings.TrimSpace(content)
34-
return md.String("~" + content + "~")
34+
return md.String("~~" + content + "~~")
3535
},
3636
}
3737

from.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ type Plugin func(conv *Converter) []Rule
237237
func (c *Converter) Use(plugins ...Plugin) *Converter {
238238
for _, plugin := range plugins {
239239
rules := plugin(c)
240-
c.AddRules(rules...) // TODO: for better perfomance only use one lock for all plugins
240+
c.AddRules(rules...) // TODO: for better performance only use one lock for all plugins
241241
}
242242
return c
243243
}

markdown.go

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var inlineElements = []string{ // -> https://developer.mozilla.org/de/docs/Web/H
6565
"button", "input", "label", "select", "textarea",
6666
}
6767

68+
// IsInlineElement can be used to check wether a node name (goquery.Nodename) is
69+
// an html inline element and not a block element. Used in the rule for the
70+
// p tag to check wether the text is inside a block element.
6871
func IsInlineElement(e string) bool {
6972
for _, element := range inlineElements {
7073
if element == e {
@@ -132,6 +135,8 @@ type Options struct {
132135
domain string
133136
}
134137

138+
// AdvancedResult is used for example for links. If you use LinkStyle:referenced
139+
// the link href is placed at the bottom of the generated markdown (Footer).
135140
type AdvancedResult struct {
136141
Header string
137142
Markdown string

plugin/confluence_code_block.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func ConfluenceCodeBlock() md.Plugin {
1414
return func(c *md.Converter) []md.Rule {
1515
character := "```"
1616
return []md.Rule{
17-
md.Rule{
17+
{
1818
Filter: []string{"ac:structured-macro"},
1919
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
2020
for _, node := range selec.Nodes {

plugin/table.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010

1111
// EXPERIMENTAL_Table converts a html table to markdown.
1212
var EXPERIMENTAL_Table = []md.Rule{
13-
md.Rule{ // TableCell
13+
{ // TableCell
1414
Filter: []string{"th", "td"},
1515
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
1616
return md.String(cell(content, selec))
1717
},
1818
},
19-
md.Rule{ // TableRow
19+
{ // TableRow
2020
Filter: []string{"tr"},
2121
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
2222
borderCells := ""

plugin/task_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TaskListItems() md.Plugin {
1010
return func(c *md.Converter) []md.Rule {
1111
return []md.Rule{
12-
md.Rule{
12+
{
1313
Filter: []string{"input"},
1414
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
1515
if !selec.Parent().Is("li") {

plugin/vimeo.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/PuerkitoBio/goquery"
1414
)
1515

16+
// Timeout for the http client
1617
var Timeout = time.Second * 10
1718
var netClient = &http.Client{
1819
Timeout: Timeout,
@@ -46,12 +47,15 @@ var vimeoID = regexp.MustCompile(`video\/(\d*)`)
4647

4748
type vimeoVariation int
4849

50+
// Configure how the Vimeo Plugin should display the video in markdown.
4951
const (
5052
VimeoOnlyThumbnail vimeoVariation = iota
5153
VimeoWithTitle
5254
VimeoWithDescription
5355
)
5456

57+
// EXPERIMENTAL_VimeoEmbed registers a rule (for iframes) and
58+
// returns a markdown compatible representation (link to video, ...).
5559
func EXPERIMENTAL_VimeoEmbed(variation vimeoVariation) md.Plugin {
5660
return func(c *md.Converter) []md.Rule {
5761
getVimeoData := func(id string) (*vimeoVideo, error) {
@@ -89,7 +93,7 @@ func EXPERIMENTAL_VimeoEmbed(variation vimeoVariation) md.Plugin {
8993
}
9094

9195
return []md.Rule{
92-
md.Rule{
96+
{
9397
Filter: []string{"iframe"},
9498
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
9599
src := selec.AttrOr("src", "")

plugin/youtube.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111

1212
var youtubeID = regexp.MustCompile(`youtube\.com\/embed\/([^\&\?\/]+)`)
1313

14+
// EXPERIMENTAL_YoutubeEmbed registers a rule (for iframes) and
15+
// returns a markdown compatible representation (link to video, ...).
1416
var EXPERIMENTAL_YoutubeEmbed = []md.Rule{
15-
md.Rule{
17+
{
1618
Filter: []string{"iframe"},
1719
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
1820
src := selec.AttrOr("src", "")

utils.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"golang.org/x/net/html"
1212
)
1313

14+
/*
15+
WARNING: The functions from this file can be used externally
16+
but there is no garanty that they will stay exported.
17+
*/
18+
19+
// CollectText returns the text of the node and all its children
1420
func CollectText(n *html.Node) string {
1521
text := &bytes.Buffer{}
1622
collectText(n, text)
@@ -26,7 +32,8 @@ func collectText(n *html.Node, buf *bytes.Buffer) {
2632
}
2733
}
2834

29-
// always have a space to the side to recognize the delimiter
35+
// AddSpaceIfNessesary adds spaces to the text based on the neighbors.
36+
// That makes sure that there is always a space to the side, to recognize the delimiter.
3037
func AddSpaceIfNessesary(selec *goquery.Selection, text string) string {
3138

3239
var prev string
@@ -92,6 +99,8 @@ func AddSpaceIfNessesary(selec *goquery.Selection, text string) string {
9299
return text
93100
}
94101

102+
// TrimpLeadingSpaces removes spaces from the beginning of a line
103+
// but makes sure that list items and code blocks are not affected.
95104
func TrimpLeadingSpaces(text string) string {
96105
parts := strings.Split(text, "\n")
97106
for i := range parts {
@@ -128,6 +137,7 @@ func TrimpLeadingSpaces(text string) string {
128137
return strings.Join(parts, "\n")
129138
}
130139

140+
// TrimTrailingSpaces removes unnecessary spaces from the end of lines.
131141
func TrimTrailingSpaces(text string) string {
132142
parts := strings.Split(text, "\n")
133143
for i := range parts {
@@ -143,6 +153,7 @@ func TrimTrailingSpaces(text string) string {
143153
// The same as `multipleNewLinesRegex`, but applies to escaped new lines inside a link `\n\`
144154
var multipleNewLinesInLinkRegex = regexp.MustCompile(`(\n\\){1,}`) // `([\n\r\s]\\)`
145155

156+
// EscapeMultiLine deals with multiline content inside a link
146157
func EscapeMultiLine(content string) string {
147158
content = strings.TrimSpace(content)
148159
content = strings.Replace(content, "\n", `\`+"\n", -1)
@@ -152,7 +163,7 @@ func EscapeMultiLine(content string) string {
152163
return content
153164
}
154165

155-
// Cal can be passed the content of a code block and it returns
166+
// CalculateCodeFence can be passed the content of a code block and it returns
156167
// how many fence characters (` or ~) should be used.
157168
//
158169
// This is useful if the html content includes the same fence characters

0 commit comments

Comments
 (0)