From 907eb998359943d57fc0dd77f83a84f4bb431573 Mon Sep 17 00:00:00 2001 From: yuin Date: Mon, 8 Nov 2021 16:13:54 +0900 Subject: [PATCH] Fixes #262 --- _test/extra.txt | 22 ++++++++++++++++++ parser/fcode_block.go | 3 +++ util/util.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/_test/extra.txt b/_test/extra.txt index f202b13..748a4ef 100644 --- a/_test/extra.txt +++ b/_test/extra.txt @@ -649,3 +649,25 @@ b

a
b

//= = = = = = = = = = = = = = = = = = = = = = = =// + + +51: Empty line in a fenced code block under list items +//- - - - - - - - -// +* This is a list item + ``` + This is a test + + This line will be dropped. + This line will be displayed. + ``` +//- - - - - - - - -// + +//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/parser/fcode_block.go b/parser/fcode_block.go index 7e44cec..5914138 100644 --- a/parser/fcode_block.go +++ b/parser/fcode_block.go @@ -90,6 +90,9 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C pos, padding := util.IndentPositionPadding(line, reader.LineOffset(), segment.Padding, fdata.indent) if pos < 0 { pos = util.FirstNonSpacePosition(line) + if pos < 0 { + pos = 0 + } padding = 0 } seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding) diff --git a/util/util.go b/util/util.go index 4d432d8..a817ec6 100644 --- a/util/util.go +++ b/util/util.go @@ -180,6 +180,59 @@ func IndentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, pad return -1, -1 } +// DedentPosition dedents lines by the given width. +// +// Deprecated: This function has bugs. Use util.IndentPositionPadding and util.FirstNonSpacePosition. +func DedentPosition(bs []byte, currentPos, width int) (pos, padding int) { + if width == 0 { + return 0, 0 + } + w := 0 + l := len(bs) + i := 0 + for ; i < l; i++ { + if bs[i] == '\t' { + w += TabWidth(currentPos + w) + } else if bs[i] == ' ' { + w++ + } else { + break + } + } + if w >= width { + return i, w - width + } + return i, 0 +} + +// DedentPositionPadding dedents lines by the given width. +// This function is mostly same as DedentPosition except this function +// takes account into additional paddings. +// +// Deprecated: This function has bugs. Use util.IndentPositionPadding and util.FirstNonSpacePosition. +func DedentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, padding int) { + if width == 0 { + return 0, paddingv + } + + w := 0 + i := 0 + l := len(bs) + for ; i < l; i++ { + if bs[i] == '\t' { + w += TabWidth(currentPos + w) + } else if bs[i] == ' ' { + w++ + } else { + break + } + } + if w >= width { + return i - paddingv, w - width + } + return i - paddingv, 0 +} + // IndentWidth calculate an indent width for the given line. func IndentWidth(bs []byte, currentPos int) (width, pos int) { l := len(bs)