-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added initial StackBlitz support
- Loading branch information
1 parent
6f0f7d1
commit b81aabf
Showing
4 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import cases from 'jest-in-case'; | ||
|
||
import plugin from '../../'; | ||
import { getHTML, shouldTransform } from '../../transformers/StackBlitz'; | ||
|
||
import { cache, getMarkdownASTForFile, mdastToHtml } from '../helpers'; | ||
|
||
cases( | ||
'url validation', | ||
({ url, valid }) => { | ||
expect(shouldTransform(url)).toBe(valid); | ||
}, | ||
{ | ||
'non-StackBlitz url': { | ||
url: 'https://not-a-stackblitz-url.com', | ||
valid: false, | ||
}, | ||
"non-StackBlitz url ending with 'stackblitz.com'": { | ||
url: 'https://this-is-not-stackblitz.com', | ||
valid: false, | ||
}, | ||
"non-CodeSandbox url ending with 'stackblitz.com' having '/edit/'": { | ||
url: 'https://this-is-not-stackblitz.com/edit/start-to-source-1-ng-template', | ||
valid: false, | ||
}, | ||
'embed url': { | ||
url: 'https://stackblitz.com/edit/start-to-source-1-ng-template', | ||
valid: true, | ||
}, | ||
'embed url with parameters': { | ||
url: 'https://stackblitz.com/edit/start-to-source-1-ng-template?file=src/app/app.component.ts', | ||
valid: true, | ||
}, | ||
} | ||
); | ||
|
||
test('Gets the correct StackBlitz iframe', () => { | ||
const html = getHTML('https://stackblitz.com/edit/start-to-source-1-ng-template'); | ||
|
||
expect(html).toMatchInlineSnapshot( | ||
`<iframe src="https://stackblitz.com/edit/start-to-source-1-ng-template?embed=1" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>` | ||
); | ||
}); | ||
|
||
test('Plugin can transform StackBlitz links', async () => { | ||
const markdownAST = getMarkdownASTForFile('StackBlitz'); | ||
|
||
const processedAST = await plugin({ cache, markdownAST }); | ||
|
||
expect(mdastToHtml(processedAST)).toMatchInlineSnapshot(` | ||
<p>https://not-a-stackblitz-url.com</p> | ||
<p>https://this-is-not-stackblitz.com</p> | ||
<p>https://this-is-not-stackblitz.com/edit/start-to-source-1-ng-template</p> | ||
<iframe src="https://stackblitz.com/edit/start-to-source-1-ng-template?embed=1" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> | ||
<iframe src="https://stackblitz.com/edit/start-to-source-1-ng-template?embed=1&file=src/app/app.component.ts" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> | ||
`); | ||
}); |
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,9 @@ | ||
https://not-a-stackblitz-url.com | ||
|
||
https://this-is-not-stackblitz.com | ||
|
||
https://this-is-not-stackblitz.com/edit/start-to-source-1-ng-template | ||
|
||
https://stackblitz.com/edit/start-to-source-1-ng-template | ||
|
||
https://stackblitz.com/edit/start-to-source-1-ng-template?file=src/app/app.component.ts |
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,28 @@ | ||
export const shouldTransform = (url) => { | ||
const { host, pathname } = new URL(url); | ||
|
||
return ( | ||
['stackblitz.com'].includes(host) && | ||
pathname.includes('/edit/') | ||
); | ||
}; | ||
|
||
export const getStackBlitzEmbedIframe = (url) => { | ||
const editRegex = /(.*\/edit\/.*?)((?:\?.*$)|$)/; | ||
const regexResults = editRegex.exec(url); | ||
const base = regexResults[1]; | ||
let queryStr = regexResults[2]; | ||
if (queryStr) { | ||
queryStr = queryStr.replace('?', '?embed=1&'); | ||
} else { | ||
queryStr = '?embed=1'; | ||
} | ||
|
||
return `${base}${queryStr}`; | ||
} | ||
|
||
export const getHTML = (url) => { | ||
const iframeUrl = getStackBlitzEmbedIframe(url); | ||
|
||
return `<iframe src="${iframeUrl}" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`; | ||
}; |
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