[GH-ISSUE #564] MD013: Unicode character width #451

Open
opened 2026-03-03 01:27:02 +03:00 by kerem · 5 comments
Owner

Originally created by @m15a on GitHub (Aug 19, 2022).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/564

Writing texts using non-ASCII characters in markdown would be daily practice worldwide. One, like me, would want to lint markdown files written in non-ASCII characters.

Regarding MD013, the current implementation of markdownlint seems checking character count of each line, based on regular expression. However, character width of unicode characters varies. For example, CJK characters often have double width.

Imagine that you configured MD013 as

$ cat .markdownlint.yml
MD013:
  line_length: 40

and you have example.md:

# An old story

<!-- Line below is of length 56, so it will be warned by markdownlint. -->
Long, long ago there lived, an old man and an old woman.

<!-- Line below has total unicode width 70 but won't be warned as its character counts 35. -->
むかしむかし,あるところに,おじいさんとおばあさんがくらしていました。

It is expected that both lines 4 and 7 are warned by MD013. However,

$ markdownlint example.md
example.md:4:41 MD013/line-length Line length [Expected: 40; Actual: 56]

Related: https://github.com/psf/black/issues/1197

Originally created by @m15a on GitHub (Aug 19, 2022). Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/564 Writing texts using non-ASCII characters in markdown would be daily practice worldwide. One, like me, would want to lint markdown files written in non-ASCII characters. Regarding MD013, the current implementation of markdownlint seems checking character count of each line, based on regular expression. However, character width of unicode characters varies. For example, CJK characters often have double width. Imagine that you configured MD013 as ```console $ cat .markdownlint.yml MD013: line_length: 40 ``` and you have `example.md`: ```markdown # An old story <!-- Line below is of length 56, so it will be warned by markdownlint. --> Long, long ago there lived, an old man and an old woman. <!-- Line below has total unicode width 70 but won't be warned as its character counts 35. --> むかしむかし,あるところに,おじいさんとおばあさんがくらしていました。 ``` It is expected that both lines 4 and 7 are warned by MD013. However, ```console $ markdownlint example.md example.md:4:41 MD013/line-length Line length [Expected: 40; Actual: 56] ``` Related: https://github.com/psf/black/issues/1197
Author
Owner

@DavidAnson commented on GitHub (Aug 19, 2022):

In my mind, rules about line length are meant to ensure that everything fits on the screen or that line lengths are consistent when using a monospaced font. With that understanding, the current implementation of this rule seems correct: it counts the number of visible characters. Trying to base a rule like this on the underlying encoding/representation of the data does not seem generally useful.

Something that complicates things is shown in your example: some characters may render wider then the default monospace character width. However, that is a rendering behavior that will vary by font, program, and operating system - and does not seem like it could be addressed by a rule.

As such, I feel the current implementation is valid.

<!-- gh-comment-id:1221032033 --> @DavidAnson commented on GitHub (Aug 19, 2022): In my mind, rules about line length are meant to ensure that everything fits on the screen or that line lengths are consistent when using a monospaced font. With that understanding, the current implementation of this rule seems correct: it counts the number of visible characters. Trying to base a rule like this on the underlying encoding/representation of the data does not seem generally useful. Something that complicates things is shown in your example: some characters may render wider then the default monospace character width. However, that is a rendering behavior that will vary by font, program, and operating system - and does not seem like it could be addressed by a rule. As such, I feel the current implementation is valid.
Author
Owner

@m15a commented on GitHub (Aug 20, 2022):

In my mind, rules about line length are meant to ensure that everything fits on the screen or that line lengths are consistent when using a monospaced font.

Agree, but displayed line lengths are inconsistent when writing CJK text since majority of monospace CJK fonts are actually duospaced.

However, that is a rendering behavior that will vary by font, program, and operating system - and does not seem like it could be addressed by a rule.

Probably I want an option, rather than changing a rule, to customize how to measure line length, based on either the number of characters or unicode width.

<!-- gh-comment-id:1221202473 --> @m15a commented on GitHub (Aug 20, 2022): > In my mind, rules about line length are meant to ensure that everything fits on the screen or that line lengths are consistent when using a monospaced font. Agree, but displayed line lengths are inconsistent when writing CJK text since majority of monospace CJK fonts are actually [duospaced](https://en.wikipedia.org/wiki/Duospaced_font). > However, that is a rendering behavior that will vary by font, program, and operating system - and does not seem like it could be addressed by a rule. Probably I want an option, rather than changing a rule, to customize how to measure line length, based on either the number of characters or unicode width.
Author
Owner

@DavidAnson commented on GitHub (Aug 20, 2022):

Do you know if there is a RegExp character class to identify "wide" characters?

<!-- gh-comment-id:1221203938 --> @DavidAnson commented on GitHub (Aug 20, 2022): Do you know if there is a RegExp character class to identify "wide" characters?
Author
Owner

@m15a commented on GitHub (Aug 20, 2022):

Hmm, no. I searched for and found cjk-regex, which matches CJK characters but not all CJK characters are necessarily double width (e.g., single width アイウエオ and double width アイウエオ).

<!-- gh-comment-id:1221215708 --> @m15a commented on GitHub (Aug 20, 2022): Hmm, no. I searched for and found [cjk-regex](https://www.npmjs.com/package/cjk-regex), which matches CJK characters but not all CJK characters are necessarily double width (e.g., single width `アイウエオ` and double width `アイウエオ`).
Author
Owner

@DavidAnson commented on GitHub (Aug 20, 2022):

This part of the Unicode spec seems relevant:

As do these packages:

However, I have a strict "no dependencies" rule and I don't see a clean way of referencing this data otherwise.

<!-- gh-comment-id:1221224150 --> @DavidAnson commented on GitHub (Aug 20, 2022): This part of the Unicode spec seems relevant: - https://unicode.org/reports/tr11/ As do these packages: - https://github.com/vangie/east-asian-width - https://github.com/susisu/meaw However, I have a strict "no dependencies" rule and I don't see a clean way of referencing this data otherwise.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/markdownlint#451
No description provided.