1+ export default function ( eleventyConfig ) {
2+ eleventyConfig . addCollection ( "series" , function ( collection ) {
3+ const posts = collection . getAll ( ) ;
4+
5+ // this will store the mapping from series to lists of posts; it can be a
6+ // regular object if you prefer
7+ const mapping = new Map ( ) ;
8+
9+ // loop over the posts
10+ for ( const post of posts ) {
11+
12+ // ignore anything with no series data
13+ if ( post . data . series === undefined ) {
14+ continue ;
15+ }
16+
17+ for ( var key of post . data . series )
18+ {
19+ if ( ! mapping . has ( key ) )
20+ {
21+ mapping . set ( key , [ ] ) ;
22+ }
23+ mapping . get ( key ) . push ( post ) ;
24+ }
25+ }
26+
27+ for ( const [ key , value ] of mapping . entries ( ) )
28+ {
29+ value . sort ( ( a , b ) => a . date - b . date ) ;
30+ }
31+
32+ return Object . fromEntries ( mapping ) ;
33+ } ) ;
34+
35+ eleventyConfig . addCollection ( "tags" , function ( collection ) {
36+ const excludedTags = [ "posts" , "all" ] ;
37+ const posts = collection . getAll ( ) ;
38+
39+ // this will store the mapping from series to lists of posts; it can be a
40+ // regular object if you prefer
41+ const mapping = new Map ( ) ;
42+
43+ // loop over the posts
44+ for ( const post of posts ) {
45+
46+ // ignore anything with no series data
47+ if ( post . data . tags === undefined ) {
48+ continue ;
49+ }
50+
51+ for ( var key of post . data . tags )
52+ {
53+ if ( excludedTags . indexOf ( key ) !== - 1 ) continue ;
54+
55+ if ( ! mapping . has ( key ) )
56+ {
57+ mapping . set ( key , [ ] ) ;
58+ }
59+ mapping . get ( key ) . push ( post ) ;
60+ }
61+ }
62+
63+ for ( const [ key , value ] of mapping . entries ( ) )
64+ {
65+ value . sort ( ( a , b ) => a . date - b . date ) ;
66+ }
67+
68+ return Object . fromEntries ( mapping ) ;
69+ } ) ;
70+ }
0 commit comments