Skip to content

Commit 5cc9de2

Browse files
committed
fix: hyperlink truncation edge case
1 parent 9796e99 commit 5cc9de2

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-truncate",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A zero-dependency, vanilla JavaScript utility to truncate markdown text.",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const truncate = (text, limit, ellipsis) => {
118118

119119
let outputText = truncateString(text);
120120

121-
if (ellipsis) {
121+
if (ellipsis && outputText.length < text.length) {
122122
outputText += '...';
123123
}
124124

@@ -128,7 +128,7 @@ const truncate = (text, limit, ellipsis) => {
128128
module.exports = function (text = '', options = {}) {
129129
const { limit, ellipsis } = options || {}
130130

131-
if (isNaN(parseInt(limit, 10)) || text.length < limit) {
131+
if (isNaN(parseInt(limit, 10)) || text.length <= limit) {
132132
return text;
133133
}
134134

src/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ describe('truncateMarkdown test suite', () => {
5454
'a [hyperlink is](https://google.com)...',
5555
{ limit: 15, ellipsis: true },
5656
],
57+
[
58+
'in hyperlinks [only the label counts towards truncate limit](https://google.com)',
59+
'in hyperlinks [only the label counts towards truncate limit](https://google.com)',
60+
{ limit: 58, ellipsis: true },
61+
],
5762
['this www.google.com is also properly truncated', 'this www.goo...', { limit: 12, ellipsis: true }],
5863
[
5964
'"quotes" and other special <characters> remain unchanged',

0 commit comments

Comments
 (0)