Skip to content

Commit 8f8a5c8

Browse files
author
Malte Legenhausen
committed
Merge branch 'jstewmon-fix-href-anchors'
2 parents 3540426 + 5781f8a commit 8f8a5c8

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ rules:
175175
quotes: 0
176176
require-jsdoc: 0
177177
semi-spacing: 0
178-
semi: 0
178+
semi: [error, always]
179179
sort-vars: 0
180180
space-after-keywords: 0
181181
space-before-blocks: 0

example/html-to-text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path');
22

33
var htmlToText = require('../lib/html-to-text');
44

5-
console.log('fromString:')
5+
console.log('fromString:');
66
var text = htmlToText.fromString('<h1>Hello World</h1>', {
77
wordwrap: 130
88
});

lib/formatter.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ function formatLineBreak(elem, fn, options) {
4343
}
4444

4545
function formatParagraph(elem, fn, options) {
46-
var paragraph = fn(elem.children, options)
46+
var paragraph = fn(elem.children, options);
4747
if (options.singleNewLineParagraphs) {
48-
return paragraph + '\n'
48+
return paragraph + '\n';
4949
} else {
50-
return paragraph + '\n\n'
50+
return paragraph + '\n\n';
5151
}
5252
}
5353

@@ -78,10 +78,10 @@ function formatAnchor(elem, fn, options) {
7878
if (!options.ignoreHref) {
7979
// Get the href, if present
8080
if (elem.attribs && elem.attribs.href) {
81-
href = elem.attribs.href.replace(/^mailto\:/, '');
81+
href = elem.attribs.href.replace(/^mailto:/, '');
8282
}
8383
if (href) {
84-
if ((!options.noAnchorUrl) || (options.noAnchorUrl && href.indexOf('#') === -1)) {
84+
if ((!options.noAnchorUrl) || (options.noAnchorUrl && href[0] !== '#')) {
8585
if (options.linkHrefBaseUrl && href.indexOf('/') === 0) {
8686
href = options.linkHrefBaseUrl + href;
8787
}
@@ -145,12 +145,12 @@ function formatOrderedList(elem, fn, options) {
145145
// TODO Imeplement the other valid types
146146
// Fallback to type '1' function for other valid types
147147
switch(olType) {
148-
case 'a': return function(start, i) { return String.fromCharCode(i + start + 97)};
149-
case 'A': return function(start, i) { return String.fromCharCode(i + start + 65)};
148+
case 'a': return function(start, i) { return String.fromCharCode(i + start + 97);};
149+
case 'A': return function(start, i) { return String.fromCharCode(i + start + 65);};
150150
case '1':
151-
default: return function(start, i) { return i + 1 + start};
151+
default: return function(start, i) { return i + 1 + start;};
152152
}
153-
}())
153+
}());
154154
// Make sure there are list items present
155155
if (nonWhiteSpaceChildren.length) {
156156
// Calculate initial start from ol attribute

lib/html-to-text.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function filterBody(dom, options, baseElement) {
6969
var documentClasses = elem.attribs && elem.attribs.class ? elem.attribs.class.split(" ") : [];
7070
var documentIds = elem.attribs && elem.attribs.id ? elem.attribs.id.split(" ") : [];
7171

72-
if ((splitTag.classes.every(function (val) { return documentClasses.indexOf(val) >= 0 })) &&
73-
(splitTag.ids.every(function (val) { return documentIds.indexOf(val) >= 0 }))) {
72+
if ((splitTag.classes.every(function (val) { return documentClasses.indexOf(val) >= 0; })) &&
73+
(splitTag.ids.every(function (val) { return documentIds.indexOf(val) >= 0; }))) {
7474
result = [elem];
7575
return;
7676
}
@@ -161,7 +161,7 @@ function walk(dom, options, result) {
161161
: walk(elem.children || [], options, result);
162162
break;
163163
case 'blockquote':
164-
result += format.blockquote(elem, walk, options)
164+
result += format.blockquote(elem, walk, options);
165165
break;
166166
default:
167167
result = walk(elem.children || [], options, result);

test/html-to-text.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ describe('html-to-text', function() {
195195
});
196196

197197
describe('a', function () {
198+
it('should decode html attribute entities from href', function () {
199+
var result = htmlToText.fromString('<a href="/foo?a&#x3D;b">test</a>');
200+
expect(result).to.equal('test [/foo?a=b]');
201+
});
202+
203+
it('should strip mailto: from email links', function () {
204+
var result = htmlToText.fromString('<a href="mailto:foo@example.com">email me</a>');
205+
expect(result).to.equal('email me [foo@example.com]');
206+
});
207+
198208
it('should return link with brackets', function () {
199209
var result = htmlToText.fromString('<a href="http://my.link">test</a>');
200210
expect(result).to.equal('test [http://my.link]');
@@ -366,7 +376,7 @@ describe('html-to-text', function() {
366376
}
367377
});
368378
expect(result).to.equal('====\ntest\n====');
369-
})
379+
});
370380
});
371381

372382
describe('Base element', function () {
@@ -608,7 +618,6 @@ describe('html-to-text', function() {
608618
var testString = 'foo<blockquote>test</blockquote>bar';
609619
var expectedResult = 'foo> test\nbar';
610620
expect(htmlToText.fromString(testString)).to.equal(expectedResult);
611-
})
612-
})
613-
621+
});
622+
});
614623
});

0 commit comments

Comments
 (0)