Skip to content

Commit 83c9a6a

Browse files
committed
Merge pull request #3 from yushi/adjust-header-for-wide-character
Adjust header for wide characters.
2 parents 7a6ce01 + 813654f commit 83c9a6a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Godeps

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"Comment": "null-125",
88
"Rev": "88fa3094706609347d6fa1e362d946bbc207fceb"
99
},
10+
{
11+
"ImportPath": "github.com/mattn/go-runewidth",
12+
"Comment": "travisish-12-g39104c7",
13+
"Rev": "39104c78546f11be4bbc161e39858258aac6d9b8"
14+
},
1015
{
1116
"ImportPath": "github.com/russross/blackfriday",
1217
"Comment": "v1.1-115-gf3ab184",

markdown/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"runtime"
1111
"strings"
1212

13+
"github.com/mattn/go-runewidth"
1314
"github.com/russross/blackfriday"
1415
)
1516

@@ -100,9 +101,11 @@ func (_ *markdownRenderer) Header(out *bytes.Buffer, text func() bool, level int
100101

101102
switch level {
102103
case 1:
103-
fmt.Fprint(out, "\n", strings.Repeat("=", out.Len()-textMarker))
104+
len := runewidth.StringWidth(out.String()[textMarker:])
105+
fmt.Fprint(out, "\n", strings.Repeat("=", len))
104106
case 2:
105-
fmt.Fprint(out, "\n", strings.Repeat("-", out.Len()-textMarker))
107+
len := runewidth.StringWidth(out.String()[textMarker:])
108+
fmt.Fprint(out, "\n", strings.Repeat("-", len))
106109
}
107110
out.WriteString("\n")
108111
}

markdown/main_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,42 @@ func Test(t *testing.T) {
191191
}
192192
}
193193

194+
func TestWideChar(t *testing.T) {
195+
input := []byte(`タイトル
196+
==
197+
198+
サブタイトル
199+
---
200+
201+
aaa/あああ
202+
----------
203+
`)
204+
205+
expected := []byte(`タイトル
206+
========
207+
208+
サブタイトル
209+
------------
210+
211+
aaa/あああ
212+
----------
213+
`)
214+
215+
output, err := markdown.Process("", input, nil)
216+
if err != nil {
217+
log.Fatalln(err)
218+
}
219+
220+
diff, err := diff(expected, output)
221+
if err != nil {
222+
log.Fatalln(err)
223+
}
224+
225+
if len(diff) != 0 {
226+
t.Errorf("Difference of %d lines:\n%s", bytes.Count(diff, []byte("\n")), string(diff))
227+
}
228+
}
229+
194230
// TODO: Factor out.
195231
func diff(b1, b2 []byte) (data []byte, err error) {
196232
f1, err := ioutil.TempFile("", "markdownfmt")

0 commit comments

Comments
 (0)