Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: medium.com extractor #724

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 0 additions & 37 deletions fixtures/medium.com--another.html

This file was deleted.

File renamed without changes.
6,186 changes: 6,186 additions & 0 deletions fixtures/medium.com--mtg.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/extractors/custom/medium.com/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const MediumExtractor = {
},

content: {
selectors: ['article'],
selectors: ['article', 'article>div[class=l]'],

// Is there anything in the content you selected that needs transformed
// before it's consumable content? E.g., unusual lazy loaded images
Expand All @@ -22,6 +22,7 @@ export const MediumExtractor = {
$node.replaceWith($text);
}
},

// Re-write lazy-loaded youtube videos
iframe: $node => {
const ytRe = /https:\/\/i.embed.ly\/.+url=https:\/\/i\.ytimg\.com\/vi\/(\w+)\//;
Expand Down
39 changes: 21 additions & 18 deletions src/extractors/custom/medium.com/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import cheerio from 'cheerio';

import Mercury from 'mercury';
import getExtractor from 'extractors/get-extractor';
import { excerptContent } from 'utils/text';
import { excerptContentRange } from 'utils/text';

const fs = require('fs');

describe('MediumExtractor', () => {
describe('extract medium article - the wtf economy', () => {
describe('initial test case', () => {
let result;
let url;
beforeAll(() => {
url =
'https://medium.com/the-wtf-economy/wtf-whats-the-future-e52ab9515573#.ilwrgwsks';
const html = fs.readFileSync('./fixtures/medium.com.html');
const html = fs.readFileSync('./fixtures/medium.com--future.html');
result = Mercury.parse(url, { html, fallback: false });
});

Expand Down Expand Up @@ -70,41 +70,44 @@ describe('MediumExtractor', () => {
const { content } = await result;

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

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

const slice1 = excerptContentRange(text, 0, 13);
assert.equal(
first13,
slice1,
'Last Thursday, I had the honor to be one of the warmup acts'
);

const slice2 = excerptContentRange(text, -37, -28);
assert.equal(slice2, 'Can we hand off a better world to our');
});
});

describe('works with another url', () => {
describe('extract medium article - the mtg color wheel', () => {
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--another.html');
url = 'https://humanparts.medium.com/the-mtg-color-wheel-c9700a7cf36d';
const html = fs.readFileSync('./fixtures/medium.com--mtg.html');
result = Mercury.parse(url, { html, fallback: false });
});

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

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

const first13 = excerptContent($.text(), 13);
const slice1 = excerptContentRange(text, 18, 18 + 12);
assert.equal(
slice1,
'Magic: The Gathering is a fantasy card game by Richard Garfield, Ph.D.'
);

const slice2 = excerptContentRange(text, -16);
assert.equal(
first13,
'I’m sure you have seen something like the following line very often while'
slice2,
'What sorts of things will I say? What sorts of things are likely to land flat?'
);
});
});
Expand Down
15 changes: 14 additions & 1 deletion src/utils/text/excerpt-content.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export default function excerptContent(content, words = 10) {
export function excerptContent(content, words = 10) {
return content
.trim()
.split(/\s+/)
.slice(0, words)
.join(' ');
}

export function excerptContentRange(content, start, end) {
return content
.trim()
.split(/\s+/)
.slice(start, end)
.join(' ');
}

export default {
excerptContent,
excerptContentRange,
};
2 changes: 1 addition & 1 deletion src/utils/text/excerpt.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import excerptContent from './excerpt-content';
import { excerptContent } from './excerpt-content';

describe('excerptContent(content, words)', () => {
it('extracts the requested number of words from content', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export { default as pageNumFromUrl } from './page-num-from-url';
export { default as removeAnchor } from './remove-anchor';
export { default as articleBaseUrl } from './article-base-url';
export { default as hasSentenceEnd } from './has-sentence-end';
export { default as excerptContent } from './excerpt-content';
export { excerptContent, excerptContentRange } from './excerpt-content';
export { default as getEncoding } from './get-encoding';