From 3259c6dc4b5d49ad30e50b51e057f056f62d9c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Sun, 19 Aug 2018 00:03:03 +0300 Subject: [PATCH] A better fix for counting columns --- block.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/block.go b/block.go index 9d669bc..4c23ac4 100644 --- a/block.go +++ b/block.go @@ -818,7 +818,7 @@ func (p *Markdown) tableHeader(data []byte) (size int, columns []CellAlignFlags) if data[0] == '|' { colCount-- } - lastPipe := backupWindowsNewline(i, data) + lastPipe := backtrackAnyNewlines(data, i) if lastPipe < len(data) && data[lastPipe] == '|' && !isBackslashEscaped(data, lastPipe) { colCount-- } @@ -1652,3 +1652,12 @@ func backupWindowsNewline(i int, data []byte) int { } return i } + +// backtrackAnyNewlines decrements i until it hits any non-newline character or +// becomes zero. +func backtrackAnyNewlines(data []byte, i int) int { + for i > 0 && i < len(data) && iseol(data[i]) { + i-- + } + return i +}