Skip to content

Commit e3c2e33

Browse files
feat(Twitter): Add support for Twitter timelines (#120)
* Solving #97 * Update tests * Update src/transformers/Twitter.js (for the sake of consistency) Co-authored-by: Michaël De Boey <[email protected]> * Update src/__tests__/transformers/Twitter.js As far as we know, this can't be created Co-authored-by: Michaël De Boey <[email protected]> * Update src/__tests__/transformers/Twitter.js This too, is not possible. Co-authored-by: Michaël De Boey <[email protected]> * Update src/__tests__/transformers/__fixtures__/Twitter.md This too, is not possible. Co-authored-by: Michaël De Boey <[email protected]> Co-authored-by: Agastya Chandrakant <[email protected]> Co-authored-by: Michaël De Boey <[email protected]>
1 parent 935d228 commit e3c2e33

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/__tests__/transformers/Twitter.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';
99
const { Response } = jest.requireActual('node-fetch');
1010
jest.mock('node-fetch', () => jest.fn());
1111

12-
const mockFetch = (status, moment) =>
12+
const mockFetch = (status, moment, timeline) =>
1313
fetchMock
1414
.mockResolvedValueOnce(new Response(JSON.stringify({ html: status })))
1515
.mockResolvedValueOnce(new Response(JSON.stringify({ html: status })))
1616
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
1717
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
1818
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
19-
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })));
19+
.mockResolvedValueOnce(new Response(JSON.stringify({ html: moment })))
20+
.mockResolvedValueOnce(new Response(JSON.stringify({ html: timeline })))
21+
.mockResolvedValueOnce(new Response(JSON.stringify({ html: timeline })));
2022

2123
beforeEach(() => {
2224
fetchMock.mockReset();
@@ -48,6 +50,10 @@ cases(
4850
url: 'https://this-is-not-twitter.com/foobar/status/123',
4951
valid: false,
5052
},
53+
"non-Twitter url ending with 'twitter.com' and having '/timelines/'": {
54+
url: 'https://this-is-not-twitter.com/foobar/timelines/123',
55+
valid: false,
56+
},
5157
'profile url': {
5258
url: 'https://twitter.com/MichaelDeBoey93',
5359
valid: false,
@@ -80,6 +86,14 @@ cases(
8086
url: 'https://www.twitter.com/i/events/994601867987619840',
8187
valid: true,
8288
},
89+
'timelines url': {
90+
url: 'https://twitter.com/wesbos/timelines/1189618481672667136',
91+
valid: true,
92+
},
93+
"timelines url having 'www' subdomain": {
94+
url: 'https://www.twitter.com/wesbos/timelines/1189618481672667136',
95+
valid: true,
96+
},
8397
}
8498
);
8599

@@ -111,10 +125,25 @@ test('Gets the correct Twitter moment link', async () => {
111125
);
112126
});
113127

128+
test('Gets the correct Twitter timeline link', async () => {
129+
mockFetch(
130+
`<a class="twitter-timeline-mocked-fetch-transformer" href="https://twitter.com/wesbos/timelines/1189618481672667136">🔥 Hot Tips from Wes Bos - Curated tweets by wesbos</a>`
131+
);
132+
133+
const html = await getHTML(
134+
'https://twitter.com/wesbos/timelines/1189618481672667136'
135+
);
136+
137+
expect(html).toMatchInlineSnapshot(
138+
`"<a class=\\"twitter-timeline-mocked-fetch-transformer\\" href=\\"https://twitter.com/wesbos/timelines/1189618481672667136\\">🔥 Hot Tips from Wes Bos - Curated tweets by wesbos</a>"`
139+
);
140+
});
141+
114142
test('Plugin can transform Twitter links', async () => {
115143
mockFetch(
116144
`<blockquote class="twitter-tweet-mocked-fetch-plugin"><p lang="en" dir="ltr">example</p>&mdash; Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/1078755736455278592">December 28, 2018</a></blockquote>`,
117-
`<a class="twitter-moment-mocked-fetch-plugin" href="https://twitter.com/i/moments/994601867987619840">🔥 Design Tips</a>`
145+
`<a class="twitter-moment-mocked-fetch-plugin" href="https://twitter.com/i/moments/994601867987619840">🔥 Design Tips</a>`,
146+
`<a class="twitter-timeline-mocked-fetch-plugin" href="https://twitter.com/wesbos/timelines/1189618481672667136">🔥 Hot Tips from Wes Bos - Curated tweets by wesbos</a>`
118147
);
119148
const markdownAST = getMarkdownASTForFile('Twitter');
120149

@@ -131,6 +160,8 @@ test('Plugin can transform Twitter links', async () => {
131160
132161
<https://this-is-not-twitter.com/foobar/status/123>
133162
163+
<https://this-is-not-twitter.com/foobar/timelines/123>
164+
134165
<https://twitter.com/MichaelDeBoey93>
135166
136167
<https://twitter.com/i/moments/edit/994601867987619840>
@@ -146,6 +177,10 @@ test('Plugin can transform Twitter links', async () => {
146177
<a class=\\"twitter-moment-mocked-fetch-plugin\\" href=\\"https://twitter.com/i/moments/994601867987619840\\">🔥 Design Tips</a>
147178
148179
<a class=\\"twitter-moment-mocked-fetch-plugin\\" href=\\"https://twitter.com/i/moments/994601867987619840\\">🔥 Design Tips</a>
180+
181+
<a class=\\"twitter-timeline-mocked-fetch-plugin\\" href=\\"https://twitter.com/wesbos/timelines/1189618481672667136\\">🔥 Hot Tips from Wes Bos - Curated tweets by wesbos</a>
182+
183+
<a class=\\"twitter-timeline-mocked-fetch-plugin\\" href=\\"https://twitter.com/wesbos/timelines/1189618481672667136\\">🔥 Hot Tips from Wes Bos - Curated tweets by wesbos</a>
149184
"
150185
`);
151186
});

src/__tests__/transformers/__fixtures__/Twitter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ https://this-is-not-twitter.com/i/moments/123
88

99
https://this-is-not-twitter.com/foobar/status/123
1010

11+
https://this-is-not-twitter.com/foobar/timelines/123
12+
1113
https://twitter.com/MichaelDeBoey93
1214

1315
https://twitter.com/i/moments/edit/994601867987619840
@@ -23,3 +25,7 @@ https://www.twitter.com/i/moments/994601867987619840
2325
https://twitter.com/i/events/994601867987619840
2426

2527
https://www.twitter.com/i/events/994601867987619840
28+
29+
https://twitter.com/wesbos/timelines/1189618481672667136
30+
31+
https://www.twitter.com/wesbos/timelines/1189618481672667136

src/transformers/Twitter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const shouldTransform = (url) => {
66
return (
77
['twitter.com', 'www.twitter.com'].includes(host) &&
88
(pathname.includes('/status/') ||
9-
(includesSomeOfArray(pathname, ['/events/', '/moments/']) &&
9+
(includesSomeOfArray(pathname, ['/events/', '/moments/', '/timelines/']) &&
1010
!pathname.includes('/edit/')))
1111
);
1212
};

0 commit comments

Comments
 (0)