@eeacms/volto-cca-policy 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ ### [0.1.12](https://github.com/eea/volto-cca-policy/compare/0.1.11...0.1.12) - 21 March 2023
8
+
9
+ #### :hammer_and_wrench: Others
10
+
11
+ - Add readme [Tiberiu Ichim - [`d674ea0`](https://github.com/eea/volto-cca-policy/commit/d674ea01c314b20f5891a593f8e44d1fcadde63d)]
12
+ - Add customization [Tiberiu Ichim - [`2d3b3b8`](https://github.com/eea/volto-cca-policy/commit/2d3b3b821a31b91caffccb280c376ff2285965e2)]
7
13
  ### [0.1.11](https://github.com/eea/volto-cca-policy/compare/0.1.10...0.1.11) - 21 March 2023
8
14
 
9
15
  #### :hammer_and_wrench: Others
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eeacms/volto-cca-policy",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "@eeacms/volto-cca-policy: Volto add-on",
5
5
  "main": "src/index.js",
6
6
  "author": "European Environment Agency: IDM2 A-Team",
@@ -0,0 +1,3 @@
1
+ Copy of https://raw.githubusercontent.com/plone/volto/9c1af8bdb5d6abd78adbd08d6b86499d133471cb/src/helpers/Url/Url.js
2
+
3
+ fixes mail regex crash
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Url helper.
3
+ * @module helpers/Url
4
+ */
5
+
6
+ import { last, memoize } from 'lodash';
7
+ import {
8
+ urlRegex,
9
+ telRegex,
10
+ mailRegex,
11
+ } from '@plone/volto/helpers/Url/urlRegex';
12
+ import prependHttp from 'prepend-http';
13
+ import config from '@plone/volto/registry';
14
+
15
+ /**
16
+ * Get base url.
17
+ * @function getBaseUrl
18
+ * @param {string} url Url to be parsed.
19
+ * @return {string} Base url of content object.
20
+ */
21
+ export const getBaseUrl = memoize((url) => {
22
+ const { settings } = config;
23
+ if (url === undefined) return;
24
+
25
+ // We allow settings.nonContentRoutes to have strings (that are supposed to match
26
+ // ending strings of pathnames, so we are converting them to RegEx to match also
27
+ const normalized_nonContentRoutes = settings.nonContentRoutes.map((item) => {
28
+ if (item.test) {
29
+ return item;
30
+ } else {
31
+ return new RegExp(item + '$');
32
+ }
33
+ });
34
+
35
+ let adjustedUrl = normalized_nonContentRoutes.reduce(
36
+ (acc, item) => acc.replace(item, ''),
37
+ url,
38
+ );
39
+
40
+ adjustedUrl = adjustedUrl || '/';
41
+ return adjustedUrl === '/' ? '' : adjustedUrl;
42
+ });
43
+
44
+ /**
45
+ * Get parent url.
46
+ * @function getParentUrl
47
+ * @param {string} url Url to be parsed.
48
+ * @return {string} Parent url of content object.
49
+ */
50
+ export const getParentUrl = memoize((url) => {
51
+ return url.substring(0, url.lastIndexOf('/'));
52
+ });
53
+
54
+ /**
55
+ * Get id from url.
56
+ * @function getId
57
+ * @param {string} url Url to be parsed.
58
+ * @return {string} Id of content object.
59
+ */
60
+ export function getId(url) {
61
+ return last(url.replace(/\?.*$/, '').split('/'));
62
+ }
63
+
64
+ /**
65
+ * Get view of an url.
66
+ * @function getView
67
+ * @param {string} url Url to be parsed.
68
+ * @return {string} View of content object.
69
+ */
70
+ export function getView(url) {
71
+ const view = last(url.replace(/\?.*$/, '').split('/'));
72
+ if (
73
+ [
74
+ 'add',
75
+ 'layout',
76
+ 'contents',
77
+ 'edit',
78
+ 'delete',
79
+ 'diff',
80
+ 'history',
81
+ 'sharing',
82
+ 'controlpanel',
83
+ ].indexOf(view) === -1
84
+ ) {
85
+ return 'view';
86
+ }
87
+ return view === 'layout' ? 'edit' : view;
88
+ }
89
+
90
+ /**
91
+ * Flatten to app server URL - Given a URL if it starts with the API server URL
92
+ * this method flattens it (removes) the server part
93
+ * TODO: Update it when implementing non-root based app location (on a
94
+ * directory other than /, eg. /myapp)
95
+ * @method flattenToAppURL
96
+ * @param {string} url URL of the object
97
+ * @returns {string} Flattened URL to the app server
98
+ */
99
+ export function flattenToAppURL(url) {
100
+ const { settings } = config;
101
+ return (
102
+ url &&
103
+ url
104
+ .replace(settings.internalApiPath, '')
105
+ .replace(settings.apiPath, '')
106
+ .replace(settings.publicURL, '')
107
+ );
108
+ }
109
+ /**
110
+ * Given a URL it remove the querystring from the URL.
111
+ * @method stripQuerystring
112
+ * @param {string} url URL of the object
113
+ * @returns {string} URL without querystring
114
+ */
115
+ export function stripQuerystring(url) {
116
+ return url.replace(/\?.*$/, '');
117
+ }
118
+
119
+ /**
120
+ * Given a URL if it starts with the API server URL
121
+ * this method removes the /api or the /Plone part.
122
+ * @method toPublicURL
123
+ * @param {string} url URL of the object
124
+ * @returns {string} public URL
125
+ */
126
+ export function toPublicURL(url) {
127
+ const { settings } = config;
128
+ return settings.publicURL.concat(flattenToAppURL(url));
129
+ }
130
+
131
+ /**
132
+ * Returns true if the current view is a cms ui view
133
+ * @method isCmsUi
134
+ * @param {string} currentPathname pathname of the current view
135
+ * @returns {boolean} true if the current view is a cms ui view
136
+ */
137
+ export const isCmsUi = memoize((currentPathname) => {
138
+ const { settings } = config;
139
+ const fullPath = currentPathname.replace(/\?.*$/, '');
140
+ // WARNING:
141
+ // not working properly for paths like /editors or similar
142
+ // because the regexp test does not take that into account
143
+ // https://github.com/plone/volto/issues/870
144
+ return settings.nonContentRoutes.reduce(
145
+ (acc, route) => acc || new RegExp(route).test(`/${fullPath}`),
146
+ false,
147
+ );
148
+ });
149
+
150
+ /**
151
+ * Flatten to app server HTML - Given a text if it contains some urls that starts
152
+ * with the API server URL this method flattens it (removes) the server part.
153
+ * TODO: Update it when implementing non-root based app location (on a
154
+ * directory other than /, eg. /myapp)
155
+ * @method flattenHTMLToAppURL
156
+ * @param {string} html Some html snippet
157
+ * @returns {string} Same HTML with Flattened URLs to the app server
158
+ */
159
+ export function flattenHTMLToAppURL(html) {
160
+ const { settings } = config;
161
+ return settings.internalApiPath
162
+ ? html
163
+ .replace(new RegExp(settings.internalApiPath, 'g'), '')
164
+ .replace(new RegExp(settings.apiPath, 'g'), '')
165
+ : html.replace(new RegExp(settings.apiPath, 'g'), '');
166
+ }
167
+
168
+ /**
169
+ * Add the app url
170
+ * @method addAppURL
171
+ * @param {string} url URL of the object
172
+ * @returns {string} New URL with app
173
+ */
174
+ export function addAppURL(url) {
175
+ const { settings } = config;
176
+ return url.indexOf(settings.apiPath) === 0
177
+ ? url
178
+ : `${settings.apiPath}${url}`;
179
+ }
180
+
181
+ /**
182
+ * Given a URL expands it to the backend URL
183
+ * Useful when you have to actually call the backend from the
184
+ * frontend code (eg. ICS calendar download)
185
+ * It is seamless mode aware
186
+ * @method expandToBackendURL
187
+ * @param {string} url URL or path of the object
188
+ * @returns {string} New URL with the backend URL
189
+ */
190
+ export function expandToBackendURL(path) {
191
+ const { settings } = config;
192
+ const APISUFIX = settings.legacyTraverse ? '' : '/++api++';
193
+ let adjustedPath;
194
+ if (path.startsWith('http://') || path.startsWith('https://')) {
195
+ // flattenToAppURL first if we get a full URL
196
+ adjustedPath = flattenToAppURL(path);
197
+ } else {
198
+ // Next adds a / in front if not a full URL to make sure it's a valid relative path
199
+ adjustedPath = path[0] !== '/' ? `/${path}` : path;
200
+ }
201
+
202
+ let apiPath = '';
203
+ if (settings.internalApiPath && __SERVER__) {
204
+ apiPath = settings.internalApiPath;
205
+ } else if (settings.apiPath) {
206
+ apiPath = settings.apiPath;
207
+ }
208
+
209
+ return `${apiPath}${APISUFIX}${adjustedPath}`;
210
+ }
211
+
212
+ /**
213
+ * Check if internal url
214
+ * @method isInternalURL
215
+ * @param {string} url URL of the object
216
+ * @returns {boolean} True if internal url
217
+ */
218
+ export function isInternalURL(url) {
219
+ const { settings } = config;
220
+ return (
221
+ url &&
222
+ (url.indexOf(settings.publicURL) !== -1 ||
223
+ (settings.internalApiPath &&
224
+ url.indexOf(settings.internalApiPath) !== -1) ||
225
+ url.indexOf(settings.apiPath) !== -1 ||
226
+ url.charAt(0) === '/' ||
227
+ url.charAt(0) === '.' ||
228
+ url.startsWith('#'))
229
+ );
230
+ }
231
+
232
+ /**
233
+ * Check if it's a valid url
234
+ * @method isUrl
235
+ * @param {string} url URL of the object
236
+ * @returns {boolean} True if is a valid url
237
+ */
238
+ export function isUrl(url) {
239
+ return urlRegex().test(url);
240
+ }
241
+
242
+ /**
243
+ * Normalize URL, adds protocol (if required eg. user has not entered the protocol)
244
+ * @method normalizeUrl
245
+ * @param {string} url URL of the object
246
+ * @returns {boolean} URL with the protocol
247
+ */
248
+ export function normalizeUrl(url) {
249
+ return prependHttp(url);
250
+ }
251
+
252
+ /**
253
+ * Removes protocol from URL (for display)
254
+ * @method removeProtocol
255
+ * @param {string} url URL of the object
256
+ * @returns {string} URL without the protocol part
257
+ */
258
+ export function removeProtocol(url) {
259
+ return url.replace('https://', '').replace('http://', '');
260
+ }
261
+
262
+ export function isMail(text) {
263
+ return mailRegex().test(text);
264
+ }
265
+
266
+ export function isTelephone(text) {
267
+ return telRegex().test(text);
268
+ }
269
+
270
+ export function normaliseMail(email) {
271
+ if (email?.toLowerCase()?.startsWith('mailto:')) {
272
+ return email;
273
+ }
274
+ return `mailto:${email}`;
275
+ }
276
+
277
+ export function normalizeTelephone(tel) {
278
+ if (tel?.toLowerCase()?.startsWith('tel:')) {
279
+ return tel;
280
+ }
281
+ return `tel:${tel}`;
282
+ }
283
+
284
+ export function checkAndNormalizeUrl(url) {
285
+ let res = {
286
+ isMail: false,
287
+ isTelephone: false,
288
+ url: url,
289
+ isValid: true,
290
+ };
291
+ if (URLUtils.isMail(URLUtils.normaliseMail(url))) {
292
+ //Mail
293
+ res.isMail = true;
294
+ res.url = URLUtils.normaliseMail(url);
295
+ } else if (URLUtils.isTelephone(url)) {
296
+ //Phone
297
+ res.isTelephone = true;
298
+ res.url = URLUtils.normalizeTelephone(url);
299
+ } else {
300
+ //url
301
+ if (res.url && !res.url.startsWith('/') && !res.url.startsWith('#')) {
302
+ res.url = URLUtils.normalizeUrl(url);
303
+ if (!URLUtils.isUrl(res.url)) {
304
+ res.isValid = false;
305
+ }
306
+ }
307
+ if (res.url === undefined || res.url === null) res.isValid = false;
308
+ }
309
+ return res;
310
+ }
311
+
312
+ export const URLUtils = {
313
+ normalizeTelephone,
314
+ normaliseMail,
315
+ normalizeUrl,
316
+ isTelephone,
317
+ isMail,
318
+ isUrl,
319
+ checkAndNormalizeUrl,
320
+ };