Skip to content

Commit c844b06

Browse files
feat(plugin): Support service-specific options (#58)
* wip: poc for twitter url parameters * only pass relevant options * add instagram custom parameter support * document service-specific options * add tests * refactor: move query parameters into sub object * add general service-specific info * rebase and remove queryparams name * update docs example * fix: import url constructor * refactor: nest and document params object * the quotes are on purpose * fix: import urlsearchparams constructor * feat: only implement plugin-specific-options mechanism * docs: removed old option docs * chore: remove unused imports * chore: remove unused imports * Update README * Update transformers * Update plugin * Update tests * test: transformer options * test: generalize * Update src/__tests__/plugin.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update src/__tests__/plugin.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update src/index.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update src/index.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update src/__tests__/plugin.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update src/__tests__/plugin.js Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update docs Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
1 parent edb1bfa commit c844b06

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ Each element of the array should be an object with two methods that receive the
596596
URL argument:
597597

598598
- `shouldTransform(url)`
599-
- `getHTML(url)`
599+
- `getHTML(url, options)`
600600

601601
The `shouldTransform` method should check if the URL matches the one intended to
602602
transform; it should to return a boolean value.
@@ -678,6 +678,7 @@ Thanks goes to these people ([emoji key][emojis]):
678678

679679
<!-- markdownlint-enable -->
680680
<!-- prettier-ignore-end -->
681+
681682
<!-- ALL-CONTRIBUTORS-LIST:END -->
682683

683684
This project follows the [all-contributors][all-contributors] specification.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://some-site.com/id/abc

src/__tests__/plugin.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import cases from 'jest-in-case';
2+
13
import plugin from '../';
24

35
import {
@@ -90,4 +92,39 @@ describe('gatsby-remark-embedder', () => {
9092
An error occurred in ErrorTransformer]
9193
`);
9294
});
95+
96+
cases(
97+
'passes service-specific options to the transformers',
98+
async ({ name, passedOptions }) => {
99+
const transformer = {
100+
getHTML: jest.fn(),
101+
name,
102+
shouldTransform: () => true,
103+
};
104+
105+
const markdownAST = getMarkdownASTForFile('ServiceTransformer', true);
106+
107+
await plugin(
108+
{ cache, markdownAST },
109+
{
110+
customTransformers: [transformer],
111+
services: { serviceTransformer: { service: 'transformer' } },
112+
}
113+
);
114+
115+
expect(transformer.getHTML).toHaveBeenCalledWith(
116+
'https://some-site.com/id/abc',
117+
passedOptions
118+
);
119+
},
120+
{
121+
'transformer with name': {
122+
name: 'serviceTransformer',
123+
passedOptions: { service: 'transformer' },
124+
},
125+
'transformer without name': {
126+
passedOptions: {},
127+
},
128+
}
129+
);
93130
});

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const getUrlString = url => {
1515

1616
export default async (
1717
{ cache, markdownAST },
18-
{ customTransformers = [] } = {}
18+
{ customTransformers = [], services = {} } = {}
1919
) => {
2020
const transformers = [...defaultTransformers, ...customTransformers];
2121

@@ -46,13 +46,13 @@ export default async (
4646

4747
transformers
4848
.filter(({ shouldTransform }) => shouldTransform(urlString))
49-
.forEach(({ getHTML }) => {
49+
.forEach(({ getHTML, name = '' }) => {
5050
transformations.push(async () => {
5151
try {
5252
let html = await cache.get(urlString);
5353

5454
if (!html) {
55-
html = await getHTML(urlString);
55+
html = await getHTML(urlString, services[name] || {});
5656
await cache.set(urlString, html);
5757
}
5858

0 commit comments

Comments
 (0)