1+ import { EntryModel } from "." ;
2+
3+ export function addTags ( entry : EntryModel , contentTypeUid : string , tagsAsObject : boolean , locale : string = 'en-us' ) : void {
4+ if ( entry )
5+ entry [ "$" ] = getTag ( entry , `${ contentTypeUid } .${ entry . uid } .${ locale } ` , tagsAsObject , locale )
6+ }
7+
8+ function getTag ( content : object , prefix : string , tagsAsObject : boolean , locale : string ) : object {
9+ let tags : any = { }
10+ Object . entries ( content ) . forEach ( ( [ key , value ] ) => {
11+ switch ( typeof value ) {
12+ case "object" :
13+ if ( Array . isArray ( value ) ) {
14+ value . forEach ( ( obj , index ) => {
15+ if ( ( typeof obj !== 'undefined' || obj !== null ) && obj . _content_type_uid !== undefined && obj . uid !== undefined ) {
16+ value [ index ] [ '$' ] = getTag ( obj , `${ obj . _content_type_uid } .${ obj . uid } .${ obj . locale || locale } ` , tagsAsObject , locale )
17+ } else {
18+ if ( typeof obj === "object" ) {
19+ obj [ '$' ] = getTag ( obj , `${ prefix } .${ key } .${ index } ` , tagsAsObject , locale )
20+ } else {
21+ tags [ key ] = getTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
22+ }
23+ }
24+ } )
25+ } else {
26+ if ( value ) {
27+ value [ "$" ] = getTag ( value , `${ prefix } .${ key } ` , tagsAsObject , locale )
28+ }
29+ }
30+ break ;
31+ default :
32+ tags [ key ] = getTagsValue ( `${ prefix } .${ key } ` , tagsAsObject )
33+ }
34+ } )
35+ return tags
36+ }
37+
38+ function getTagsValue ( dataValue :string , tagsAsObject : boolean ) : any {
39+ if ( tagsAsObject ) {
40+ return { "data-cslp" : dataValue } ;
41+ } else {
42+ return `data-cslp=${ dataValue } ` ;
43+ }
44+ }
0 commit comments