File size: 5,974 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
import clsx from 'clsx';
import { createElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server.browser';
import * as MediaUtils from 'calypso/lib/media/utils';
import { deserialize } from 'calypso/lib/media-serialization';
import { parse, stringify } from 'calypso/lib/shortcode';

/**
 * Module variables
 */
const Markup = {
	/**
	 * Given a media object and a site, returns a markup string representing that object
	 * as HTML.
	 * @param  {Object} site    A site object
	 * @param  {Object} media   A media object
	 * @param  {Object} options Appearance options
	 * @returns {string}         A markup string
	 */
	get: function ( site, media, options ) {
		if ( ! media || media.hasOwnProperty( 'status' ) ) {
			return '';
		}

		const mimePrefix = MediaUtils.getMimePrefix( media );

		// Attempt to find a matching function in the mimeTypes object using
		// the MIME type prefix
		if ( mimePrefix && 'function' === typeof Markup.mimeTypes[ mimePrefix ] ) {
			return Markup.mimeTypes[ mimePrefix ]( site, media, options );
		}

		return Markup.link( media );
	},

	/**
	 * Given a media object, returns a link markup string representing that
	 * object.
	 * @param  {Object} media A media object
	 * @returns {string}       A link markup string
	 */
	link: function ( media ) {
		const element = createElement(
			'a',
			{
				href: media.URL,
				title: media.title,
			},
			media.title
		);

		return renderToStaticMarkup( element );
	},

	/**
	 * Given a media object or markup string and a site, returns a caption React element.
	 *
	 * Adapted from WordPress.
	 * @copyright 2015 by the WordPress contributors.
	 * @license LGPL-2.1
	 * @see https://github.com/WordPress/WordPress/blob/4.3/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js#L97-L157
	 * @param  {Object} site           A site object
	 * @param  {(Object | string)} media A media object or markup string
	 * @returns {string}                A caption React element, or null if not
	 *                                 a captioned item.
	 */
	caption: function ( site, media ) {
		let img;
		let caption;
		let width;

		if ( 'string' !== typeof media ) {
			media = Markup.get( site, media );
		}

		const parsed = parse( media );
		if ( ! parsed || ! parsed.content ) {
			return null;
		}

		const match = parsed.content.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
		if ( match ) {
			img = match[ 1 ].trim();
			caption = match[ 2 ].trim();
		}

		width = parsed.attrs.named.width;
		if ( ! width ) {
			width = deserialize( img ).width;
		}

		/*eslint-disable react/no-danger*/
		return (
			<dl
				className={ clsx( 'wp-caption', parsed.attrs.named.align, parsed.attrs.named.classes ) }
				style={ { width: parseInt( width, 10 ) } }
			>
				<dt className="wp-caption-dt" dangerouslySetInnerHTML={ { __html: img } } />
				<dd className="wp-caption-dd">{ caption }</dd>
			</dl>
		);
		/*eslint-enable react/no-danger*/
	},

	mimeTypes: {
		/**
		 * Given an image media object and a site, returns a markup string representing that
		 * image object as HTML.
		 * @param  {Object} site    A site object
		 * @param  {Object} media   An image media object
		 * @param  {Object} options Appearance options
		 * @returns {string}         An image markup string
		 */
		image: function ( site, media, options ) {
			options = {
				size: 'full',
				align: 'none',
				forceResize: false,
				...options,
			};

			let width;
			let height;
			if ( 'full' === options.size ) {
				width = media.width;
				height = media.height;
			} else {
				const dimensions = MediaUtils.getThumbnailSizeDimensions( options.size, site );
				const ratio = Math.min(
					dimensions.width / media.width || Infinity,
					dimensions.height / media.height || Infinity
				);

				width = Math.round( media.width * ratio );
				height = Math.round( media.height * ratio );
			}

			let urlOptions;
			if ( options.forceResize || ( site && ! site.jetpack && width && width !== media.width ) ) {
				urlOptions = { maxWidth: width };
			} else {
				urlOptions = { size: options.size };
			}

			const img = createElement( 'img', {
				src: MediaUtils.url( media, urlOptions ),
				alt: media.alt || media.title,
				width: isFinite( width ) ? width : null,
				height: isFinite( height ) ? height : null,
				className: clsx( 'align' + options.align, 'size-' + options.size, 'wp-image-' + media.ID ),
				// make data-istransient a boolean att https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attribute
				// it is false if it doesn't exist
				'data-istransient': media.transient ? 'istransient' : null,
			} );

			let markup = renderToStaticMarkup( img );
			if ( media.caption && width ) {
				markup = stringify( {
					tag: 'caption',
					attrs: {
						id: 'attachment_' + media.ID,
						width: width,
					},
					content: [ markup, media.caption ].join( ' ' ),
				} );
			}

			return markup;
		},

		/**
		 * Given an audio media object, returns a markup string representing that
		 * audio object as HTML.
		 * @param  {Object} site  A site object
		 * @param  {Object} media An audio media object
		 * @returns {string}       An audio markup string
		 */
		audio: function ( site, media ) {
			return stringify( {
				tag: 'audio',
				attrs: {
					src: media.URL,
				},
			} );
		},

		/**
		 * Given a video media object, returns a markup string representing that
		 * video object as HTML.
		 * @param  {Object} site  A site object
		 * @param  {string} media A video media object
		 * @returns {string}       A video markup string
		 */
		video: function ( site, media ) {
			if ( MediaUtils.isVideoPressItem( media ) ) {
				return stringify( {
					tag: 'wpvideo',
					attrs: [ media.videopress_guid ],
					type: 'single',
				} );
			}

			return stringify( {
				tag: 'video',
				attrs: {
					src: media.URL,
					height: media.height,
					width: media.width,
				},
			} );
		},
	},
};

export default Markup;