mirror of
https://github.com/postlight/mercury-parser
synced 2024-11-18 21:28:22 +00:00
c314e3befa
Squashed commit of the following: commit 0ee7d51ce609ad23d2deca1af41e7b4e56681bd7 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 15:44:28 2016 -0700 feat: dek does not return if it's basically the same as the excerpt commit 6ad27f994fff3652e04ffe7c81f1ae0b1647e941 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 14:35:54 2016 -0700 feat: added excerpt util
88 lines
2.4 KiB
JavaScript
88 lines
2.4 KiB
JavaScript
import template from './index';
|
|
|
|
const IGNORE = [
|
|
'url',
|
|
'domain',
|
|
'content',
|
|
'word_count',
|
|
'next_page_url',
|
|
'excerpt',
|
|
'direction',
|
|
'total_pages',
|
|
'rendered_pages',
|
|
]
|
|
|
|
function testFor(key, value, dir, file, url) {
|
|
if (IGNORE.find(k => k === key)) return ''
|
|
|
|
return template`
|
|
it('returns the ${key}', async () => {
|
|
// To pass this test, fill out the ${key} selector
|
|
// in ${dir}/index.js.
|
|
const html =
|
|
fs.readFileSync('${file}');
|
|
const articleUrl =
|
|
'${url}';
|
|
|
|
const { ${key} } =
|
|
await Mercury.parse(articleUrl, html, { fallback: false });
|
|
|
|
// Update these values with the expected values from
|
|
// the article.
|
|
assert.equal(${key}, ${value ? "'" + value + "'" : "''"})
|
|
});
|
|
`;
|
|
}
|
|
|
|
export default function (file, url, dir, result) {
|
|
return template`
|
|
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';
|
|
|
|
// Rename CustomExtractor
|
|
describe('CustomExtractor', () => {
|
|
it('is selected properly', () => {
|
|
// To pass this test, rename your extractor in
|
|
// ${dir}/index.js
|
|
// (e.g., CustomExtractor => NYTimesExtractor)
|
|
// then add your new extractor to
|
|
// src/extractors/all.js
|
|
const url =
|
|
'${url}';
|
|
const extractor = getExtractor(url);
|
|
assert.equal(extractor.domain, URL.parse(url).hostname)
|
|
})
|
|
|
|
${Reflect.ownKeys(result).map(k => testFor(k, result[k], dir, file, url)).join('\n\n')}
|
|
|
|
it('returns the content', async () => {
|
|
// To pass this test, fill out the content selector
|
|
// in ${dir}/index.js.
|
|
// You may also want to make use of the clean and transform
|
|
// options.
|
|
const html =
|
|
fs.readFileSync('${file}');
|
|
const url =
|
|
'${url}';
|
|
|
|
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, null);
|
|
});
|
|
});
|
|
`;
|
|
}
|