Skip to content

Commit f1a7446

Browse files
committed
Format anchor tags more informatively.
1 parent 595313a commit f1a7446

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/formatter.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@ function formatHeading(elem, fn, options) {
2121
return fn(elem.children, options).toUpperCase() + '\n';
2222
}
2323

24+
// If we have both href and anchor text, format it in a useful manner:
25+
// - "anchor text [href]"
26+
// Otherwise if we have only anchor text or an href, we return the part we have:
27+
// - "anchor text" or
28+
// - "href"
2429
function formatAnchor(elem, fn, options) {
30+
var href = '';
31+
// Always get the anchor text
32+
var result = _s.strip(fn(elem.children || [], options));
33+
// Get the href, if present
2534
if (elem.attribs && elem.attribs.href) {
26-
return elem.attribs.href.replace(/^mailto\:/, '');
27-
}
28-
else {
29-
return helper.wordwrap(helper.decodeHTMLEntities(_s.strip(elem.raw)), options.wordwrap);
30-
}
35+
href = elem.attribs.href.replace(/^mailto\:/, '');
36+
}
37+
if (result && href) {
38+
result += ' [' + href + ']';
39+
}
40+
return formatText({ raw: result || href, needsSpace: elem.needsSpace }, options);
3141
};
3242

3343
function formatHorizontalLine(elem, fn, options) {

lib/html-to-text.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function walk(dom, options) {
7979
case 'tag':
8080
switch(elem.name.toLowerCase()) {
8181
case 'a':
82+
// Inline element needs a leading space if `result` currently
83+
// doesn't end with whitespace
84+
elem.needsSpace = whiteSpaceRegex.test(result);
8285
result += format.anchor(elem, walk, options);
8386
break;
8487
case 'p':

0 commit comments

Comments
 (0)