From 64f36805b02a874baa467a980029782c686ade1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mit=C3=A1=C5=A1?= Date: Sun, 25 Feb 2024 16:24:50 +0100 Subject: [PATCH] Fix handling tab when removing trailing whitespace. Espacially in connection with ATX headers. --- CHANGELOG.md | 4 ++++ src/md4c.c | 10 +++++----- test/regressions.txt | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e02071b6..dbb6317c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ Fixes: same logic as other emphasis spans in respect to punctuation character and word boundaries. + - [#248](https://github.com/mity/md4c/issues/248): + Fix handling tab when removing trailing whitespace, especially in connection + with ATX headers. + ## Version 0.5.2 diff --git a/src/md4c.c b/src/md4c.c index 054c559e..3679526d 100644 --- a/src/md4c.c +++ b/src/md4c.c @@ -5250,10 +5250,10 @@ md_is_atxheader_line(MD_CTX* ctx, OFF beg, OFF* p_beg, OFF* p_end, unsigned* p_l *p_level = n; if(!(ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS) && off < ctx->size && - CH(off) != _T(' ') && CH(off) != _T('\t') && !ISNEWLINE(off)) + !ISBLANK(off) && !ISNEWLINE(off)) return FALSE; - while(off < ctx->size && CH(off) == _T(' ')) + while(off < ctx->size && ISBLANK(off)) off++; *p_beg = off; *p_end = off; @@ -6248,17 +6248,17 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end, /* But for ATX header, we should exclude the optional trailing mark. */ if(line->type == MD_LINE_ATXHEADER) { OFF tmp = line->end; - while(tmp > line->beg && CH(tmp-1) == _T(' ')) + while(tmp > line->beg && ISBLANK(tmp-1)) tmp--; while(tmp > line->beg && CH(tmp-1) == _T('#')) tmp--; - if(tmp == line->beg || CH(tmp-1) == _T(' ') || (ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS)) + if(tmp == line->beg || ISBLANK(tmp-1) || (ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS)) line->end = tmp; } /* Trim trailing spaces. */ if(line->type != MD_LINE_INDENTEDCODE && line->type != MD_LINE_FENCEDCODE && line->type != MD_LINE_HTML) { - while(line->end > line->beg && CH(line->end-1) == _T(' ')) + while(line->end > line->beg && ISBLANK(line->end-1)) line->end--; } diff --git a/test/regressions.txt b/test/regressions.txt index 7411206c..836b970a 100644 --- a/test/regressions.txt +++ b/test/regressions.txt @@ -740,3 +740,24 @@ copy "~user1/file" to "~user2/file" . --fstrikethrough ```````````````````````````````` + + +## [Issue 248](https://github.com/mity/md4c/issues/248) + +(These are in spec.txt, but we need the [no-normalize] flag in order to +catch the whitespace issues.) + +```````````````````````````````` example [no-normalize] +#→Foo +. +

Foo

+```````````````````````````````` + +```````````````````````````````` example [no-normalize] + Foo *bar +baz*→ +==== +. +

Foo bar +baz

+````````````````````````````````