-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement a shared Glitch shortcode #68
base: main
Are you sure you want to change the base?
Changes from 1 commit
ae99e1c
1f12f4a
dec5259
8a915f9
fc8c5c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const {html} = require('common-tags'); | ||
const {escape, stringify} = require('querystring'); | ||
const iframe = require('./IFrame'); | ||
|
||
/** | ||
* Validates allow sources are an array and lower case. | ||
* If allow sources are a string, it will be split by the `;` character. | ||
* | ||
* @param {string|string[]} s | ||
* @returns {string[]} | ||
*/ | ||
function expandAllowSource(s) { | ||
if (typeof s === 'string') { | ||
s = s.split(/;\s*/g); | ||
} | ||
return s.map(a => a.toLowerCase()); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string | GlitchParam } param | ||
* @return string | ||
*/ | ||
module.exports = param => { | ||
const defaultAllow = [ | ||
'camera', | ||
'clipboard-read', | ||
'clipboard-write', | ||
'encrypted-media', | ||
'geolocation', | ||
'microphone', | ||
'midi', | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to top of file as configuration constant, like |
||
|
||
/** @type GlitchParam */ | ||
let glitchProps = { | ||
allow: [], | ||
height: 420, | ||
id: '', | ||
path: '', | ||
highlights: '', | ||
previewSize: 100, | ||
}; | ||
|
||
if (typeof param === 'string') { | ||
glitchProps.id = param; | ||
} else if (param.constructor === {}.constructor) { | ||
glitchProps = {...glitchProps, ...param}; | ||
} | ||
|
||
const { | ||
allow: userAllow, | ||
id, | ||
path, | ||
highlights, | ||
previewSize, | ||
height, | ||
} = glitchProps; | ||
|
||
if (!id) { | ||
return; | ||
} | ||
const url = 'https://glitch.com/embed/#!/embed/' + escape(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
const queryParams = { | ||
attributionHidden: 'true', | ||
sidebarCollapsed: 'true', | ||
}; | ||
|
||
if (path) { | ||
queryParams.path = path; | ||
} | ||
if (highlights) { | ||
queryParams.highlights = highlights; | ||
} | ||
if (typeof previewSize === 'number') { | ||
queryParams.previewSize = previewSize; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have another look at the d.c.c Glitch implementation. It already uses URLSearchParams: https://github.com/GoogleChrome/developer.chrome.com/blob/main/site/_shortcodes/Glitch.js |
||
|
||
let allow = Array.from(new Set([...defaultAllow])).join('; '); | ||
if (userAllow) { | ||
allow = Array.from( | ||
new Set([...defaultAllow, ...expandAllowSource(userAllow)]) | ||
).join('; '); | ||
} | ||
|
||
const src = `${url}?${stringify(queryParams)}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
return html` | ||
<div class="glitch-embed-wrap" style="height: ${height}px; width: 100%;"> | ||
${iframe({src, title: `${escape(id)} on Glitch`, allow})} | ||
</div> | ||
`; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare global { | ||
export interface GlitchParam { | ||
allow?: string | string[]; | ||
height?: number; | ||
id: string; | ||
path?: string; | ||
previewSize?: number; | ||
highlights?: string; | ||
} | ||
} | ||
|
||
// empty export to keep file a module | ||
export {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the Node.js native URL module instead.