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
Next Next commit
Initial support for ignoring entities.
  • Loading branch information
benubois committed Feb 13, 2019
commit 7e712f801ebada0f66c66d65eae20f53d2f127d3
6 changes: 3 additions & 3 deletions src/extractors/generic/content/extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const GenericContentExtractor = {
extract({ $, html, title, url }, opts) {
opts = { ...this.defaultOpts, ...opts };

$ = $ || cheerio.load(html);
$ = $ || cheerio.load(html, { decodeEntities: false });

// Cascade through our extraction-specific opts in an ordered fashion,
// turning them off as we try to extract content.
Expand All @@ -50,7 +50,7 @@ const GenericContentExtractor = {
// eslint-disable-next-line no-restricted-syntax
for (const key of Reflect.ownKeys(opts).filter(k => opts[k] === true)) {
opts[key] = false;
$ = cheerio.load(html);
$ = cheerio.load(html, { decodeEntities: false });

node = this.getContentNode($, title, url, opts);

Expand Down Expand Up @@ -80,7 +80,7 @@ const GenericContentExtractor = {
return null;
}

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

Expand Down
6 changes: 4 additions & 2 deletions src/extractors/generic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const GenericExtractor = {
const { html, $, contentType = 'html' } = options;

if (html && !$) {
const loaded = cheerio.load(html);
const loaded = cheerio.load(html, { decodeEntities: false });
options.$ = loaded;
}

Expand All @@ -53,7 +53,9 @@ const GenericExtractor = {
if (contentType === 'html') {
convertedContent = content;
} else if (contentType === 'text') {
convertedContent = $.text(cheerio.load(content));
convertedContent = $.text(
cheerio.load(content, { decodeEntities: false })
);
} else if (contentType === 'markdown') {
const turndownService = new TurndownService();
convertedContent = turndownService.turndown(content);
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/generic/word-count/extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { normalizeSpaces } from 'utils/text';

const GenericWordCountExtractor = {
extract({ content }) {
const $ = cheerio.load(content);
const $ = cheerio.load(content, { decodeEntities: false });
const $content = $('div').first();

const text = normalizeSpaces($content.text());
Expand Down
4 changes: 2 additions & 2 deletions src/resource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const Resource = {
encodeDoc({ content, contentType }) {
const encoding = getEncoding(contentType);
let decodedContent = iconv.decode(content, encoding);
let $ = cheerio.load(decodedContent);
let $ = cheerio.load(decodedContent, { decodeEntities: false });

// after first cheerio.load, check to see if encoding matches
const metaContentType =
Expand All @@ -74,7 +74,7 @@ const Resource = {
// if encodings in the header/body dont match, use the one in the body
if (metaContentType && properEncoding !== encoding) {
decodedContent = iconv.decode(content, properEncoding);
$ = cheerio.load(decodedContent);
$ = cheerio.load(decodedContent, { decodeEntities: false });
}

return $;
Expand Down
10 changes: 10 additions & 0 deletions src/resource/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ describe('Resource', () => {
assert.equal(typeof $, 'function');
});

it('leaves entities and special characters intact', async () => {
const url =
'https://gist.githubusercontent.com/benubois/397678cb7deeb4d91ad61d2d9b05a0fc/raw/8c5f504c6a8d18d8ab2cc119fd3481764b16ce5f/entities.html';
const $ = await Resource.create(url);

const result = /😀"/g;

assert.equal(result.test($.html()), true);
});

it('handles special encoding', async () => {
const url =
'http://www.elmundo.es/opinion/2016/11/19/582f476846163fc65a8b4578.html';
Expand Down