Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Decode entities for author, dek, and title.
  • Loading branch information
benubois committed Feb 15, 2019
commit c6732d01774066497a5b30b92e7ad4457af528c9
5 changes: 4 additions & 1 deletion src/cleaners/author.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { normalizeSpaces } from 'utils/text';
import { decodeEntities, normalizeSpaces } from 'utils/text';
import { CLEAN_AUTHOR_RE } from './constants';

// Take an author string (like 'By David Smith ') and clean it to
// just the name(s): 'David Smith'.
export default function cleanAuthor(author) {
// Convert HTML encoded entities back to into characters
author = decodeEntities(author);

return normalizeSpaces(author.replace(CLEAN_AUTHOR_RE, '$2').trim());
}
7 changes: 5 additions & 2 deletions src/cleaners/dek.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stripTags } from 'utils/dom';
import { excerptContent, normalizeSpaces } from 'utils/text';
import { excerptContent, decodeEntities, normalizeSpaces } from 'utils/text';

import { TEXT_LINK_RE } from './constants';

Expand All @@ -13,11 +13,14 @@ export default function cleanDek(dek, { $, excerpt }) {
if (excerpt && excerptContent(excerpt, 10) === excerptContent(dek, 10))
return null;

const dekText = stripTags(dek, $);
let dekText = stripTags(dek, $);

// Plain text links shouldn't exist in the dek. If we have some, it's
// not a good dek - bail.
if (TEXT_LINK_RE.test(dekText)) return null;

// Convert HTML encoded entities back to into characters
dekText = decodeEntities(dekText);

return normalizeSpaces(dekText.trim());
}
5 changes: 4 additions & 1 deletion src/cleaners/title.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stripTags } from 'utils/dom';
import { normalizeSpaces } from 'utils/text';
import { decodeEntities, normalizeSpaces } from 'utils/text';

import { TITLE_SPLITTERS_RE } from './constants';
import { resolveSplitTitle } from './index';
Expand All @@ -21,6 +21,9 @@ export default function cleanTitle(title, { url, $ }) {
}
}

// Convert HTML encoded entities back to into characters
title = decodeEntities(title);

// strip any html tags in the title text
return normalizeSpaces(stripTags(title, $).trim());
}
2 changes: 1 addition & 1 deletion src/extractors/generic/content/extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const GenericContentExtractor = {
return null;
}

return normalizeSpaces($.html(node, { decodeEntities: false }));
return normalizeSpaces($.html(node));
},
};

Expand Down
5 changes: 5 additions & 0 deletions src/utils/text/decode-entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cheerio from 'cheerio';

export default function decodeEntities(str) {
return cheerio.load(str, { decodeEntities: true }).text();
}
35 changes: 35 additions & 0 deletions src/utils/text/decode-entities.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert from 'assert';
import decodeEntities from './decode-entities';

describe('decodeEntities(str)', () => {
it('decodes html entities', () => {
const entityMap = {
'¢': `¢`,
'£': `£`,
'¥': `¥`,
'©': `©`,
'&': `&`,
'&#60;': `<`,
'&#62;': `>`,
'&#8364;': `€`,
'&amp;': `&`,
'&cent;': `¢`,
'&copy;': `©`,
'&euro;': `€`,
'&gt;': `>`,
'&lt;': `<`,
'&pound;': `£`,
'&reg;': `®`,
'&yen;': `¥`,
};
const entities = Object.keys(entityMap).join(' ');
const characters = Object.values(entityMap).join(' ');

assert.equal(decodeEntities(entities), characters);
});

it('Leaves non-ascii alone', () => {
const str = '德 😀 ě';
assert.equal(decodeEntities(str), str);
});
});
1 change: 1 addition & 0 deletions src/utils/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { default as articleBaseUrl } from './article-base-url';
export { default as hasSentenceEnd } from './has-sentence-end';
export { default as excerptContent } from './excerpt-content';
export { default as getEncoding } from './get-encoding';
export { default as decodeEntities } from './decode-entities';