Fix extension bugs (#23)

* feat: cleaning supplemental elements in nytimes (visible in web only)

closes https://github.com/postlight/mercury-reader-chrome-extension/issues/102

* wip

* fix: more generous date published bits

* feat: added washington post extractor (including figure transforms)

closes https://github.com/postlight/mercury-reader-chrome-extension/issues/100

* feat: cleaning zoom lightbox from gizmodo/kinja

* lint fix
pull/24/head^2
Adam Pash 8 years ago committed by GitHub
parent 3a2f32b0eb
commit a8face796a

55
dist/mercury.js vendored

@ -2060,7 +2060,7 @@ var NYTimesExtractor = {
}
},
clean: ['.ad', 'header#story-header', '.story-body-1 .lede.video', '.visually-hidden', '#newsletter-promo', '.promo', '.comments-button', '.hidden', '.comments']
clean: ['.ad', 'header#story-header', '.story-body-1 .lede.video', '.visually-hidden', '#newsletter-promo', '.promo', '.comments-button', '.hidden', '.comments', '.supplemental', '.nocontent']
},
date_published: null,
@ -2739,6 +2739,53 @@ var WwwTmzComExtractor = {
}
};
var WwwWashingtonpostComExtractor = {
domain: 'www.washingtonpost.com',
title: {
selectors: ['h1', '#topper-headline-wrapper']
},
author: {
selectors: ['.pb-byline']
},
date_published: {
selectors: [['.pb-timestamp[itemprop="datePublished"]', 'content']]
},
dek: {
selectors: [['meta[name="og:description"]', 'value']]
},
lead_image_url: {
selectors: [['meta[name="og:image"]', 'value']]
},
content: {
selectors: ['.article-body'],
// Is there anything in the content you selected that needs transformed
// before it's consumable content? E.g., unusual lazy loaded images
transforms: {
'div.inline-content': function divInlineContent($node) {
if ($node.has('img,iframe,video').length > 0) {
return 'figure';
}
$node.remove();
return null;
},
'.pb-caption': 'figcaption'
},
// Is there anything that is in the result that shouldn't be?
// The clean selectors will remove anything that matches from
// the result
clean: ['.interstitial-link']
}
};
var CustomExtractors = Object.freeze({
@ -2760,7 +2807,8 @@ var CustomExtractors = Object.freeze({
BroadwayWorldExtractor: BroadwayWorldExtractor,
ApartmentTherapyExtractor: ApartmentTherapyExtractor,
MediumExtractor: MediumExtractor,
WwwTmzComExtractor: WwwTmzComExtractor
WwwTmzComExtractor: WwwTmzComExtractor,
WwwWashingtonpostComExtractor: WwwWashingtonpostComExtractor
});
var Extractors = _Object$keys(CustomExtractors).reduce(function (acc, key) {
@ -2802,7 +2850,8 @@ var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oc
var allMonths = months.join('|');
var timestamp1 = '[0-9]{1,2}:[0-9]{2,2}( ?[ap].?m.?)?';
var timestamp2 = '[0-9]{1,2}[/-][0-9]{1,2}[/-][0-9]{2,4}';
var SPLIT_DATE_STRING = new RegExp('(' + timestamp1 + ')|(' + timestamp2 + ')|([0-9]{1,4})|(' + allMonths + ')', 'ig');
var timestamp3 = '-[0-9]{3,4}$';
var SPLIT_DATE_STRING = new RegExp('(' + timestamp1 + ')|(' + timestamp2 + ')|(' + timestamp3 + ')|([0-9]{1,4})|(' + allMonths + ')', 'ig');
// CLEAN TITLE CONSTANTS
// A regular expression that will match separating characters on a

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -48,8 +48,9 @@ const months = [
const allMonths = months.join('|');
const timestamp1 = '[0-9]{1,2}:[0-9]{2,2}( ?[ap].?m.?)?';
const timestamp2 = '[0-9]{1,2}[/-][0-9]{1,2}[/-][0-9]{2,4}';
const timestamp3 = '-[0-9]{3,4}$';
export const SPLIT_DATE_STRING =
new RegExp(`(${timestamp1})|(${timestamp2})|([0-9]{1,4})|(${allMonths})`, 'ig');
new RegExp(`(${timestamp1})|(${timestamp2})|(${timestamp3})|([0-9]{1,4})|(${allMonths})`, 'ig');
// CLEAN TITLE CONSTANTS
// A regular expression that will match separating characters on a

@ -58,4 +58,11 @@ describe('cleanDateString(dateString)', () => {
const date1 = cleanDateString('This page was last modified on 15 April 2016, at 10:59.');
assert.equal(date1, '15 Apr 2016 10:59');
});
it('massages the T out', () => {
// The JS date parser is forgiving, but
// it needs am/pm separated from a time
const date1 = cleanDateString('2016-11-22T08:57-500');
assert.equal(date1, '2016 11 22 08:57 -500');
});
});

@ -41,6 +41,8 @@ export const DeadspinExtractor = {
// The clean selectors will remove anything that matches from
// the result
clean: [
'.magnifier',
'.lightbox',
],
},

@ -17,3 +17,4 @@ export * from './www.broadwayworld.com';
export * from './www.apartmenttherapy.com';
export * from './medium.com';
export * from './www.tmz.com';
export * from './www.washingtonpost.com';

@ -51,6 +51,8 @@ export const NYTimesExtractor = {
'.comments-button',
'.hidden',
'.comments',
'.supplemental',
'.nocontent',
],
},

@ -0,0 +1,61 @@
export const WwwWashingtonpostComExtractor = {
domain: 'www.washingtonpost.com',
title: {
selectors: [
'h1',
'#topper-headline-wrapper',
],
},
author: {
selectors: [
'.pb-byline',
],
},
date_published: {
selectors: [
['.pb-timestamp[itemprop="datePublished"]', 'content'],
],
},
dek: {
selectors: [
['meta[name="og:description"]', 'value'],
],
},
lead_image_url: {
selectors: [
['meta[name="og:image"]', 'value'],
],
},
content: {
selectors: [
'.article-body',
],
// Is there anything in the content you selected that needs transformed
// before it's consumable content? E.g., unusual lazy loaded images
transforms: {
'div.inline-content': ($node) => {
if ($node.has('img,iframe,video').length > 0) {
return 'figure';
}
$node.remove();
return null;
},
'.pb-caption': 'figcaption',
},
// Is there anything that is in the result that shouldn't be?
// The clean selectors will remove anything that matches from
// the result
clean: [
'.interstitial-link',
],
},
};

@ -0,0 +1,122 @@
import assert from 'assert';
import fs from 'fs';
import URL from 'url';
import cheerio from 'cheerio';
import Mercury from 'mercury';
import getExtractor from 'extractors/get-extractor';
import { excerptContent } from 'utils/text';
describe('WwwWashingtonpostComExtractor', () => {
it('is selected properly', () => {
// This test should be passing by default.
// It sanity checks that the correct parser
// is being selected for URLs from this domain
const url =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const extractor = getExtractor(url);
assert.equal(extractor.domain, URL.parse(url).hostname);
});
it('returns the title', async () => {
// To pass this test, fill out the title selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const articleUrl =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { title } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(title, 'Trump Foundation admits to violating ban on self-dealing, new filing to IRS shows');
});
it('returns the author', async () => {
// To pass this test, fill out the author selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const articleUrl =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { author } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(author, 'David A. Fahrenthold');
});
it('returns the date_published', async () => {
// To pass this test, fill out the date_published selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const articleUrl =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { date_published } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(date_published, '2016-11-22T13:57:00.000Z');
});
it('returns the dek', async () => {
// To pass this test, fill out the dek selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const articleUrl =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { dek } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(dek, 'The foundation checked “yes” on the form for 2015 when asked whether it had transferred “income or assets to a disqualified person.”');
});
it('returns the lead_image_url', async () => {
// To pass this test, fill out the lead_image_url selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const articleUrl =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { lead_image_url } =
await Mercury.parse(articleUrl, html, { fallback: false });
// Update these values with the expected values from
// the article.
assert.equal(lead_image_url, 'https://img.washingtonpost.com/rf/image_1484w/2010-2019/WashingtonPost/2016/11/01/Others/Images/2016-11-01/Trump-HomeSafe-News-131478026931.jpg');
});
it('returns the content', async () => {
// To pass this test, fill out the content selector
// in ./src/extractors/custom/www.washingtonpost.com/index.js.
// You may also want to make use of the clean and transform
// options.
const html =
fs.readFileSync('./fixtures/www.washingtonpost.com/1480364838420.html');
const url =
'https://www.washingtonpost.com/politics/trump-foundation-apparently-admits-to-violating-ban-on-self-dealing-new-filing-to-irs-shows/2016/11/22/893f6508-b0a9-11e6-8616-52b15787add0_story.html';
const { content } =
await Mercury.parse(url, html, { fallback: false });
const $ = cheerio.load(content || '');
const first13 = excerptContent($('*').first().text(), 13);
// Update these values with the expected values from
// the article.
assert.equal(first13, 'Painter Michael Israel, left, poses with Donald and Melania Trump in 2007 at');
});
});
Loading…
Cancel
Save