Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions shortcodes/Glitch.js
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');
Copy link
Collaborator

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.

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',
];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to top of file as configuration constant, like const DEFAULT_ALLOW.


/** @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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use new URL instead.

const queryParams = {
attributionHidden: 'true',
sidebarCollapsed: 'true',
};

if (path) {
queryParams.path = path;
}
if (highlights) {
queryParams.highlights = highlights;
}
if (typeof previewSize === 'number') {
queryParams.previewSize = previewSize;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use URLSearchParams instead.


return html`
<div class="glitch-embed-wrap" style="height: ${height}px; width: 100%;">
${iframe({src, title: `${escape(id)} on Glitch`, allow})}
</div>
`;
};
1 change: 1 addition & 0 deletions shortcodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
...require('./Codepen'),
...require('./Details'),
...require('./DetailsSummary'),
...require('./Glitch'),
...require('./IFrame'),
...require('./Img'),
...require('./Video'),
Expand Down
29 changes: 29 additions & 0 deletions types/shortcodes/Glitch.d.ts
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 {};