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