-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Pinterest): Add support for Pinterest (#73)
* Support Pinterest * Update tests Co-authored-by: Michaël De Boey <[email protected]>
- Loading branch information
1 parent
ed6f881
commit dad289c
Showing
7 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import cases from 'jest-in-case'; | ||
|
||
import plugin from '../..'; | ||
import { getHTML, shouldTransform } from '../../transformers/Pinterest'; | ||
|
||
import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers'; | ||
|
||
cases( | ||
'url validation', | ||
({ url, valid }) => { | ||
expect(shouldTransform(url)).toBe(valid); | ||
}, | ||
{ | ||
'non-Pinterest url': { | ||
url: 'https://not-a-pinterest-url.com', | ||
valid: false, | ||
}, | ||
"non-Pinterest url ending with 'pinterest.com'": { | ||
url: 'https://this-is-not-pinterest.com', | ||
valid: false, | ||
}, | ||
"non-Pinterest url ending with 'pinterest.com' and having '/pin/' in the url": { | ||
url: 'https://this-is-not-pinterest.com/pin/99360735500167749', | ||
valid: false, | ||
}, | ||
'board url': { | ||
url: 'https://pinterest.com/pinterest/official-news', | ||
valid: true, | ||
}, | ||
"board url having 'www' subdomain": { | ||
url: 'https://www.pinterest.com/pinterest/official-news', | ||
valid: true, | ||
}, | ||
'pin url': { | ||
url: 'https://pinterest.com/pin/99360735500167749', | ||
valid: true, | ||
}, | ||
"pin url having 'www' subdomain": { | ||
url: 'https://www.pinterest.com/pin/99360735500167749', | ||
valid: true, | ||
}, | ||
'profile url': { | ||
url: 'https://pinterest.com/pinterest', | ||
valid: true, | ||
}, | ||
"profile url having 'www' subdomain": { | ||
url: 'https://www.pinterest.com/pinterest', | ||
valid: true, | ||
}, | ||
} | ||
); | ||
|
||
test('Gets the correct Pinterest board link', () => { | ||
const html = getHTML('https://pinterest.com/pinterest/official-news'); | ||
|
||
expect(html).toMatchInlineSnapshot( | ||
`"<a data-pin-do=\\"embedBoard\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://pinterest.com/pinterest/official-news\\"></a>"` | ||
); | ||
}); | ||
|
||
test('Gets the correct Pinterest pin link', () => { | ||
const html = getHTML('https://pinterest.com/pin/99360735500167749'); | ||
|
||
expect(html).toMatchInlineSnapshot( | ||
`"<a data-pin-do=\\"embedPin\\" href=\\"https://pinterest.com/pin/99360735500167749\\"></a>"` | ||
); | ||
}); | ||
|
||
test('Gets the correct Pinterest profile link', () => { | ||
const html = getHTML('https://pinterest.com/pinterest'); | ||
|
||
expect(html).toMatchInlineSnapshot( | ||
`"<a data-pin-do=\\"embedUser\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://pinterest.com/pinterest\\"></a>"` | ||
); | ||
}); | ||
|
||
test('Plugin can transform Pinterest links', async () => { | ||
const markdownAST = getMarkdownASTForFile('Pinterest'); | ||
|
||
const processedAST = await plugin({ cache, markdownAST }); | ||
|
||
expect(parseASTToMarkdown(processedAST)).toMatchInlineSnapshot(` | ||
"<https://not-a-pinterest-url.com> | ||
<https://this-is-not-pinterest.com> | ||
<https://this-is-not-pinterest.com/pin/99360735500167749> | ||
<a data-pin-do=\\"embedBoard\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://pinterest.com/pinterest/official-news\\"></a> | ||
<a data-pin-do=\\"embedBoard\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://www.pinterest.com/pinterest/official-news\\"></a> | ||
<a data-pin-do=\\"embedPin\\" href=\\"https://pinterest.com/pin/99360735500167749\\"></a> | ||
<a data-pin-do=\\"embedPin\\" href=\\"https://www.pinterest.com/pin/99360735500167749\\"></a> | ||
<a data-pin-do=\\"embedUser\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://pinterest.com/pinterest\\"></a> | ||
<a data-pin-do=\\"embedUser\\" data-pin-board-width=\\"400\\" data-pin-scale-height=\\"240\\" data-pin-scale-width=\\"80\\" href=\\"https://www.pinterest.com/pinterest\\"></a> | ||
" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
https://not-a-pinterest-url.com | ||
|
||
https://this-is-not-pinterest.com | ||
|
||
https://this-is-not-pinterest.com/pin/99360735500167749 | ||
|
||
https://pinterest.com/pinterest/official-news | ||
|
||
https://www.pinterest.com/pinterest/official-news | ||
|
||
https://pinterest.com/pin/99360735500167749 | ||
|
||
https://www.pinterest.com/pin/99360735500167749 | ||
|
||
https://pinterest.com/pinterest | ||
|
||
https://www.pinterest.com/pinterest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { URL } from 'url'; | ||
|
||
import { getTrimmedPathName } from './utils'; | ||
|
||
const isPin = pathname => pathname.includes('pin/'); | ||
const isProfile = pathname => pathname.split('/').length === 1; | ||
const isBoard = pathname => pathname.split('/').length === 2; | ||
|
||
export const shouldTransform = url => { | ||
const { host, pathname } = new URL(url); | ||
const trimmedPathName = getTrimmedPathName(pathname); | ||
|
||
return ( | ||
['pinterest.com', 'www.pinterest.com'].includes(host) && | ||
(isPin(trimmedPathName) || | ||
isProfile(trimmedPathName) || | ||
isBoard(trimmedPathName)) | ||
); | ||
}; | ||
|
||
export const getHTML = url => { | ||
const { pathname } = new URL(url); | ||
const trimmedPathName = getTrimmedPathName(pathname); | ||
|
||
if (isPin(trimmedPathName)) { | ||
return `<a data-pin-do="embedPin" href="${url}"></a>`; | ||
} | ||
|
||
if (isBoard(trimmedPathName)) { | ||
return `<a data-pin-do="embedBoard" data-pin-board-width="400" data-pin-scale-height="240" data-pin-scale-width="80" href="${url}"></a>`; | ||
} | ||
|
||
return `<a data-pin-do="embedUser" data-pin-board-width="400" data-pin-scale-height="240" data-pin-scale-width="80" href="${url}"></a>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters