Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
teekennedy committed Feb 8, 2024
1 parent 96dcd23 commit 0cdef01
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Run linters
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
version: v1.56.0

test:
runs-on: ubuntu-latest
Expand Down
50 changes: 25 additions & 25 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ func (r *Renderer) renderBlockSeparator(node ast.Node, entering bool) ast.WalkSt
func (r *Renderer) renderAutoLink(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.AutoLink)
if entering {
r.rc.writer.Write([]byte("<"))
r.rc.writer.Write(n.URL(r.rc.source))
r.rc.writer.WriteBytes([]byte("<"))
r.rc.writer.WriteBytes(n.URL(r.rc.source))
} else {
r.rc.writer.Write([]byte(">"))
r.rc.writer.WriteBytes([]byte(">"))
}
return ast.WalkContinue
}
Expand Down Expand Up @@ -182,15 +182,15 @@ func (r *Renderer) renderHeading(node ast.Node, entering bool) ast.WalkStatus {

func (r *Renderer) renderATXHeading(node *ast.Heading, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.Write(bytes.Repeat([]byte("#"), node.Level))
r.rc.writer.WriteBytes(bytes.Repeat([]byte("#"), node.Level))
// Only print space after heading if non-empty
if node.HasChildren() {
r.rc.writer.Write([]byte(" "))
r.rc.writer.WriteBytes([]byte(" "))
}
} else {
if r.config.HeadingStyle == HeadingStyleATXSurround {
r.rc.writer.Write([]byte(" "))
r.rc.writer.Write(bytes.Repeat([]byte("#"), node.Level))
r.rc.writer.WriteBytes([]byte(" "))
r.rc.writer.WriteBytes(bytes.Repeat([]byte("#"), node.Level))
}
}
return ast.WalkContinue
Expand All @@ -213,8 +213,8 @@ func (r *Renderer) renderSetextHeading(node *ast.Heading, entering bool) ast.Wal
}
}
}
r.rc.writer.Write([]byte("\n"))
r.rc.writer.Write(bytes.Repeat(underlineChar, underlineWidth))
r.rc.writer.WriteBytes([]byte("\n"))
r.rc.writer.WriteBytes(bytes.Repeat(underlineChar, underlineWidth))
return ast.WalkContinue
}

Expand All @@ -228,7 +228,7 @@ func (r *Renderer) renderThematicBreak(node ast.Node, entering bool) ast.WalkSta
} else {
breakLen = int(r.config.ThematicBreakLength)
}
r.rc.writer.Write(bytes.Repeat(breakChar, breakLen))
r.rc.writer.WriteBytes(bytes.Repeat(breakChar, breakLen))
}
return ast.WalkContinue
}
Expand All @@ -245,10 +245,10 @@ func (r *Renderer) renderCodeBlock(node ast.Node, entering bool) ast.WalkStatus

func (r *Renderer) renderFencedCodeBlock(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.FencedCodeBlock)
r.rc.writer.Write([]byte("```"))
r.rc.writer.WriteBytes([]byte("```"))
if entering {
if info := n.Info; info != nil {
r.rc.writer.Write(info.Text(r.rc.source))
r.rc.writer.WriteBytes(info.Text(r.rc.source))
}
r.rc.writer.FlushLine()
r.renderLines(node, entering)
Expand Down Expand Up @@ -315,7 +315,7 @@ func (r *Renderer) renderText(node ast.Node, entering bool) ast.WalkStatus {
if entering {
text := n.Text(r.rc.source)

r.rc.writer.Write(text)
r.rc.writer.WriteBytes(text)
if n.SoftLineBreak() {
r.rc.writer.EndLine()
}
Expand All @@ -327,7 +327,7 @@ func (r *Renderer) renderSegments(segments *text.Segments, asLines bool) {
for i := 0; i < segments.Len(); i++ {
segment := segments.At(i)
value := segment.Value(r.rc.source)
r.rc.writer.Write(value)
r.rc.writer.WriteBytes(value)
if asLines {
r.rc.writer.FlushLine()
}
Expand All @@ -350,40 +350,40 @@ func (r *Renderer) renderLink(node ast.Node, entering bool) ast.WalkStatus {
func (r *Renderer) renderImage(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Image)
if entering {
r.rc.writer.Write([]byte("!"))
r.rc.writer.WriteBytes([]byte("!"))
}
return r.renderLinkCommon(n.Title, n.Destination, entering)
}

func (r *Renderer) renderLinkCommon(title, destination []byte, entering bool) ast.WalkStatus {
if entering {
r.rc.writer.Write([]byte("["))
r.rc.writer.WriteBytes([]byte("["))
} else {
r.rc.writer.Write([]byte("]("))
r.rc.writer.Write(destination)
r.rc.writer.WriteBytes([]byte("]("))
r.rc.writer.WriteBytes(destination)
if len(title) > 0 {
r.rc.writer.Write([]byte(" \""))
r.rc.writer.Write(title)
r.rc.writer.Write([]byte("\""))
r.rc.writer.WriteBytes([]byte(" \""))
r.rc.writer.WriteBytes(title)
r.rc.writer.WriteBytes([]byte("\""))
}
r.rc.writer.Write([]byte(")"))
r.rc.writer.WriteBytes([]byte(")"))
}
return ast.WalkContinue
}

func (r *Renderer) renderCodeSpan(node ast.Node, entering bool) ast.WalkStatus {
if bytes.Count(node.Text(r.rc.source), []byte("`"))%2 != 0 {
r.rc.writer.Write([]byte("``"))
r.rc.writer.WriteBytes([]byte("``"))
} else {
r.rc.writer.Write([]byte("`"))
r.rc.writer.WriteBytes([]byte("`"))
}

return ast.WalkContinue
}

func (r *Renderer) renderEmphasis(node ast.Node, entering bool) ast.WalkStatus {
n := node.(*ast.Emphasis)
r.rc.writer.Write(bytes.Repeat([]byte{'*'}, n.Level))
r.rc.writer.WriteBytes(bytes.Repeat([]byte{'*'}, n.Level))
return ast.WalkContinue
}

Expand Down
12 changes: 8 additions & 4 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *markdownWriter) FlushLine() {

// EndLine ends the current line, flushing the line buffer regardless of whether it's empty.
func (m *markdownWriter) EndLine() {
m.Write([]byte{lineDelim})
_, _ = m.Write([]byte{lineDelim})
}

// PushPrefix adds the given bytes as a prefix for lines written to the output. The prefix
Expand Down Expand Up @@ -103,8 +103,12 @@ func (p *markdownWriter) PopPrefix() {
// Write writes the given data to an internal buffer, then writes any complete lines to the
// underlying writer.
func (m *markdownWriter) Write(data []byte) (n int, err error) {
return m.WriteBytes(data), m.err
}

func (m *markdownWriter) WriteBytes(data []byte) (n int) {
if m.err != nil {
return 0, m.err
return 0
}
// Writing to a bytes.Buffer always returns a nil error
n, _ = m.buf.Write(data)
Expand All @@ -127,12 +131,12 @@ func (m *markdownWriter) Write(data []byte) (n int, err error) {
_, err := m.output.Write(prefixedLine.Bytes())
if err != nil {
m.err = err
return 0, m.err
return 0
}
m.line += 1
prefixedLine.Reset()
}
return n, nil
return n
}

// Err returns the last write error, or nil.
Expand Down
23 changes: 14 additions & 9 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ func TestWrite(t *testing.T) {
assert.Equal(t, 0, available)
assert.Equal(t, 0, writer.Buffered())

writer.WriteByte(byte('b'))
writer.WriteRune('r')
writer.WriteString("s")
err := writer.WriteByte(byte('b'))
require.NoError(t, err)
_, err = writer.Write([]byte("y"))
require.NoError(t, err)
_, err = writer.WriteRune('t')
require.NoError(t, err)
_, err = writer.WriteString("e")
require.NoError(t, err)

assert.Less(t, available, writer.Available())
assert.Equal(t, 3, writer.Buffered())
assert.Equal(t, 4, writer.Buffered())

err := writer.Flush()
err = writer.Flush()
require.NoError(t, err)
assert.Equal(t, "brs\n", buf.String())
assert.Equal(t, "byte\n", buf.String())
}

// TestFlushLine tests that the writer will flush the current buffered line if non-empty.
Expand All @@ -38,7 +43,7 @@ func TestFlushLine(t *testing.T) {

writer.FlushLine()
assert.Equal("", buf.String(), "FlushLine() on an empty buffer should not produce output")
writer.Write([]byte("foobar"))
writer.WriteBytes([]byte("foobar"))
writer.FlushLine()
assert.Equal("foobar\n", buf.String(), "FlushLine() on partial line should produce output.")
}
Expand All @@ -51,7 +56,7 @@ func TestEndLine(t *testing.T) {

writer.EndLine()
assert.Equal("\n", buf.String(), "EndLine() should write newline to output")
writer.Write([]byte("A line"))
writer.WriteBytes([]byte("A line"))
assert.Equal("\n", buf.String(), "Writing a partial line should not produce output.")
writer.FlushLine()
assert.Equal("\nA line\n", buf.String(), "FlushLine() on partial line should produce output.")
Expand Down Expand Up @@ -100,7 +105,7 @@ func TestWriterOutputs(t *testing.T) {
func(writer *markdownWriter) {
lines := []string{"Consider me", "As one who loved poetry", "And persimmons."}
writer.PushPrefix([]byte(" "), 1)
writer.Write([]byte("- "))
writer.WriteBytes([]byte("- "))
for _, line := range lines {
writer.WriteLine([]byte(line))
}
Expand Down

0 comments on commit 0cdef01

Please sign in to comment.