Skip to content

Commit d8c0421

Browse files
committed
feat(CodePen): added customizable options
1 parent 77d2e85 commit d8c0421

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/__tests__/plugin.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import cases from 'jest-in-case';
2-
32
import plugin from '../';
4-
53
import {
64
cache,
75
getMarkdownASTForFile,
@@ -104,11 +102,12 @@ describe('gatsby-remark-embedder', () => {
104102

105103
cases(
106104
'passes service-specific options to the transformers',
107-
async ({ name, passedOptions }) => {
105+
async ({ name, passedOptions, defaultOptions }) => {
108106
const transformer = {
109107
getHTML: jest.fn(),
110108
name,
111109
shouldTransform: () => true,
110+
defaultOptions,
112111
};
113112

114113
const markdownAST = getMarkdownASTForFile('ServiceTransformer', true);
@@ -117,13 +116,16 @@ describe('gatsby-remark-embedder', () => {
117116
{ cache, markdownAST },
118117
{
119118
customTransformers: [transformer],
120-
services: { serviceTransformer: { service: 'transformer' } },
119+
services: { serviceTransformer: passedOptions },
121120
}
122121
);
123122

124123
expect(transformer.getHTML).toHaveBeenCalledWith(
125124
'https://some-site.com/id/abc',
126-
passedOptions
125+
{
126+
...(defaultOptions || {}),
127+
...(passedOptions || {}),
128+
}
127129
);
128130
},
129131
{
@@ -134,6 +136,15 @@ describe('gatsby-remark-embedder', () => {
134136
'transformer without name': {
135137
passedOptions: {},
136138
},
139+
'transformer with default options': {
140+
name: 'serviceTransformer',
141+
defaultOptions: { width: '100%', height: '100%' },
142+
},
143+
'transformer with partially provided options': {
144+
name: 'serviceTransformer',
145+
defaultOptions: { width: '100%', height: '100%' },
146+
passedOptions: { height: '50%' },
147+
},
137148
}
138149
);
139150
});

src/__tests__/transformers/CodePen.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import cases from 'jest-in-case';
2-
32
import plugin from '../../';
43
import { getHTML, shouldTransform } from '../../transformers/CodePen';
5-
64
import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers';
75

86
cases(
@@ -78,14 +76,25 @@ cases(
7876
}
7977
);
8078

81-
test('Gets the correct CodePen iframe', () => {
79+
test('Gets the correct CodePen iframe with default dimensions', () => {
8280
const html = getHTML('https://codepen.io/team/codepen/pen/PNaGbb');
8381

8482
expect(html).toMatchInlineSnapshot(
8583
`"<iframe src=\\"https://codepen.io/team/codepen/embed/preview/PNaGbb\\" style=\\"width:100%; height:300px;\\"></iframe>"`
8684
);
8785
});
8886

87+
test('Gets the correct CodePen iframe with custom dimensions', () => {
88+
const html = getHTML('https://codepen.io/team/codepen/pen/PNaGbb', {
89+
width: '500px',
90+
height: '500px',
91+
});
92+
93+
expect(html).toMatchInlineSnapshot(
94+
`"<iframe src=\\"https://codepen.io/team/codepen/embed/preview/PNaGbb\\" style=\\"width:500px; height:500px;\\"></iframe>"`
95+
);
96+
});
97+
8998
test('Plugin can transform CodePen links', async () => {
9099
const markdownAST = getMarkdownASTForFile('CodePen');
91100

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import visit from 'unist-util-visit';
2-
32
import { defaultTransformers } from './transformers';
43

54
const getUrlString = (url) => {
@@ -45,13 +44,16 @@ export default async (
4544

4645
transformers
4746
.filter(({ shouldTransform }) => shouldTransform(urlString))
48-
.forEach(({ getHTML, name = '' }) => {
47+
.forEach(({ getHTML, name = '', defaultOptions = {} }) => {
4948
transformations.push(async () => {
5049
try {
5150
let html = await cache.get(urlString);
5251

5352
if (!html) {
54-
html = await getHTML(urlString, services[name] || {});
53+
html = await getHTML(urlString, {
54+
...defaultOptions,
55+
...(services[name] || {}),
56+
});
5557
await cache.set(urlString, html);
5658
}
5759

src/transformers/CodePen.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ export const shouldTransform = (url) => {
77
);
88
};
99

10-
export const getHTML = (url) => {
10+
export const name = 'CodePen';
11+
12+
export const defaultOptions = {
13+
width: '100%',
14+
height: '300px',
15+
};
16+
17+
export const getHTML = (url, options) => {
18+
const { width, height } = options || defaultOptions;
1119
const iframeUrl = url.replace('/pen/', '/embed/preview/');
1220

13-
return `<iframe src="${iframeUrl}" style="width:100%; height:300px;"></iframe>`;
21+
return `<iframe src="${iframeUrl}" style="width:${width}; height:${height};"></iframe>`;
1422
};

0 commit comments

Comments
 (0)