Skip to content

Commit

Permalink
Merge pull request #16 from herablog/fix/twitter-link
Browse files Browse the repository at this point in the history
Check if tweet id is valid or not.
  • Loading branch information
herablog authored Jul 17, 2018
2 parents 435defe + 1ce3f87 commit c7bce61
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/plugins/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ function extractTweetId(href) {
// Extract '690077988243836928' from https://twitter.com/nodenpm/status/690077988243836928?ref_src=twsrc
var pathname = _url2.default.parse(href).pathname || '';
var tweetId = _path2.default.basename(pathname);
return tweetId;
var isValidTweetId = /\d+/.test(tweetId);
return isValidTweetId ? tweetId : null;
}

function removeTwitterScripts() {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function extractTweetId(href) {
// Extract '690077988243836928' from https://twitter.com/nodenpm/status/690077988243836928?ref_src=twsrc
const pathname = url.parse(href).pathname || '';
const tweetId = path.basename(pathname);
return tweetId;
const isValidTweetId = /\d+/.test(tweetId);
return isValidTweetId ? tweetId : null;
}

function removeTwitterScripts() {
Expand Down
8 changes: 8 additions & 0 deletions tests/plugins/twitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ describe('twitter', () => {
expect(result).to.equal(fixture);
});

it('does not change tag when the link is not entry.', () => {
const html = '<a href="https://twitter.com/nodenpm">Twitter</a>';
const fixture = '<a href="https://twitter.com/nodenpm">Twitter</a>';
const builder = ampBuilder(html);
const result = builder.toAmpTwitter().html();
expect(result).to.equal(fixture);
});

it('changes tag when the twitter embed.', () => {
const html = '<blockquote class="twitter-tweet" lang="en"><p lang="en" dir="ltr">bentojs-api-email (1.0.0): <a href="https://t.co/kujT9n6i2P">https://t.co/kujT9n6i2P</a> A module written for the bentojs api platform for emails</p>&mdash; npm tweets (@nodenpm) <a href="https://twitter.com/nodenpm/status/690077988243836928">January 21, 2016</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
const fixture = '<amp-twitter width="486" height="657" data-tweetid="690077988243836928" data-cards="visible" layout="responsive"></amp-twitter>';
Expand Down

0 comments on commit c7bce61

Please sign in to comment.