Skip to content

Commit 3a137a1

Browse files
committed
Don't insert spacer when there's only one word.
1 parent e230985 commit 3a137a1

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

js/linkpurpose.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ class LinkPurpose {
340340
})
341341
}
342342

343+
const spacer = document.createElement('span');
344+
spacer.classList.add('link-purpose-spacer');
345+
spacer.textContent = '\u0020';
346+
343347
const markLinks = function () {
344348
if (!marks) {
345349
return
@@ -411,11 +415,8 @@ class LinkPurpose {
411415
const lastText = lastTextNode.textContent
412416
const lastWordRegex = /\s*\S+\s*$/g
413417
const lastWord = lastText.match(lastWordRegex)
414-
if (lastWord !== null) {
418+
if (lastWord) {
415419
// Wrap the last word in a span.
416-
const spacer = document.createElement('span');
417-
spacer.classList.add('link-purpose-spacer');
418-
spacer.textContent = '\u0020';
419420
const breakPreventer = document.createElement('span')
420421
breakPreventer.classList.add(LinkPurpose.options.noBreakClass)
421422
breakPreventer.textContent = lastWord[0].trim()
@@ -424,8 +425,15 @@ class LinkPurpose {
424425
breakPreventer.append(node);
425426
})
426427
}
427-
lastTextNode.textContent = lastText.substring(0, lastText.length - lastWord[0].length)
428-
lastTextNode.parentNode.append(spacer, breakPreventer)
428+
if (lastWord[0].length !== lastText.length) {
429+
// Only wrap last string, and insert a controlled width spacer.
430+
lastTextNode.textContent = lastText.substring(0, lastText.length - lastWord[0].length)
431+
lastTextNode.parentNode.append(spacer.cloneNode(true), breakPreventer)
432+
} else {
433+
// Wrap entire textContent.
434+
lastTextNode.textContent = '';
435+
lastTextNode.parentNode.append(breakPreventer)
436+
}
429437
if (trailingWhitespace.length > 0) {
430438
// Move whitespace out of link.
431439
trailingWhitespace.forEach(space => {

0 commit comments

Comments
 (0)