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 | 18× 40× 18× 1× 34× 34× 34× 40× 40× 40× 40× 34× 34× 1× 1× 18× 18× 1× 44× 44× 44× 44× 54× 54× 18× 18× 36× 72× 72× 90× 6× 84× 44× 1× 22× 1× 36× 1× 1× 102× 102× 102× 186× 186× 102× 102× 1× 18× 4× 4× 4× 4× 1× 4× 2× 4× 4× 2× 2× 1× 34× 34× 34× 1× 1× 2× 1× 2× 1× | import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { EcanUseDOM } from 'exenv'; import withSideEffect from 'react-side-effect'; import { clone, defaults, forEach } from './utils'; function reducePropsTostate ( propsList ) { const props = {}; let extend = true; for (var i = propsList.length - 1; extend && i >= 0; i--) { const _props = clone(propsList[i]); if ( _props.hasOwnProperty('description') ) { defaults(_props, { meta: { name: { description: _props.description } } } ); } if ( _props.hasOwnProperty('canonical') ) { defaults(_props, { link: { rel: { canonical: _props.canonical } } } ); } defaults(props, _props); extend = _props.hasOwnProperty('extend'); } if (Iprops.auto) { autoProps( props ); } return props; } function autoProps ( props ) { if (props.auto.ograph === true) { ograph(props); } return props; } function handleStateChangeOnClient ( props ) { if ( canUseDOM ) { document.title = props.title || ''; insertDocumentMeta( props ); } } function ograph ( p ) { if (!p.meta) { p.meta = {}; } if (!p.meta.property) { p.meta.property = {}; } const group = p.meta.property; if ( group ) { if ( p.title && !group['og:title'] ) { group['og:title'] = p.title; } if ( p.hasOwnProperty('description') && !group['og:description'] ) { group['og:description'] = p.description; } if ( p.hasOwnProperty('canonical') && !group['og:url'] ) { group['og:url'] = p.canonical; } } return p; } function parseTags ( tagName, props = {} ) { const tags = []; const contentKey = tagName === 'link' ? 'href' : 'content'; Object.keys( props ).forEach(( groupKey ) => { const group = props[groupKey]; if (typeof group === 'string') { tags.push({ tagName, [ groupKey ]: group }); return; } Object.keys( group ).forEach(( key ) => { const values = Array.isArray(group[key]) ? group[key] : [group[key]]; values.forEach(( value ) => { if (value === null ) { return; } tags.push({ tagName, [ groupKey ]: key, [ contentKey ]: value }); }); }); }); return tags; } function getTags ( _props ) { return [].concat( parseTags( 'meta', _props.meta ), parseTags( 'link', _props.link ) ); } function removeNode ( node ) { node.parentNode.removeChild(node); } function removeDocumentMeta () { forEach(document.querySelectorAll('head [data-rdm]'), removeNode); } function insertDocumentMetaNode ( entry ) { const { tagName, ...attr } = entry; var newNode = document.createElement( tagName ); for (var prop in attr) { if (Eentry.hasOwnProperty(prop)) { newNode.setAttribute(prop, entry[prop]); } } newNode.setAttribute('data-rdm', ''); document.getElementsByTagName('head')[0].appendChild(newNode); } function insertDocumentMeta ( props ) { removeDocumentMeta(); forEach( getTags( props ), insertDocumentMetaNode); } function render ( meta = {}, opts ) { if ( Itypeof opts !== 'object' ) { return meta; } let i = 0; const tags = []; function renderTag ( entry ) { const { tagName, ...attr } = entry; if ( tagName === 'meta' ) { return ( <meta {...attr} key={ i++ } data-rdm /> ); } if ( tagName === 'link' ) { return ( <link {...attr} key={ i++ } data-rdm /> ); } return null; } if ( meta.title ) { tags.push( <title key={ i++ }>{ meta.title }</title> ); } getTags( meta ).reduce(( acc, entry ) => { tags.push( renderTag(entry) ); return tags; }, tags); if ( opts.asReact ) { return tags; } return renderToStaticMarkup( <div>{ tags }</div> ).replace(/(^<div>|<\/div>$)/g, ''); } const DocumentMeta = React.createClass({ displayName: 'DocumentMeta', propTypes: { title: React.PropTypes.string, description: React.PropTypes.string, canonical: React.PropTypes.string, meta: React.PropTypes.objectOf( React.PropTypes.oneOfType([ React.PropTypes.string, React.PropTypes.objectOf( React.PropTypes.oneOfType([ React.PropTypes.string, React.PropTypes.arrayOf(React.PropTypes.string) ]) ) ]) ), link: React.PropTypes.objectOf( React.PropTypes.objectOf( React.PropTypes.oneOfType([ React.PropTypes.string, React.PropTypes.arrayOf(React.PropTypes.string) ]) ) ), auto: React.PropTypes.objectOf(React.PropTypes.bool) }, render: function render () { const { children } = this.props; const count = React.Children.count(children); return count === 1 ? React.Children.only(children) : ( count ? <div>{ this.props.children }</div> : null ); } }); const DocumentMetaWithSideEffect = withSideEffect( reducePropsTostate, handleStateChangeOnClient )(DocumentMeta); DocumentMetaWithSideEffect.renderAsReact = function rewindAsReact () { return render( DocumentMetaWithSideEffect.rewind(), { asReact: true } ); }; DocumentMetaWithSideEffect.renderAsHTML = function rewindAsHTML () { return render( DocumentMetaWithSideEffect.rewind(), { asHtml: true } ); }; export default DocumentMetaWithSideEffect; |