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 + '
', 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 + '
',
						inFence: true,
						inList,
					};
				}

				// closing a fence
				return {
					out: out + '
', 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 ) ? `${ raw.split( ':' )[ 0 ] }:${ raw.replace( /^[^:]+:/, '' ) }` : raw; // inline `code` snippets const coded = defined.replace( /`([^`]+)`/, '$1' ); // detect list items // - one // * two // [bullet] three // [dash] four if ( /^\s*[*\-\u2012-\u2015\u2022]\s/.test( coded ) ) { return { out: out + ( inList ? '' : '