File size: 7,280 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { html as toHtml } from '../indices-to-html';

/**
 * Adds markup to some common text patterns
 *
 * - Bullet lists
 * - Todo lists, WP style
 * - Inline `code` snippets
 * - Code fences
 * - Header: description/explanation paragraphs
 *
 * Note: This code is only meant to serve until a
 * proper parser can be built up to convert the
 * unstructured text into structured data. Since at
 * this time we still create HTML strings directly
 * and on every render this function will serve
 * sufficiently but it should not be looked upon
 * as good example code!
 * @param {string} text input list of blocks as HTML string
 * @returns {string} marked-up text
 */
const toBlocks = ( text ) =>
	text.split( '\n' ).reduce(
		( { out, inFence, inList }, raw, index, src ) => {
			if ( ! raw ) {
				if ( ! src[ index + 1 ] ) {
					return { out, inFence, inList };
				}
				return {
					out: out + '<br />',
					inFence,
					inList,
				};
			}

			// Blockquote and list start/end tags do not need to be wrapped in div/p
			const skipRegex = /(blockquote|ol|ul|li|div)(.*)>/i;
			const shouldSkipWrap = skipRegex.test( raw );
			if ( shouldSkipWrap ) {
				return {
					out: out + raw,
					inFence,
					inList,
				};
			}

			// detect code fences
			// ```js
			// doFoo()
			// ```

			// code fence?
			// WordPress can replace `` with a fancy double-quote
			if ( /^(```|“`)[a-z1-9]*\s*$/i.test( raw ) ) {
				// opening a fence
				if ( ! inFence ) {
					return {
						out: out + '<pre><code>',
						inFence: true,
						inList,
					};
				}

				// closing a fence
				return {
					out: out + '</code></pre>',
					inFence: false,
					inList,
				};
			}

			// content inside a fence
			if ( inFence ) {
				return {
					out: out + raw + '\n',
					inFence,
					inList,
				};
			}

			// emphasized definition-like text
			// Some value: some description
			// Header: Value
			//
			// Not! This is fun. This: again; isn't emphasized.
			// May detect false positive if colon found in first sentence.
			const defined = /^[\w\s-_]+:/.test( raw )
				? `<strong>${ raw.split( ':' )[ 0 ] }:</strong>${ raw.replace( /^[^:]+:/, '' ) }`
				: raw;

			// inline `code` snippets
			const coded = defined.replace( /`([^`]+)`/, '<code>$1</code>' );

			// detect list items
			//  - one
			//  * two
			//  [bullet] three
			//  [dash] four
			if ( /^\s*[*\-\u2012-\u2015\u2022]\s/.test( coded ) ) {
				return {
					out:
						out +
						( inList ? '' : '<ul class="wpnc__body-list">' ) +
						`<li>${ coded.replace( /^\s*[*\-\u2012-\u2015\u2022]\s/, '' ) }</li>`,
					inFence,
					inList: true,
				};
			}

			// detect todo lists
			//  x Done
			//  o Not done
			//  O Also not done
			// X also done

			if ( /^\s*x\s/i.test( coded ) ) {
				return {
					out:
						out +
						( inList ? '' : '<ul class="wpnc__body-todo">' ) +
						`<li class="wpnc__todo-done">${ coded.replace( /^\s*x\s/i, '' ) }</li>`,
					inFence,
					inList: true,
				};
			}

			if ( /^\s*o\s/i.test( coded ) ) {
				return {
					out:
						out +
						( inList ? '' : '<ul class="wpnc__body-todo">' ) +
						`<li class="wpnc__todo-not-done">${ coded.replace( /^\s*o\s/i, '' ) }</li>`,
					inFence,
					inList: true,
				};
			}

			// Return a basic paragraph
			// anything else…
			return {
				out: out + ( inList ? '</ul>' : '' ) + `<div>${ coded }</div>`,
				inFence,
				inList: false,
			};
		},
		{ out: '', inFence: false, inList: false }
	).out;

export function internalP( html ) {
	return html.split( '\n\n' ).map( ( chunk, i ) => {
		const key = `block-text-${ i }-${ chunk.length }-${ chunk.slice( 0, 3 ) }-${ chunk.slice(
			-3
		) }`;
		const blocks = toBlocks( chunk );

		return (
			<div
				key={ key }
				// eslint-disable-next-line react/no-danger
				dangerouslySetInnerHTML={ {
					__html: blocks,
				} }
			/>
		);
	} );
}

export function p( html, className ) {
	if ( undefined === className ) {
		className = 'wpnc__paragraph';
	}
	const blocks = toBlocks( html );

	const result = (
		<div
			className={ className }
			// eslint-disable-next-line react/no-danger
			dangerouslySetInnerHTML={ {
				__html: blocks,
			} }
		/>
	);
	return result;
}

export const pSoup = ( items ) => items.map( toHtml ).map( internalP );

export function getSignature( blocks, note ) {
	if ( ! blocks || ! blocks.length ) {
		return [];
	}

	return blocks.map( function ( block ) {
		let type = 'text';
		let id = null;

		if ( 'undefined' !== typeof block.type ) {
			type = block.type;
		}

		if ( note && note.meta && note.meta.ids && note.meta.ids.reply_comment ) {
			if (
				block.ranges &&
				block.ranges.length > 1 &&
				block.ranges[ 1 ].id === note.meta.ids.reply_comment
			) {
				type = 'reply';
				id = block.ranges[ 1 ].id;
			}
		}

		if (
			'undefined' === typeof block.meta ||
			'undefined' === typeof block.meta.ids ||
			Object.keys( block.meta.ids ).length < 1
		) {
			return { type: type, id: id };
		}

		if ( 'undefined' !== typeof block.meta.ids.prompt ) {
			type = 'prompt';
			id = block.meta.ids.prompt;
		} else if ( 'undefined' !== typeof block.meta.ids.comment ) {
			type = 'comment';
			id = block.meta.ids.comment;
		} else if ( 'undefined' !== typeof block.meta.ids.post ) {
			type = 'post';
			id = block.meta.ids.post;
		} else if ( 'undefined' !== typeof block.meta.ids.user ) {
			type = 'user';
			id = block.meta.ids.user;
		}

		return { type: type, id: id };
	} );
}

export function formatString() {
	const args = [].slice.apply( arguments );
	const str = args.shift();

	return str.replace( /{(\d+)}/g, function ( match, number ) {
		return typeof args[ number ] !== 'undefined' ? args[ number ] : match;
	} );
}

export function zipWithSignature( blocks, note ) {
	const signature = getSignature( blocks, note );

	return blocks.map( function ( block, i ) {
		return {
			block: block,
			signature: signature[ i ],
		};
	} );
}

export const validURL =
	/^(?:http(?:s?):\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|blog|[a-z]{2}))|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |/.,*:;=]|%[a-f\d]{2})*)?$/i;

export const linkProps = ( note, block ) => {
	const { site: noteSite, comment, post } = note?.meta?.ids ?? {};
	const { site: blockSite } = block?.meta?.ids ?? {};

	const site = block ? blockSite : noteSite;

	let type;

	if ( block ) {
		type = 'site';
	} else if ( comment ) {
		type = 'comment';
	} else if ( post ) {
		type = 'post';
	}

	// if someone's home url is not to a wp site (twitter etc)
	if ( type === 'site' && ! site ) {
		return {};
	}

	switch ( type ) {
		case 'site':
		case 'post':
		case 'comment':
			return Object.fromEntries(
				[
					[ 'data-link-type', type ],
					[ 'data-site-id', site ],
					[ 'data-post-id', post ],
					[ 'data-comment-id', comment ],
				].filter( ( [ , val ] ) => !! val )
			);
		default:
			return {};
	}
};