File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
extensions/blocks/google-calendar Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,13 @@ import { getBlockDefaultClassName } from '@wordpress/blocks';
1919 * Internal dependencies
2020 */
2121import 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' ;
2329import { isAtomicSite , isSimpleSite } from '../../shared/site-type-utils' ;
2430
2531class 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 }
Original file line number Diff line number Diff line change 1- const url_regex_string = 's*https?://calendar.google.com/calendar/embed ' ;
1+ const url_regex_string = 's*https?://calendar.google.com/calendar' ;
22export const URL_REGEX = new RegExp ( `^${ url_regex_string } ` , 'i' ) ;
33export 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
812const 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 *
You can’t perform that action at this time.
0 commit comments