|
|
import { deserialize as _recurse } from '../'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const REGEXP_CAPTION_CONTENT = /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i; |
|
|
const REGEXP_CAPTION_ID = /attachment_(\d+)/; |
|
|
const REGEXP_CAPTION_ALIGN = /align(\w+)/; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseCaption( node, _parsed ) { |
|
|
|
|
|
if ( node.attrs && node.attrs.named ) { |
|
|
|
|
|
if ( REGEXP_CAPTION_ALIGN.test( node.attrs.named.align ) ) { |
|
|
_parsed.appearance.align = node.attrs.named.align.match( REGEXP_CAPTION_ALIGN )[ 1 ]; |
|
|
} |
|
|
|
|
|
|
|
|
if ( REGEXP_CAPTION_ID.test( node.attrs.named.id ) ) { |
|
|
_parsed.media.ID = parseInt( node.attrs.named.id.match( REGEXP_CAPTION_ID )[ 1 ], 10 ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( _parsed.media.width && isFinite( node.attrs.named.width ) ) { |
|
|
_parsed.media.width = parseInt( node.attrs.named.width, 10 ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( ! node.content ) { |
|
|
return _parsed; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const img = node.content.match( REGEXP_CAPTION_CONTENT ); |
|
|
if ( img && img[ 2 ] ) { |
|
|
_parsed.media.caption = img[ 2 ].trim(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( img ) { |
|
|
return _recurse( img[ 1 ], _parsed ); |
|
|
} |
|
|
|
|
|
return _parsed; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function deserialize( node, _parsed = { media: {}, appearance: {} } ) { |
|
|
switch ( node.tag ) { |
|
|
case 'caption': |
|
|
_parsed = parseCaption( node, _parsed ); |
|
|
break; |
|
|
} |
|
|
|
|
|
return _parsed; |
|
|
} |
|
|
|