Skip to content

Commit

Permalink
Fix handling tab when removing trailing whitespace.
Browse files Browse the repository at this point in the history
Espacially in connection with ATX headers.
  • Loading branch information
mity committed Feb 25, 2024
1 parent 3848bfb commit 64f3680
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/md4c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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--;
}

Expand Down
21 changes: 21 additions & 0 deletions test/regressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
.
<h1>Foo</h1>
````````````````````````````````

```````````````````````````````` example [no-normalize]
Foo *bar
baz*→
====
.
<h1>Foo <em>bar
baz</em></h1>
````````````````````````````````

0 comments on commit 64f3680

Please sign in to comment.