Skip to content

Commit b15e07d

Browse files
committed
Line wrap long strings in code blocks.
1 parent 8844d07 commit b15e07d

30 files changed

Lines changed: 7 additions & 3 deletions

cmd/md2pdf/md2pdf.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ func main() {
177177
pf := mdtopdf.NewPdfRenderer(params)
178178

179179
if *generateTOC == true {
180-
// we need to generate the TOC for `content`
181180
headers, err := mdtopdf.GetTOCEntries(content)
182181
if err != nil {
183182
log.Fatal(err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/gabriel-vasile/mimetype v1.4.8
99
github.com/gomarkdown/markdown v0.0.0-20250311123330-531bef5e742b
1010
github.com/jessp01/gohighlight v0.21.2
11+
github.com/mitchellh/go-wordwrap v1.0.1
1112
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
1213
)
1314

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/jessp01/gohighlight v0.21.2 h1:radLDWQMJeDwzn6b8cduio7kVXhy3zkYFsWxkr
1313
github.com/jessp01/gohighlight v0.21.2/go.mod h1:52r0Yxd1+T9f7uLenaO2/34K3gPOejxCxXwdNc/2Z8Y=
1414
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 h1:YocNLcTBdEdvY3iDK6jfWXvEaM5OCKkjxPKoJRdB3Gg=
1515
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=
16+
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
17+
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
1618
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1719
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1820
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

nodeProcessing.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/gabriel-vasile/mimetype"
3939
"github.com/gomarkdown/markdown/ast"
4040
highlight "github.com/jessp01/gohighlight"
41+
"github.com/mitchellh/go-wordwrap"
4142
)
4243

4344
func (r *PdfRenderer) processText(node *ast.Text) {
@@ -119,9 +120,10 @@ func (r *PdfRenderer) processCodeblock(node ast.CodeBlock) {
119120
}
120121
syntaxDef, _ := highlight.ParseDef(syntaxFile)
121122
h := highlight.NewHighlighter(syntaxDef)
122-
matches := h.HighlightString(string(node.Literal))
123+
linesWrapped := wordwrap.WrapString(string(node.Literal), 90)
124+
matches := h.HighlightString(linesWrapped)
123125
r.cr()
124-
lines := strings.Split(string(node.Literal), "\n")
126+
lines := strings.Split(linesWrapped, "\n")
125127
for lineN, l := range lines {
126128
colN := 0
127129
for _, c := range l {
0 Bytes
Binary file not shown.

testdata/Auto links.pdf

0 Bytes
Binary file not shown.

testdata/Backslash escapes.pdf

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

testdata/Code Blocks.pdf

0 Bytes
Binary file not shown.

testdata/Code Spans.pdf

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)