|
|
import { createElement } from 'react'; |
|
|
import { renderToStaticMarkup } from 'react-dom/server.browser'; |
|
|
import Gridicon from '../templates/gridicons'; |
|
|
import noticon2gridicon from '../utils/noticon2gridicon'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function render_range( new_sub_text, new_sub_range, range_info, range_data, options ) { |
|
|
|
|
|
let new_container = null; |
|
|
let type_mappings; |
|
|
const new_classes = []; |
|
|
|
|
|
let range_info_type = range_info.type; |
|
|
|
|
|
if ( typeof range_info.type !== 'undefined' ) { |
|
|
type_mappings = { |
|
|
b: 'strong', |
|
|
i: 'em', |
|
|
noticon: 'gridicon', |
|
|
}; |
|
|
|
|
|
|
|
|
if ( range_info.type in type_mappings ) { |
|
|
range_info_type = type_mappings[ range_info.type ]; |
|
|
} |
|
|
|
|
|
new_classes.push( `wpnc__${ range_info_type }` ); |
|
|
} |
|
|
|
|
|
|
|
|
switch ( range_info_type ) { |
|
|
|
|
|
|
|
|
|
|
|
case 'badge': |
|
|
if ( ! range_info.hasOwnProperty( 'width' ) ) { |
|
|
range_info.width = 256; |
|
|
} |
|
|
if ( ! range_info.hasOwnProperty( 'height' ) ) { |
|
|
range_info.height = 256; |
|
|
} |
|
|
case 'image': |
|
|
|
|
|
new_container = document.createElement( 'img' ); |
|
|
new_container.setAttribute( 'src', range_info.url ); |
|
|
if ( range_info.hasOwnProperty( 'width' ) ) { |
|
|
new_container.setAttribute( 'width', range_info.width ); |
|
|
} |
|
|
if ( range_info.hasOwnProperty( 'height' ) ) { |
|
|
new_container.setAttribute( 'height', range_info.height ); |
|
|
} |
|
|
if ( new_sub_text.trim().length > 0 ) { |
|
|
new_container.setAttribute( 'alt', new_sub_text ); |
|
|
} |
|
|
break; |
|
|
|
|
|
|
|
|
case 'blockquote': |
|
|
case 'cite': |
|
|
case 'hr': |
|
|
case 'p': |
|
|
case 'br': |
|
|
case 'div': |
|
|
case 'code': |
|
|
case 'pre': |
|
|
case 'span': |
|
|
case 'strong': |
|
|
case 'em': |
|
|
case 'sub': |
|
|
case 'sup': |
|
|
case 'del': |
|
|
case 's': |
|
|
case 'ol': |
|
|
case 'ul': |
|
|
case 'li': |
|
|
case 'h1': |
|
|
case 'h2': |
|
|
case 'h3': |
|
|
case 'h4': |
|
|
case 'h5': |
|
|
case 'h6': |
|
|
case 'figure': |
|
|
case 'figcaption': |
|
|
switch ( range_info_type ) { |
|
|
case 'list': |
|
|
range_info_type = 'span'; |
|
|
break; |
|
|
} |
|
|
new_container = document.createElement( range_info_type ); |
|
|
if ( range_info.hasOwnProperty( 'class' ) ) { |
|
|
new_classes.push( range_info.class ); |
|
|
} |
|
|
if ( range_info.hasOwnProperty( 'style' ) ) { |
|
|
new_container.setAttribute( 'style', range_info.style ); |
|
|
} |
|
|
build_chunks( new_sub_text, new_sub_range, range_data, new_container, options ); |
|
|
break; |
|
|
case 'gridicon': |
|
|
|
|
|
new_container = document.createElement( 'span' ); |
|
|
new_container.innerHTML = renderToStaticMarkup( |
|
|
createElement( Gridicon, { |
|
|
icon: noticon2gridicon( range_info.value ), |
|
|
size: 18, |
|
|
} ) |
|
|
); |
|
|
break; |
|
|
case 'button': |
|
|
new_classes.push( 'is-primary' ); |
|
|
default: |
|
|
|
|
|
if ( ( options.links && range_info.url ) || range_info_type === 'a' ) { |
|
|
|
|
|
new_container = document.createElement( 'a' ); |
|
|
new_container.setAttribute( 'href', range_info.url ); |
|
|
if ( range_info.hasOwnProperty( 'class' ) ) { |
|
|
new_classes.push( range_info.class ); |
|
|
} |
|
|
if ( range_info.hasOwnProperty( 'style' ) ) { |
|
|
new_container.setAttribute( 'style', range_info.style ); |
|
|
} |
|
|
if ( range_info_type === 'stat' ) { |
|
|
|
|
|
new_container.setAttribute( 'target', '_parent' ); |
|
|
} else { |
|
|
|
|
|
new_container.setAttribute( 'target', '_blank' ); |
|
|
new_container.setAttribute( 'rel', 'noopener noreferrer' ); |
|
|
} |
|
|
|
|
|
if ( 'post' === range_info_type ) { |
|
|
new_container.setAttribute( 'data-post-id', range_info.id ); |
|
|
new_container.setAttribute( 'data-site-id', range_info.site_id ); |
|
|
new_container.setAttribute( 'data-link-type', 'post' ); |
|
|
new_container.setAttribute( 'target', '_self' ); |
|
|
} else if ( |
|
|
( 'tracks' === range_info_type || 'button' === range_info_type ) && |
|
|
range_info.context |
|
|
) { |
|
|
new_container.setAttribute( 'data-link-type', 'tracks' ); |
|
|
new_container.setAttribute( 'data-tracks-event', range_info.context ); |
|
|
} |
|
|
|
|
|
build_chunks( new_sub_text, new_sub_range, range_data, new_container, options ); |
|
|
} else { |
|
|
|
|
|
new_container = document.createElement( 'span' ); |
|
|
if ( new_sub_text.length > 0 ) { |
|
|
build_chunks( new_sub_text, new_sub_range, range_data, new_container, options ); |
|
|
} |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
if ( new_classes.length > 0 ) { |
|
|
new_container.className = new_classes.join( ' ' ); |
|
|
} |
|
|
|
|
|
return new_container; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function build_chunks( sub_text, sub_ranges, range_data, container, options ) { |
|
|
let text_start = null; |
|
|
let text_stop = null; |
|
|
|
|
|
const ranges = JSON.parse( JSON.stringify( sub_ranges ) ); |
|
|
|
|
|
|
|
|
|
|
|
for ( let i = 0; i < sub_ranges.length; i++ ) { |
|
|
if ( ranges[ i ].length === 0 ) { |
|
|
|
|
|
if ( text_start == null ) { |
|
|
|
|
|
text_start = i; |
|
|
} |
|
|
} else { |
|
|
if ( text_start != null ) { |
|
|
text_stop = i; |
|
|
|
|
|
|
|
|
container.appendChild( |
|
|
document.createTextNode( sub_text.substring( text_start, text_stop ) ) |
|
|
); |
|
|
text_start = null; |
|
|
text_stop = null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let range = null; |
|
|
for ( const potential_range of ranges[ i ] ) { |
|
|
|
|
|
|
|
|
if ( potential_range.parent ) { |
|
|
const parent_range = ranges[ i ].find( ( r ) => r.id === potential_range.parent ); |
|
|
if ( parent_range ) { |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( potential_range.len === 0 ) { |
|
|
range = potential_range; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
if ( null === range || potential_range.len > range.len ) { |
|
|
range = potential_range; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const range_info = range_data[ range.pos ]; |
|
|
const new_sub_text = sub_text.substr( i, range.len ); |
|
|
const new_sub_range = sub_ranges.slice( i, i + ( range.len > 0 ? range.len : 1 ) ); |
|
|
|
|
|
for ( let j = 0; j < new_sub_range.length; j++ ) { |
|
|
|
|
|
|
|
|
new_sub_range[ j ] = new_sub_range[ j ].filter( ( r ) => range.parent !== r.parent ); |
|
|
} |
|
|
|
|
|
container.appendChild( |
|
|
render_range( new_sub_text, new_sub_range, range_info, range_data, options ) |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ranges[ i ] = ranges[ i ].filter( ( sub_range ) => sub_range.len > 0 ); |
|
|
|
|
|
i += range.len - 1; |
|
|
} |
|
|
} |
|
|
if ( text_start != null ) { |
|
|
|
|
|
|
|
|
container.appendChild( |
|
|
document.createTextNode( sub_text.substring( text_start, sub_text.length ) ) |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
container.normalize(); |
|
|
} |
|
|
|
|
|
function recurse_convert( text, ranges, options ) { |
|
|
const container = document.createDocumentFragment(); |
|
|
const ranges_copy = JSON.parse( JSON.stringify( ranges ) ); |
|
|
const t = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( text.length > 0 ) { |
|
|
for ( let i = 0; i < text.length; i++ ) { |
|
|
t[ i ] = []; |
|
|
} |
|
|
} else { |
|
|
t.push( [] ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ranges_copy.forEach( ( range, pos ) => { |
|
|
const { id, parent, indices } = range; |
|
|
const start = indices[ 0 ]; |
|
|
const stop = indices[ 1 ]; |
|
|
const len = stop - start; |
|
|
if ( len > 0 ) { |
|
|
for ( let i = start; i < stop; i++ ) { |
|
|
t[ i ].push( { id, len, parent, pos } ); |
|
|
} |
|
|
} else { |
|
|
if ( typeof t[ start ] === 'undefined' ) { |
|
|
t[ start ] = []; |
|
|
} |
|
|
t[ start ].push( { id, len, parent, pos } ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
build_chunks( text, t, ranges, container, options ); |
|
|
return container; |
|
|
} |
|
|
|
|
|
export function convert( blob, options ) { |
|
|
let ranges = new Array(); |
|
|
|
|
|
if ( typeof options !== 'object' ) { |
|
|
options = {}; |
|
|
} |
|
|
options.links = 'undefined' === typeof options.links ? true : options.links; |
|
|
ranges = ranges.concat( blob.ranges || [] ); |
|
|
ranges = ranges.concat( blob.media || [] ); |
|
|
return recurse_convert( blob.text, ranges, options ); |
|
|
} |
|
|
|
|
|
export function html( blob, options ) { |
|
|
const div = document.createElement( 'div' ); |
|
|
div.appendChild( convert( blob, options ) ); |
|
|
return div.innerHTML; |
|
|
} |
|
|
|
|
|
export default convert; |
|
|
|