@@ -6,6 +6,7 @@ const HELMET_PROPS = {
6
6
ENCODE_SPECIAL_CHARACTERS : 'encodeSpecialCharacters' ,
7
7
ON_CHANGE_CLIENT_STATE : 'onChangeClientState' ,
8
8
TITLE_TEMPLATE : 'titleTemplate' ,
9
+ PRIORITIZE_SEO_TAGS : 'prioritizeSeoTags' ,
9
10
} ;
10
11
11
12
const getInnermostProperty = ( propsList , property ) => {
@@ -20,6 +21,11 @@ const getInnermostProperty = (propsList, property) => {
20
21
return null ;
21
22
} ;
22
23
24
+ const getAnyTrueFromPropsList = ( propsList , checkedTag ) =>
25
+ propsList . reduce ( ( accumulator , currentPropList ) =>
26
+ currentPropList [ checkedTag ] ? true : accumulator
27
+ ) ;
28
+
23
29
const getTitleFromPropsList = propsList => {
24
30
let innermostTitle = getInnermostProperty ( propsList , TAG_NAMES . TITLE ) ;
25
31
const innermostTemplate = getInnermostProperty ( propsList , HELMET_PROPS . TITLE_TEMPLATE ) ;
@@ -170,39 +176,76 @@ const getTagsFromPropsList = (tagName, primaryAttributes, propsList) => {
170
176
. reverse ( ) ;
171
177
} ;
172
178
173
- const reducePropsToState = propsList => ( {
174
- baseTag : getBaseTagFromPropsList ( [ TAG_PROPERTIES . HREF ] , propsList ) ,
175
- bodyAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . BODY , propsList ) ,
176
- defer : getInnermostProperty ( propsList , HELMET_PROPS . DEFER ) ,
177
- encode : getInnermostProperty ( propsList , HELMET_PROPS . ENCODE_SPECIAL_CHARACTERS ) ,
178
- htmlAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . HTML , propsList ) ,
179
- linkTags : getTagsFromPropsList (
180
- TAG_NAMES . LINK ,
181
- [ TAG_PROPERTIES . REL , TAG_PROPERTIES . HREF ] ,
182
- propsList
183
- ) ,
184
- metaTags : getTagsFromPropsList (
185
- TAG_NAMES . META ,
186
- [
187
- TAG_PROPERTIES . NAME ,
188
- TAG_PROPERTIES . CHARSET ,
189
- TAG_PROPERTIES . HTTPEQUIV ,
190
- TAG_PROPERTIES . PROPERTY ,
191
- TAG_PROPERTIES . ITEM_PROP ,
192
- ] ,
193
- propsList
194
- ) ,
195
- noscriptTags : getTagsFromPropsList ( TAG_NAMES . NOSCRIPT , [ TAG_PROPERTIES . INNER_HTML ] , propsList ) ,
196
- onChangeClientState : getOnChangeClientState ( propsList ) ,
197
- scriptTags : getTagsFromPropsList (
198
- TAG_NAMES . SCRIPT ,
199
- [ TAG_PROPERTIES . SRC , TAG_PROPERTIES . INNER_HTML ] ,
200
- propsList
201
- ) ,
202
- styleTags : getTagsFromPropsList ( TAG_NAMES . STYLE , [ TAG_PROPERTIES . CSS_TEXT ] , propsList ) ,
203
- title : getTitleFromPropsList ( propsList ) ,
204
- titleAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . TITLE , propsList ) ,
205
- } ) ;
179
+ // helper to inspect for matching props on components
180
+ export const checkIfPropsMatch = ( props , toMatch ) => {
181
+ const pairs = Object . entries ( props ) ;
182
+ for ( let i = 0 ; i < pairs . length ; i += 1 ) {
183
+ const [ propName , propVal ] = pairs [ i ] ;
184
+ if ( toMatch [ propName ] ) {
185
+ // e.g. if rel exists in the list of allowed props
186
+ const propWeAreCheckingOut = toMatch [ propName ] ; // e.g. [amphtml, alternate, etc]
187
+ if ( propWeAreCheckingOut . includes ( propVal ) ) {
188
+ return true ;
189
+ }
190
+ }
191
+ }
192
+ return false ;
193
+ } ;
194
+
195
+ // re-usable fn to prioritize tags by matching props
196
+ export const prioritizer = ( elementsList , propsToMatch ) => {
197
+ if ( Array . isArray ( elementsList ) ) {
198
+ return elementsList . reduce (
199
+ ( acc , elementAttrs ) => {
200
+ if ( checkIfPropsMatch ( elementAttrs , propsToMatch ) ) {
201
+ acc . priority . push ( elementAttrs ) ;
202
+ } else {
203
+ acc . default . push ( elementAttrs ) ;
204
+ }
205
+ return acc ;
206
+ } ,
207
+ { priority : [ ] , default : [ ] }
208
+ ) ;
209
+ }
210
+ return { default : elementsList } ;
211
+ } ;
212
+
213
+ const reducePropsToState = propsList => {
214
+ return {
215
+ baseTag : getBaseTagFromPropsList ( [ TAG_PROPERTIES . HREF ] , propsList ) ,
216
+ bodyAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . BODY , propsList ) ,
217
+ defer : getInnermostProperty ( propsList , HELMET_PROPS . DEFER ) ,
218
+ encode : getInnermostProperty ( propsList , HELMET_PROPS . ENCODE_SPECIAL_CHARACTERS ) ,
219
+ htmlAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . HTML , propsList ) ,
220
+ linkTags : getTagsFromPropsList (
221
+ TAG_NAMES . LINK ,
222
+ [ TAG_PROPERTIES . REL , TAG_PROPERTIES . HREF ] ,
223
+ propsList
224
+ ) ,
225
+ metaTags : getTagsFromPropsList (
226
+ TAG_NAMES . META ,
227
+ [
228
+ TAG_PROPERTIES . NAME ,
229
+ TAG_PROPERTIES . CHARSET ,
230
+ TAG_PROPERTIES . HTTPEQUIV ,
231
+ TAG_PROPERTIES . PROPERTY ,
232
+ TAG_PROPERTIES . ITEM_PROP ,
233
+ ] ,
234
+ propsList
235
+ ) ,
236
+ noscriptTags : getTagsFromPropsList ( TAG_NAMES . NOSCRIPT , [ TAG_PROPERTIES . INNER_HTML ] , propsList ) ,
237
+ onChangeClientState : getOnChangeClientState ( propsList ) ,
238
+ scriptTags : getTagsFromPropsList (
239
+ TAG_NAMES . SCRIPT ,
240
+ [ TAG_PROPERTIES . SRC , TAG_PROPERTIES . INNER_HTML ] ,
241
+ propsList
242
+ ) ,
243
+ styleTags : getTagsFromPropsList ( TAG_NAMES . STYLE , [ TAG_PROPERTIES . CSS_TEXT ] , propsList ) ,
244
+ title : getTitleFromPropsList ( propsList ) ,
245
+ titleAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . TITLE , propsList ) ,
246
+ prioritizeSeoTags : getAnyTrueFromPropsList ( propsList , HELMET_PROPS . PRIORITIZE_SEO_TAGS ) ,
247
+ } ;
248
+ } ;
206
249
207
250
export const flattenArray = possibleArray =>
208
251
Array . isArray ( possibleArray ) ? possibleArray . join ( '' ) : possibleArray ;
0 commit comments