fcrespo82 / fcrespo82/vscode-markdown-table-formatter

Wrong whitespace padding when formatting content with emojis

Open
#56 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
32
Forks
8
PR merge metrics
No merged PRs in 30d

Description

Trying to format a table with emoji results in wrong whitespace padding:

```
| First column | Second column | Third column |
|--------------|---------------|--------------|
| First row | ✅ | ❌ |
| Second row | ✅ | ❌ |
```

JavaScript assumes each Unicode character taking up 2 bytes, but some characters take up more bytes (for example emojis):

```js
// This is wrong
'✅'.length === 2
> true
```

Using [Intl.Segmenter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) fixes this issue:

```js
// Much better!
[...new Intl.Segmenter().segment('✅')].length
> 1

[...new Intl.Segmenter().segment('✅ hello world')].length
> 13
```

See also: [How to count the correct length of a string with emojis in javascript?](https://stackoverflow.com/a/54369605)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.