Skip to content

Commit

Permalink
fix: medium bug (#129)
Browse files Browse the repository at this point in the history
* fix: improved medium parser for images and multi-section content

* fix: duplicate video
  • Loading branch information
adampash authored Jan 31, 2017
1 parent 4e049de commit aa682d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions fixtures/medium.com/1485902752952.html

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions src/extractors/custom/medium.com/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const MediumExtractor = {

content: {
selectors: [
['.section-content'],
'.section-content',
'article > div > section',
],
Expand All @@ -36,10 +37,20 @@ export const MediumExtractor = {
const [_, youtubeId] = thumb.match(ytRe) // eslint-disable-line
$node.attr('src', `https://www.youtube.com/embed/${youtubeId}`);
const $parent = $node.parents('figure');
$parent.prepend($node.clone());
$node.remove();
const $caption = $parent.find('figcaption');
$parent.empty().append([$node, $caption]);
}
},

// rewrite figures to pull out image and caption, remove rest
figure: ($node) => {
// ignore if figure has an iframe
if ($node.find('iframe').length > 0) return;

const $img = $node.find('img').slice(-1)[0];
const $caption = $node.find('figcaption');
$node.empty().append([$img, $caption]);
},
},

// Is there anything that is in the result that shouldn't be?
Expand Down
22 changes: 22 additions & 0 deletions src/extractors/custom/medium.com/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,26 @@ describe('MediumExtractor', () => {
assert.equal(first13, 'Video of WTF? My talk at the White House Frontiers ConferenceLast Thursday, I');
});
});

describe('works with another url', () => {
let result;
let url;
beforeAll(() => {
url =
'https://medium.com/@JakobUlbrich/flag-attributes-in-android-how-to-use-them-ac4ec8aee7d1#.h949wjmyw';
const html = fs.readFileSync('./fixtures/medium.com/1485902752952.html');
result =
Mercury.parse(url, html, { fallback: false });
});

it('returns the content', async () => {
const { content } = await result;

const $ = cheerio.load(content || '');

const first13 = excerptContent($.text(), 13);

assert.equal(first13, 'I’m sure you have seen something like the following line very often while');
});
});
});

0 comments on commit aa682d7

Please sign in to comment.