Skip to content

Commit

Permalink
refactor: remove unused util function
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Oct 25, 2020
1 parent fa80752 commit f54ea22
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 52 deletions.
18 changes: 0 additions & 18 deletions lib/util.js
Expand Up @@ -352,24 +352,6 @@ exports.addFormatMeta = format => {
};


/**
* Get only the string from an HTML string.
*
* @param {string} html
* @returns {string}
*/
exports.stripHTML = html => html
.replace(/[\n\r]/g, ' ')
.replace(/\s*<\s*br\s*\/?\s*>\s*/gi, '\n')
.replace(/<\s*\/\s*p\s*>\s*<\s*p[^>]*>/gi, '\n')
.replace(/<a\s+(?:[^>]*?\s+)?href=(?:["'])\/redirect.*?q=(.*?)(?:[&'"]).*?<\/a>/gi,
(_, p1) => decodeURIComponent(p1))
.replace(/<a\s+(?:[^>]*?\s+)?href=(?:["'])((?:https?|\/).*?)(?:['"]).*?<\/a>/gi,
(_, p1) => url.resolve('https://youtube.com/', p1))
.replace(/<.*?>/gi, '')
.trim();


/**
* Get a number from an abbreviated number string.
*
Expand Down
34 changes: 0 additions & 34 deletions test/util-test.js
Expand Up @@ -596,37 +596,3 @@ describe('util.parseAbbreviatedNumber', () => {
assert.strictEqual(util.parseAbbreviatedNumber('abc'), null);
});
});


describe('util.stripHTML()', () => {
it('Normal text with some html', () => {
const html = '<p>This page isn\'t available. Sorry about that.</p><p>Try searching for something else.</p>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'This page isn\'t available. Sorry about that.\nTry searching for something else.');
});
it('Redirect link in text', () => {
const html = '<a href="/redirect?q=https%3A%2F%2Ftwitter.com%2Flinustech&amp;redir_token=rJA12ePqgl4MjA4&amp"></a>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'https://twitter.com/linustech');
});
it('Youtube watch link in text', () => {
const html = '<a spellcheck="false" href="/watch?v=PKfxmFU3lWY" dir="auto">https://youtube.com/watch?v=PKfx...</a>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'https://youtube.com/watch?v=PKfxmFU3lWY');
});
it('Normal link in text', () => {
const html = '<a href="https://stackoverflow.com">stackoverflow.com</a>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'https://stackoverflow.com/');
});
it('Silent on malformed URI in link', () => {
const html = '<a href="http://%E0%A4%A">malformed</a>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'http://%E0%A4%A/');
});
it('Invalid html in text', () => {
const html = '<a href="#" <p/>Some text<div><div>';
const text = util.stripHTML(html);
assert.strictEqual(text, 'Some text');
});
});

0 comments on commit f54ea22

Please sign in to comment.