@@ -14,104 +14,6 @@ const models = {
1414 Asset : require ( './asset' )
1515}
1616
17- const findLinkedNode = ( allNodes , linkPath ) => {
18- const leafSlug = linkPath . pop ( )
19- const leafRe = new RegExp ( `^${ leafSlug } $` , 'i' )
20- const leafMatches = allNodes . filter ( p => p . slug . match ( leafRe ) )
21-
22- if ( ! leafMatches . length ) {
23- return undefined
24- }
25-
26- if ( leafMatches . length === 1 ) {
27- return leafMatches [ 0 ]
28- }
29-
30- if ( ! linkPath . length ) {
31- return undefined
32- }
33-
34- const paths = linkPath . reverse ( )
35- return leafMatches . find ( node => {
36- let ctx = node . context
37- for ( const path of paths ) {
38- ctx = ctx . throwUntil ( item => {
39- return item . slug ?. match ( new RegExp ( `^${ path } $` , 'i' ) )
40- } )
41- }
42- return ! ! ctx . items . length
43- } )
44- }
45-
46- const linkNodes = ( nodes ) => {
47- nodes . forEach ( node => {
48- Object . keys ( node ) . forEach ( key => {
49- const value = node [ key ]
50- if ( Array . isArray ( value ) ) {
51- for ( let i = 0 ; i < value . length ; i ++ ) {
52- let valueItem = value [ i ]
53- if ( ! valueItem . linkPath ) {
54- break
55- }
56- const linkedNode = findLinkedNode ( nodes , valueItem . linkPath )
57- if ( linkedNode ) {
58- node [ key ] [ i ] = Object . assign ( { } , linkedNode )
59- linkBack ( node , linkedNode , key )
60- } else {
61- node [ key ] . splice ( i , 1 )
62- i --
63- }
64- }
65- } else {
66- if ( ! value ?. linkPath ) {
67- return
68- }
69- const linkedNode = findLinkedNode ( nodes , value . linkPath )
70- if ( linkedNode ) {
71- node [ key ] = Object . assign ( { } , linkedNode )
72- linkBack ( node , linkedNode , key )
73- } else {
74- node [ key ] = undefined
75- }
76- }
77- } )
78- } )
79- }
80-
81- const linkBack = ( post , entry , key ) => {
82- if ( entry . schema ) {
83- Object . keys ( entry . schema ) . forEach ( schemaKey => {
84- const schemaValue = entry . schema [ schemaKey ]
85- const isSchemaValueArray = Array . isArray ( schemaValue )
86- const re = new RegExp ( `^\\+(${ post . contentType } |):${ key } $` )
87- const match = isSchemaValueArray ?
88- schemaValue . find ( v => re . test ( v ) ) :
89- re . test ( schemaValue )
90- if ( match ) {
91- if ( isSchemaValueArray ) {
92- // console.log('linking', post.title, 'to', schemaKey, 'field of', entry.title)
93- entry [ schemaKey ] = entry [ schemaKey ] || [ ]
94- entry [ schemaKey ] . push ( post )
95- } else {
96- entry [ schemaKey ] = post
97- }
98- }
99- } )
100- return
101- }
102- entry . links = entry . links || { }
103- entry . links . relations = entry . links . relations || [ ]
104- const relation = entry . links . relations . find ( r => r . key === key )
105- if ( relation ) {
106- relation . entries . push ( post )
107- } else {
108- entry . links . relations . push ( {
109- key,
110- entries : [ post ]
111- } )
112- }
113- }
114-
11517const defaultSettings = {
11618 permalinkPrefix : '/' ,
11719 out : resolve ( '.' ) ,
@@ -129,6 +31,8 @@ const defaultSettings = {
12931class ContentModel extends ContentModelEntryNode {
13032 static serialize ( contentModel ) {
13133 return {
34+ ...contentModel ,
35+ ...contentModel . serializeLinks ( ) ,
13236 homepage : models . Homepage . serialize ( contentModel . subtree . homepage ) ,
13337 subpages : contentModel . subtree . subpages . map ( models . Subpage . serialize ) ,
13438 collections : contentModel . subtree . collections . map ( models . Collection . serialize ) ,
@@ -286,7 +190,7 @@ class ContentModel extends ContentModelEntryNode {
286190 } ]
287191 }
288192
289- afterEffects ( ) {
193+ linkNodes ( ) {
290194 const flatMapDeepCategories = ( container ) => {
291195 return _ . flatMapDeep ( container , ( { subtree } ) => {
292196 if ( subtree . categories . length ) {
@@ -299,12 +203,18 @@ class ContentModel extends ContentModelEntryNode {
299203 } )
300204 }
301205
302- linkNodes ( [
206+ const nodes = [
303207 ...this . subtree . subpages ,
304208 ...this . subtree . collections ,
305209 ...flatMapDeepCategories ( this . subtree . collections ) ,
306210 ..._ . flatMap ( this . subtree . collections , ( { subtree } ) => subtree . posts )
307- ] )
211+ ]
212+
213+ nodes . forEach ( node => node . resolveLinks ( nodes ) )
214+ }
215+
216+ afterEffects ( ) {
217+ this . linkNodes ( )
308218
309219 this . subtree . collections . forEach ( collection => {
310220 collection . afterEffects ( this . subtree )
0 commit comments