Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrapping on lines succeeded by unwrappable lines #228

Merged
merged 1 commit into from
Jul 7, 2022

Conversation

edwardloveall
Copy link
Contributor

When outputting as commonmark lines followed by a long, unwrappable
section would wrap incorrectly. For example:

item that doesn't need wrapping

a-long-line-that-won't-be-wrapped-because-there-is-no-character-it-can-break-on

When running this with the --to commonmark and --width 72 options:

comrak --to commonmark --width 72 some-markdown.md

It outputs the following:

item that doesn't need
wrapping

a-long-line-that-won't-be-wrapped-because-there-is-no-character-it-can-break-on

What was happening is that when a new line is needed, the algorithm
resets a bunch of variables like the current column, if we're at the
beginning of a line, etc. One of those variables is the position of the
last place we can break a line: the last non-space character. This is
stored in CommonMarkFormatters last_breakable field.

cmark-gfm resets this in render.c, and now so does comrak.

This also adds a new commonmark test type that doesn't write to HTML
to test commonmark specific features or bugs.

Fixes #227

When outputting as `commonmark` lines followed by a long, unwrappable
section would wrap incorrectly. For example:

```md
item that doesn't need wrapping

a-long-line-that-won't-be-wrapped-because-there-is-no-character-it-can-break-on
```

When running this with the `--to commonmark` and `--width 72` options:

```sh
comrak --to commonmark --width 72 some-markdown.md
```

It outputs the following:

```md
item that doesn't need
wrapping

a-long-line-that-won't-be-wrapped-because-there-is-no-character-it-can-break-on
```

What was happening is that when a new line is needed, the algorithm
resets a bunch of variables like the current column, if we're at the
beginning of a line, etc. One of those variables is the position of the
last place we can break a line: the last non-space character. This is
stored in `CommonMarkFormatter`s `last_breakable` field.

cmark-gfm [resets this][cmark-gfm-render] in `render.c`, and now so does comrak.

This also adds a new `commonmark` test type that doesn't write to HTML
to test commonmark specific features or bugs.

[cmark-gfm-render]: https://github.com/github/cmark-gfm/blob/f26f75ce5dbfeddf8c3e3df10a6f238f65f8d6ba/src/render.c#L60
@kivikakk
Copy link
Owner

kivikakk commented Jul 7, 2022

Wonderful — thank you so much!

@kivikakk kivikakk merged commit e23a579 into kivikakk:main Jul 7, 2022
@edwardloveall edwardloveall deleted the el-bad-wrap branch July 7, 2022 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lines wrap incorrectly when followed by a non-wrappable line
2 participants