Skip to content

Commit 0b8c319

Browse files
committed
style: Cleanup code
1 parent 0ff70f2 commit 0b8c319

File tree

6 files changed

+69
-69
lines changed

6 files changed

+69
-69
lines changed

src/__tests__/get-twitter-html.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import cases from 'jest-in-case';
22
import fetchMock from 'node-fetch';
3+
34
import getTwitterHtml, { shouldTransform } from '../get-twitter-html';
45

56
jest.mock('node-fetch', () =>
67
jest.fn().mockResolvedValue({
78
json: () =>
89
Promise.resolve({
910
html: `
10-
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">example</p>&mdash; Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/1078755736455278592?ref_src=twsrc%5Etfw">December 28, 2018</a></blockquote>
11-
`.trim(),
11+
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">example</p>&mdash; Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/1078755736455278592?ref_src=twsrc%5Etfw">December 28, 2018</a></blockquote>
12+
`.trim(),
1213
}),
1314
})
1415
);

src/__tests__/get-youtube-html.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import cases from 'jest-in-case';
2+
23
import getYouTubeHTML, {
4+
getTimeValueInSeconds,
35
getYouTubeIFrameSrc,
46
shouldTransform,
5-
getTimeValueInSeconds,
67
} from '../get-youtube-html';
78

89
cases(
@@ -71,8 +72,8 @@ cases(
7172
{ value: '1h', seconds: '3600' },
7273
{ value: '1h1m1s', seconds: '3661' },
7374
].map(opts => ({
74-
name: `${opts.value} -> ${opts.seconds}`,
7575
...opts,
76+
name: `${opts.value} -> ${opts.seconds}`,
7677
}))
7778
);
7879

src/get-codesandbox-html.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const { URL } = require('url');
1+
import { URL } from 'url';
22

3-
function shouldTransform(string) {
3+
export const shouldTransform = string => {
44
return new URL(string).host.endsWith('codesandbox.io');
5-
}
5+
};
66

7-
function getCodeSandboxHTML(string) {
7+
const getCodeSandboxHTML = string => {
88
const iframeUrl = string.replace('/s/', '/embed/');
9+
910
return `<iframe src="${iframeUrl}" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"></iframe>`;
10-
}
11+
};
1112

12-
module.exports = getCodeSandboxHTML;
13-
module.exports.shouldTransform = shouldTransform;
13+
export default getCodeSandboxHTML;

src/get-twitter-html.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
const { URL } = require('url');
2-
const fetch = require('node-fetch');
1+
import { URL } from 'url';
2+
import fetch from 'node-fetch';
33

4-
function shouldTransform(string) {
4+
export const shouldTransform = string => {
55
const { host, pathname } = new URL(string);
6+
67
return host.endsWith('twitter.com') && pathname.includes('/status/');
7-
}
8+
};
89

9-
function getTwitterHtml(string) {
10-
return fetch(
11-
`https://publish.twitter.com/oembed?url=${string}&omit_script=true`
12-
)
10+
const getTwitterHtml = string =>
11+
fetch(`https://publish.twitter.com/oembed?url=${string}c&omit_script=true`)
1312
.then(r => r.json())
14-
.then(r => {
15-
return [r.html]
13+
.then(r =>
14+
[r.html]
1615
.map(s => s.replace(/\?ref_src=twsrc.*?fw/g, ''))
1716
.map(s => s.replace(/<br>/g, '<br />'))
1817
.join('')
19-
.trim();
20-
});
21-
}
18+
.trim()
19+
);
2220

23-
module.exports = getTwitterHtml;
24-
module.exports.shouldTransform = shouldTransform;
21+
export default getTwitterHtml;

src/get-youtube-html.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
const { URL } = require('url');
1+
import { URL } from 'url';
22

3-
function shouldTransform(string) {
3+
export const shouldTransform = string => {
44
const { host } = new URL(string);
5+
56
return host.endsWith('youtube.com') || host.endsWith('youtu.be');
6-
}
7+
};
78

8-
function getYouTubeHTML(string) {
9-
const iframeSrc = getYouTubeIFrameSrc(string);
10-
return `<iframe width="100%" height="315" src="${iframeSrc}" frameBorder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>`;
11-
}
9+
export const getTimeValueInSeconds = timeValue => {
10+
if (Number(timeValue).toString() === timeValue) {
11+
return timeValue;
12+
}
1213

13-
function getYouTubeIFrameSrc(string) {
14+
const {
15+
2: hours = '0',
16+
4: minutes = '0',
17+
6: seconds = '0',
18+
} = timeValue.match(/((\d*)h)?((\d*)m)?((\d*)s)?/);
19+
20+
return String((Number(hours) * 60 + Number(minutes)) * 60 + Number(seconds));
21+
};
22+
export const getYouTubeIFrameSrc = string => {
1423
const url = new URL(string);
1524
let id = url.searchParams.get('v');
1625
if (url.host === 'youtu.be') {
@@ -23,28 +32,20 @@ function getYouTubeIFrameSrc(string) {
2332
if (name === 'v') {
2433
return;
2534
}
35+
2636
if (name === 't') {
27-
name = 'start';
28-
value = getTimeValueInSeconds(value);
37+
embedUrl.searchParams.append('start', getTimeValueInSeconds(value));
38+
} else {
39+
embedUrl.searchParams.append(name, value);
2940
}
30-
embedUrl.searchParams.append(name, value);
3141
});
42+
3243
return embedUrl.toString();
33-
}
44+
};
45+
const getYouTubeHTML = string => {
46+
const iframeSrc = getYouTubeIFrameSrc(string);
3447

35-
function getTimeValueInSeconds(timeValue) {
36-
if (Number(timeValue).toString() === timeValue) {
37-
return timeValue;
38-
}
39-
const {
40-
2: hours = '0',
41-
4: minutes = '0',
42-
6: seconds = '0',
43-
} = timeValue.match(/((\d*)h)?((\d*)m)?((\d*)s)?/);
44-
return String((Number(hours) * 60 + Number(minutes)) * 60 + Number(seconds));
45-
}
48+
return `<iframe width="100%" height="315" src="${iframeSrc}" frameBorder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>`;
49+
};
4650

47-
module.exports = getYouTubeHTML;
48-
module.exports.shouldTransform = shouldTransform;
49-
module.exports.getYouTubeIFrameSrc = getYouTubeIFrameSrc;
50-
module.exports.getTimeValueInSeconds = getTimeValueInSeconds;
51+
export default getYouTubeHTML;

src/index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
const visit = require('unist-util-visit');
2-
const getYouTubeHTML = require('./get-youtube-html');
3-
const getTwitterHTML = require('./get-twitter-html');
4-
const getCodeSandboxHTML = require('./get-codesandbox-html');
1+
import visit from 'unist-util-visit';
2+
3+
import getCodeSandboxHTML from './get-codesandbox-html';
4+
import getTwitterHTML from './get-twitter-html';
5+
import getYouTubeHTML from './get-youtube-html';
56

67
const transformers = [getYouTubeHTML, getTwitterHTML, getCodeSandboxHTML];
78

8-
module.exports = async ({ markdownAST, cache }) => {
9+
const getUrlString = string => {
10+
const urlString = string.startsWith('http') ? string : `https://${string}`;
11+
12+
try {
13+
return new URL(urlString).toString();
14+
} catch (error) {
15+
return null;
16+
}
17+
};
18+
19+
export default async ({ markdownAST, cache }) => {
920
const transformations = [];
1021
visit(markdownAST, 'paragraph', paragraphNode => {
1122
if (paragraphNode.children.length !== 1) {
@@ -49,14 +60,3 @@ module.exports = async ({ markdownAST, cache }) => {
4960

5061
return markdownAST;
5162
};
52-
53-
function getUrlString(string) {
54-
if (!string.startsWith('http')) {
55-
string = `https://${string}`;
56-
}
57-
try {
58-
return new URL(string).toString();
59-
} catch (error) {
60-
return null;
61-
}
62-
}

0 commit comments

Comments
 (0)