diff --git a/parser/code_block.go b/parser/code_block.go index 8781bb0..4b18639 100644 --- a/parser/code_block.go +++ b/parser/code_block.go @@ -52,14 +52,7 @@ func (b *codeBlockParser) Continue(node ast.Node, reader text.Reader, pc Context // if code block line starts with a tab, keep a tab as it is. if segment.Padding != 0 { - offsetWithPadding := reader.LineOffset() - sl, ss := reader.Position() - reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop)) - if offsetWithPadding == reader.LineOffset() { - segment.Padding = 0 - segment.Start-- - } - reader.SetPosition(sl, ss) + preserveLeadingTabInCodeBlock(&segment, reader) } node.Lines().Append(segment) @@ -90,3 +83,14 @@ func (b *codeBlockParser) CanInterruptParagraph() bool { func (b *codeBlockParser) CanAcceptIndentedLine() bool { return true } + +func preserveLeadingTabInCodeBlock(segment *text.Segment, reader text.Reader) { + offsetWithPadding := reader.LineOffset() + sl, ss := reader.Position() + reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop)) + if offsetWithPadding == reader.LineOffset() { + segment.Padding = 0 + segment.Start-- + } + reader.SetPosition(sl, ss) +} diff --git a/parser/fcode_block.go b/parser/fcode_block.go index 75410d7..4801449 100644 --- a/parser/fcode_block.go +++ b/parser/fcode_block.go @@ -73,14 +73,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C fdata := pc.Get(fencedCodeBlockInfoKey).(*fenceData) // if code block line starts with a tab, keep a tab as it is. if segment.Padding != 0 { - offsetWithPadding := reader.LineOffset() - sl, ss := reader.Position() - reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop)) - if offsetWithPadding == reader.LineOffset() { - segment.Padding = 0 - segment.Start-- - } - reader.SetPosition(sl, ss) + preserveLeadingTabInCodeBlock(&segment, reader) } w, pos := util.IndentWidth(line, reader.LineOffset()) if w < 4 {