Skip to content

Commit 2be4aff

Browse files
authored
Add support for shareable URLs (#14847)
The shareable URL for a Google Calendar includes the calendar src parameter that is used in the standard embed, but it's base64 encoded. This adds support for those URLs to the Google Calendar block.
1 parent 3fca2c5 commit 2be4aff

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

extensions/blocks/google-calendar/edit.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import { getBlockDefaultClassName } from '@wordpress/blocks';
1919
* Internal dependencies
2020
*/
2121
import icon from './icon';
22-
import { extractAttributesFromIframe, IFRAME_REGEX, URL_REGEX } from './utils';
22+
import {
23+
extractAttributesFromIframe,
24+
convertShareableUrl,
25+
IFRAME_REGEX,
26+
URL_REGEX,
27+
SHAREABLE_REGEX,
28+
} from './utils';
2329
import { isAtomicSite, isSimpleSite } from '../../shared/site-type-utils';
2430

2531
class GoogleCalendarEdit extends Component {
@@ -64,6 +70,8 @@ class GoogleCalendarEdit extends Component {
6470

6571
if ( IFRAME_REGEX.test( embedString ) ) {
6672
attributes = extractAttributesFromIframe( embedString );
73+
} else if ( SHAREABLE_REGEX.test( embedString ) ) {
74+
attributes = { url: convertShareableUrl( embedString ) };
6775
} else {
6876
attributes = { url: embedString };
6977
}

extensions/blocks/google-calendar/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
const url_regex_string = 's*https?://calendar.google.com/calendar/embed';
1+
const url_regex_string = 's*https?://calendar.google.com/calendar';
22
export const URL_REGEX = new RegExp( `^${ url_regex_string }`, 'i' );
33
export const IFRAME_REGEX = new RegExp(
44
`<iframe((?:\\s+\\w+=(['"]).*?\\2)*)\\s+src=(["'])(${ url_regex_string }.*?)\\3((?:\\s+\\w+=(['"]).*?\\6)*)`,
55
'i'
66
);
7+
export const SHAREABLE_REGEX = new RegExp(
8+
`${ url_regex_string }\\?cid=([-A-Za-z0-9+/]+={0,3})`,
9+
'i'
10+
);
711

812
const ATTRIBUTE_REGEX = /\s+(\w+)=(["'])(.*?)\2/gi;
913

14+
/**
15+
* Converts a Google Calendar shareable URL of the format:
16+
* https://calendar.google.com/calendar?cid=Z2xlbi5kYXZpZXNAYThjLmNvbQ
17+
*
18+
* to an embed URL.
19+
*
20+
* @param {string} shareableUrl The Google Calendar shareable URL
21+
* @returns {string} The embed URL or undefined if the conversion fails
22+
*/
23+
export function convertShareableUrl( shareableUrl ) {
24+
const parsedUrl = SHAREABLE_REGEX.exec( shareableUrl );
25+
if ( ! parsedUrl ) {
26+
return;
27+
}
28+
return (
29+
'https://calendar.google.com/calendar/embed?src=' + encodeURIComponent( atob( parsedUrl[ 1 ] ) )
30+
);
31+
}
32+
1033
/**
1134
* Given an <iframe> that matches IFRAME_REGEX, extract the url, width, and height.
1235
*

0 commit comments

Comments
 (0)