Skip to content

Commit a3ffe56

Browse files
committed
Fix deprecated usage of node.Text
1 parent 21aa32d commit a3ffe56

File tree

3 files changed

+93
-16
lines changed

3 files changed

+93
-16
lines changed

.github/workflows/test.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ jobs:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: Install Go
19-
uses: actions/setup-go@v4
19+
uses: actions/setup-go@v5
2020
with:
2121
go-version: ${{ env.go_version }}
2222
- name: Checkout code
23-
uses: actions/checkout@v3
23+
uses: actions/checkout@v4
2424
- name: Run linters
25-
uses: golangci/golangci-lint-action@v3
25+
uses: golangci/golangci-lint-action@v6
2626
with:
27-
version: v1.56.0
27+
version: v1.62.2
2828

2929
test:
3030
runs-on: ubuntu-latest
3131
steps:
3232
- name: Install Go
3333
if: success()
34-
uses: actions/setup-go@v4
34+
uses: actions/setup-go@v5
3535
with:
3636
go-version: ${{ env.go_version }}
3737
- name: Checkout code
38-
uses: actions/checkout@v3
38+
uses: actions/checkout@v4
3939
- name: Run tests
4040
run: go test -v -covermode=count -coverprofile=coverage.out
4141
- name: Send coverage

renderer.go

+71-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8+
"slices"
89
"sync"
10+
"unicode"
911

1012
"github.com/yuin/goldmark/ast"
1113
"github.com/yuin/goldmark/renderer"
@@ -243,7 +245,7 @@ func (r *Renderer) renderFencedCodeBlock(node ast.Node, entering bool) ast.WalkS
243245
r.rc.writer.WriteBytes([]byte("```"))
244246
if entering {
245247
if info := n.Info; info != nil {
246-
r.rc.writer.WriteBytes(info.Text(r.rc.source))
248+
r.rc.writer.WriteBytes(info.Value(r.rc.source))
247249
}
248250
r.rc.writer.FlushLine()
249251
r.renderLines(node, entering)
@@ -310,7 +312,7 @@ func (r *Renderer) renderRawHTML(node ast.Node, entering bool) ast.WalkStatus {
310312
func (r *Renderer) renderText(node ast.Node, entering bool) ast.WalkStatus {
311313
n := node.(*ast.Text)
312314
if entering {
313-
text := n.Text(r.rc.source)
315+
text := n.Value(r.rc.source)
314316

315317
r.rc.writer.WriteBytes(text)
316318
if n.SoftLineBreak() {
@@ -369,10 +371,64 @@ func (r *Renderer) renderLinkCommon(title, destination []byte, entering bool) as
369371
}
370372

371373
func (r *Renderer) renderCodeSpan(node ast.Node, entering bool) ast.WalkStatus {
372-
if bytes.Count(node.Text(r.rc.source), []byte("`"))%2 != 0 {
373-
r.rc.writer.WriteBytes([]byte("``"))
374+
if entering {
375+
// get contents of codespan
376+
var contentBytes []byte
377+
for c := node.FirstChild(); c != nil; c = c.NextSibling() {
378+
text := c.(*ast.Text).Segment
379+
contentBytes = append(contentBytes, text.Value(r.rc.source)...)
380+
}
381+
contents := string(contentBytes)
382+
383+
//
384+
var beginsWithSpace bool
385+
var endsWithSpace bool
386+
var beginsWithBackTick bool
387+
var endsWithBackTick bool
388+
isOnlySpace := true
389+
backtickLengths := []int{}
390+
count := 0
391+
for i, c := range contents {
392+
if i == 0 {
393+
beginsWithSpace = unicode.IsSpace(c)
394+
beginsWithBackTick = c == '`'
395+
} else if i == len(contents)-1 {
396+
endsWithSpace = unicode.IsSpace(c)
397+
endsWithBackTick = c == '`'
398+
}
399+
if !unicode.IsSpace(c) {
400+
isOnlySpace = false
401+
}
402+
if c == '`' {
403+
count++
404+
} else if count > 0 {
405+
backtickLengths = append(backtickLengths, count)
406+
count = 0
407+
}
408+
}
409+
if count > 0 {
410+
backtickLengths = append(backtickLengths, count)
411+
}
412+
413+
// Surround the codespan with the minimum number of backticks required to contain the span.
414+
for i := 1; i <= len(contentBytes); i++ {
415+
if !slices.Contains(backtickLengths, i) {
416+
r.rc.codeSpanContext.backtickLength = i
417+
break
418+
}
419+
}
420+
r.rc.writer.WriteBytes(bytes.Repeat([]byte("`"), r.rc.codeSpanContext.backtickLength))
421+
422+
// Check if the code span needs to be padded with spaces
423+
if beginsWithSpace && endsWithSpace && !isOnlySpace || beginsWithBackTick || endsWithBackTick {
424+
r.rc.codeSpanContext.padSpace = true
425+
r.rc.writer.WriteBytes([]byte(" "))
426+
}
374427
} else {
375-
r.rc.writer.WriteBytes([]byte("`"))
428+
if r.rc.codeSpanContext.padSpace {
429+
r.rc.writer.WriteBytes([]byte(" "))
430+
}
431+
r.rc.writer.WriteBytes(bytes.Repeat([]byte("`"), r.rc.codeSpanContext.backtickLength))
376432
}
377433

378434
return ast.WalkContinue
@@ -389,14 +445,23 @@ type renderContext struct {
389445
// source is the markdown source
390446
source []byte
391447
// listMarkers is the marker character used for the current list
392-
lists []listContext
448+
lists []listContext
449+
codeSpanContext codeSpanContext
393450
}
394451

395452
type listContext struct {
396453
list *ast.List
397454
num int
398455
}
399456

457+
// codeSpanContext holds state about how the current codespan should be rendererd.
458+
type codeSpanContext struct {
459+
// number of backticks to use
460+
backtickLength int
461+
// whether to surround the codespan with spaces
462+
padSpace bool
463+
}
464+
400465
// newRenderContext returns a new renderContext object
401466
func newRenderContext(writer io.Writer, source []byte, config *Config) renderContext {
402467
return renderContext{

renderer_test.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,35 @@ func TestRenderedOutput(t *testing.T) {
193193
"`foo`",
194194
"`foo`\n",
195195
},
196+
{
197+
"Multiline code span",
198+
[]Option{},
199+
"`foo\nbar`",
200+
"`foo\nbar`\n",
201+
},
196202
{
197203
"Two-backtick code span",
198204
[]Option{},
199205
"``foo ` bar``",
200206
"``foo ` bar``\n",
201207
},
202208
{
203-
"Code span stripping leading and trailing spaces",
209+
"Reduced backtick code span",
210+
[]Option{},
211+
"``foo bar``",
212+
"`foo bar`\n",
213+
},
214+
{
215+
"Code span preserving leading and trailing spaces",
204216
[]Option{},
205217
"` `` `",
206-
"````\n",
218+
"` `` `\n",
207219
},
208220
{
209-
"Code span stripping one space",
221+
"Code span preserving surrounding spaces",
210222
[]Option{},
211223
"` `` `",
212-
"` `` `\n",
224+
"` `` `\n",
213225
},
214226
{
215227
"Unstrippable left space only",

0 commit comments

Comments
 (0)