-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpost.macros.njk
More file actions
848 lines (789 loc) · 18.2 KB
/
Copy pathpost.macros.njk
File metadata and controls
848 lines (789 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
{% import 'birds.macros.njk' as birds %}
{% import 'layout.macros.njk' as layout %}
{% import 'embed.macros.njk' as embed %}
{% import 'utility.macros.njk' as utility %}
{% import 'talks.macros.njk' as talks %}
{# @docs
label: Post Macros
category: file
#}
{# @docs
label: by_year
category: lists
note: |
Group posts by year,
when there are more than
a set (`above`) number of posts
params:
posts:
type: array
note: containing 11ty page objects
collections:
type: 11ty collections object
default: none
above:
type: number
default: 6
pagination:
type: 11ty pagination object
default: none
#}
{% macro by_year(
posts,
collections=none,
above=6,
pagination=none,
label='Posts'
) %}
{% set posts = posts | getPublic %}
{%- if posts | length -%}
{%- if posts | length > above -%}
{%- for group in posts | eventSort | byYear -%}
{% set title = [group.year, label] | join(' ') if loop.first else group.year %}
{{ list(
posts=group.posts,
collections=collections,
title=title,
pagination=pagination if loop.first else none
) }}
{%- endfor -%}
{%- else -%}
{{ list(posts, collections, title=label, pagination=pagination) }}
{%- endif -%}
{%- endif -%}
{% endmacro %}
{# @docs
label: related_posts
category: lists
note: |
Returns a given number of most recent
posts with a tag
params:
tag:
type: string
note: The tag to show related posts for
collections:
type: 11ty collections object
default: '[]'
limit:
type: number
default: 3
banner_title:
type: string
default: ''
note: If set, will override the default title of "Posts about {tag}"
page_url:
type: url
default: "''"
#}
{% macro related_posts(
tag,
collections=[],
limit=3,
banner_title=none,
page_url=''
) %}
{%- set posts = collections[tag] | getPublic -%}
{%- if posts | length -%}
{%- set related = posts | pageYears | isPublicType | reverse | removePage(page_url) | onlyShow(limit) -%}
{%- set title = banner_title or ['Posts about', tag] | join(' ') -%}
{%- if related | length -%}
{{ list(
related,
collections=collections,
title=title,
override_feature=true,
override_large=true
) }}
{%- endif -%}
{%- endif -%}
{% endmacro %}
{# @docs
label: recent_posts
category: lists
note: |
Returns a given number of recent
posts from a collection
params:
type:
type: string
note: one of several post "types" to display (types are defined in the `taxonomy.yaml` data file)
collections:
type: 11ty collections object
default: '[]'
limit:
type: number
default: 3
banner_title:
type: string
default: 'Posts'
page_url:
type: url
default: "''"
#}
{% macro recent_posts(
type,
collections=[],
limit=3,
banner_title='Posts',
page_url=''
) %}
{%- set posts = collections[type] | getPublic -%}
{%- if posts | length -%}
{%- set recents = posts | isType(type) | pageYears | reverse | removePage(page_url) | onlyShow(limit) -%}
{%- if recents | length -%}
{%- set title = ['Recent', banner_title] | join(' ') -%}
{{ list(
recents,
collections=collections,
title=title,
override_feature=true,
override_large=true
) }}
{%- endif -%}
{%- endif -%}
{% endmacro %}
{# @docs
label: list
category: lists
note: Generate a list of posts
params:
posts:
type: array
note: containing 11ty page objects
collections:
type: 11ty collections object
default: none
title:
type: string
default: 'Posts'
pagination:
type: 11ty pagination object
default: none
override_feature:
type: boolean
default: 'false'
override_large:
type: boolean
default: 'false'
class:
type: string
note: optional class added to wrapping `<ol>` element
default: none
#}
{% macro list(
posts,
collections=none,
title='Posts',
pagination=none,
override_feature=false,
override_large=false,
class=none
) %}
{%- if posts -%}
{%- set posts = posts if (posts[0].sort) else posts | pageYears -%}
{%- set sort = 'sort' if (posts[0].sort) else 'date' -%}
{% if pagination %}
<header class="cards-header">
{{ layout.title(title) }}
{{ page_nav(pagination, current=false) }}
</header>
{% else %}
{{ layout.title(title) }}
{% endif %}
<ol data-grid="cards" {{ utility.attr_if('class', class) }} reversed>
{%- for post in posts | sort(reverse=true, attribute=sort) -%}
{{ item(
post,
collections,
loop,
override_feature=override_feature,
override_large=override_large
) }}
{%- endfor -%}
</ol>
{% endif %}
{% endmacro %}
{# @docs
label: item
category: posts
note: Generate each post in a list
params:
post:
type: 11ty page object
collections:
type: 11ty collections object
default: none
loop:
type: Nunjucks `loop` object -- https://mozilla.github.io/nunjucks/templating.html#for
default: none
override_feature:
type: boolean
default: 'false'
override_large:
type: boolean
default: 'false'
#}
{% macro item(
post,
collections=none,
loop=none,
override_feature=false,
override_large=false
) %}
{%- set type = post.data.tags | pageType -%}
{%- set card = post.data.card or none -%}
{%- set card = 'feature' if (override_large and card === 'large') else card -%}
{%- set card = none if (override_feature and card === 'feature') else card -%}
{# Adjust card settings based on the number of posts in a list #}
{% if loop %}
{%- set card = 'large' if (loop.length == 1) else card -%}
{%- set card = 'feature' if (card == 'large' and loop.length == 2) else card -%}
{% endif %}
<li data-card="{{ card }}">
{%- if card == 'large' -%}
{{ type_link(type, collections) }}
{{ detail(post, card, type, collections) }}
{%- else -%}
{{ detail(post, card, type, collections) }}
{{ type_link(type, collections) }}
{%- endif -%}
</li>
{% endmacro %}
{# @docs
label: detail
category: posts
note: Content for posts in a list
params:
post:
type: 11ty page object
type:
type: string
default: none
collections:
type: 11ty collections object
default: none
#}
{% macro detail(
post,
card='link',
type=none,
collections=none
) %}
{%- set image = post.data.image -%}
{%- set image = image if image and not (image.type == 'media') else none -%}
<article
data-post="{{ 'large' if (card == 'large') else 'link' }}"
aria-labelledby="{{ id(post) }}"
class="h-entry">
{{ hero(image, card or 'post', true) }}
{% if type %}
<div class="type-flag" data-flag="left">
{{ type.tag | displayName }}
<span class="sr-only">post type</span>
</div>
{% endif %}
<div class="post-content">
<header class="post-header">
{{ banner(post, hentry=true) }}
{{ subtitle(post.data.sub) }}
{{ byline(
post=post,
collections=collections,
links=false,
dateformat='range' if post.data.client else 'month_day'
) }}
</header>
{% if post.data.events %}
<div class="p-summary">
{{ talks.list(post | pageEvents, compact=true, show_links=false) }}
</div>
{% elif post.data.summary %}
{% set summary = post.data.summary | md %}
<div class="p-summary">
{% if (card == 'large') %}
{{ summary | safe }}
{% else %}
<p>
{{ summary | elide | safe }}
</p>
{% endif %}
</div>
{% endif %}
</div>
</article>
{% endmacro %}
{# @docs
label: hero
category: metadata
note: Display hero image for a post
params:
image:
type: object
type:
type: large | card | page
default: 'large'
#}
{% macro hero(
image,
type='large',
is_entry=false
) %}
{%- if image -%}
<div data-hero="{{ type or 'large' }}">
{%- if image.src -%}
{%- set position = {'--hero-position': image.position} | styles -%}
{{ embed.img(
src=image.src,
alt=image.alt,
sizes='card' if (type == 'post') else none,
attrs={
style: position,
class: 'u-photo' if is_entry else ''
}
) }}
{%- elif image.svg -%}
{{ embed.svg(image.svg, image.alt) }}
{%- endif -%}
</div>
{%- endif -%}
{% endmacro %}
{# @docs
label: byline
category: metadata
note: |
Generate the post byline with
type, authors, venue/client, date, location, and length if Winging It
params:
post:
type: 11ty page object
collections:
type: 11ty collections object
default: none
links:
type: boolean
default: true
note: Optionally remove links for content in linked cards
dateformat:
type: false | string
default: 'false'
note: Optionally show the date in a given format
#}
{% macro byline(
post,
collections=none,
links=true,
dateformat=false,
prefix='by'
) %}
<p class="byline">
{%- set is_episode = post.data.tags and post.data.tags | pageType('tag') in ['Podcast', 'Video', 'Winging It'] -%}
{%- set is_talk = post.data.tags and ('_talk' in post.data.tags) -%}
{% if is_episode -%}
{{ linked(
venue=post.data.venue,
url=(post.data.canonical or post.data.url) if links else none,
prefix='on'
)}}
{%- if post.data.episode %} episode {{ post.data.episode }}{% endif -%}
{{ by(post.data.author, collections, prefix='with', link=links) }}
{%- else -%}
{{ by(
post.data.author,
collections,
prefix=none if post.data.venue else prefix,
link=links
) }}
{% if post.data.oss %}
{% set plural = not post.data.author | typeCheck('string') %}
{% set plural = (post.data.author == 'oddbird') or plural %}
{% set plural = false if (post.data.oss == 'core team') else plural %}
({{ post.data.oss }}{{ 's' if plural else '' }})
{% endif %}
{{ linked(
venue=post.data.venue,
url=(post.data.canonical or post.data.url) if links else none
) }}
{{ client(post.data.client) }}
{%- endif %}
{% if dateformat and not is_talk %}
{{ pubdate(post.page.date, post.data.end, dateformat) }}
{% endif %}
{%- if is_episode and post.data.length %}
| {{ post.data.length }}
{%- endif %}
</p>
{% endmacro %}
{# @docs
label: type_link
category: metadata
note: |
Generate an icon-link
to the post-type index page
params:
type:
type: string
collections:
type: 11ty collections object
default: none
#}
{% macro type_link(
type,
collections=none
) %}
{%- set url = '/tags/' -%}
{%- if type.icon and not type.url -%}
{% set url = collections.all | tagLink(type.tag) if collections else none %}
{%- endif -%}
{%- set display_tag = type.display or type.tag | displayName + ' posts' -%}
{{ utility.link_if(
content=embed.svg(
path='icons/' + (type.icon or 'tags'),
alt=['see all', display_tag] | join(' ') if type else 'all tags'
),
url=type.url or url,
class='type-link'
) }}
{% endmacro %}
{# @docs
label: banner
category: metadata
note: Generate a linked heading with the post title
params:
post:
type: 11ty page object
#}
{% macro banner(
post,
level=3,
attrs={},
id_prefix='post',
called=false,
hentry=false
) -%}
{% set name = caller() if called != false else name(post) %}
{%- h level, {
'class': 'item-title p-name' if hentry else 'item-title',
'id': id(post, id_prefix)
} | merge(attrs) -%}
{{ utility.link_if(
name,
post.page.url,
'title-link u-url' if hentry else 'title-link'
) }}
{%- endh -%}
{%- endmacro %}
{% macro name(
post
) -%}
{% set title = post.data.banner or post.data.title %}
{{ title | mdInline | safe }}
{%- endmacro %}
{% macro id(
post,
id_prefix='post'
) -%}
{{ [id_prefix, post.page.url or post.url] | join('-') | slug }}
{%- endmacro %}
{% macro aria_label(
post,
id_prefix='post'
) %}aria-labelledby="{{ id(post, id_prefix) }}"{% endmacro %}
{# @docs
label: subtitle
category: metadata
note: Generate each post in a list
params:
sub:
type: markdown
class:
type: string
default: none
#}
{% macro subtitle(
sub,
class=none
) %}
{% if sub %}
<p class="subtitle{% if class %} {{ class }}{% endif %}">
{{ sub | mdInline | safe }}
</p>
{% endif %}
{% endmacro %}
{# @docs
label: by
category: metadata
note: Show a linked list of authors
params:
author:
type: string | array
collections:
type: 11ty collections object
#}
{% macro by(
author,
collections,
prefix='by',
link=true
) %}
{% if author %}
{% if prefix %}<i>{{ prefix }}</i>{% endif %}
{{ birds.authors(author, collections, link, class='p-author') }}
{% endif %}
{% endmacro %}
{# @docs
label: pubdate
category: metadata
note: Generate a pubdate in the proper format
params:
start:
type: date
end:
type: date
format:
type: string
default: 'long'
#}
{% macro pubdate(
start,
end,
format='long'
) -%}
{{ utility.datetime(
start=start,
end=end,
format=format,
is_pubdate=true,
prefix=true
) }}
{%- endmacro %}
{# @docs
label: client
category: metadata
note: Show the client name on a client project
params:
name:
type: string
#}
{% macro client(name) -%}
{%- if name %}
<i>for</i>
<b>{{ name | mdInline | safe }}</b>
{% endif -%}
{%- endmacro %}
{# @docs
label: where
category: metadata
note: Show the address when available
params:
adr:
type: string
#}
{% macro where(
adr
) %}
{% if adr %}
<i>in</i>
<span class="adr">
{{ adr }}
</span>
{% endif %}
{% endmacro %}
{# @docs
label: linked
category: metadata
note: Link to original post or venue
params:
venue:
type: string
url:
type: url
#}
{% macro linked(
venue,
url,
prefix='at'
) -%}
{%- if venue %}
<i>{{ prefix }}</i>
{{ utility.link_if(
content=venue | mdInline | safe,
url=url,
class='h-card',
attrs={'rel': 'bookmark'} if url else none
) }}
{% endif -%}
{%- endmacro %}
{# @docs
label: taglist
category: metadata
note: Show a linked list of all post tags, as pills
params:
tags:
type: array
collections:
type: 11ty collections object
#}
{% macro taglist(
tags,
collections,
pill=true
) -%}
{%- set public_tags = collections | tagData(tags) -%}
{%- set public_tags = public_tags if public_tags | length else none -%}
{%- if public_tags -%}
<div class="post-tags">
{%- include "icons/tags.svg" -%}
<ul inline-list="{{ 'pill-set' if pill else 'comma' }}">
{%- for group, items in public_tags | groupby('is_type') | dictsort | reverse -%}
{% for item in items | sort(attribute='tag') %}
<li {% if pill %}class="pill-item p-category"{% endif %}>
{{- utility.link_if(
content=item.tag,
url=item.url,
class='pill' if pill else none
) -}}
</li>
{% endfor %}
{%- endfor -%}
</ul>
</div>
{%- endif -%}
{%- endmacro %}
{# @docs
label: linklist
category: metadata
note: List of links to docs, source, sites, slides, etc
params:
links:
type: object
note: the key will be used as text, with the value for url
slides:
type: url
default: none
#}
{% macro linklist(
links,
slides=none
) %}
{% if links or slides %}
<ul class="post-links">
{% if slides %}
<li>{{ utility.link_if('slides »', slides) }}</li>
{% endif %}
{% for name, url in links %}
<li>{{ utility.link_if(name + ' »', url) }}</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
{# @docs
label: page_nav
category: pagination
note: Navigate paginated collections
params:
title:
type: string
note: Will be hidden, for screen-readers only
pagination:
type: 11ty pagination object
default: none
current:
type: boolean
default: 'false'
note: Show the current location in pagination?
id:
type: string
default: "''"
note: |
When repeating the pagination on a page,
each instance should get a unique ID
level:
type: integer [1-6]
default: 2
note: Heading level
#}
{% macro page_nav(pagination, current=true, labelledby='title') %}
{% if pagination %}
<nav class="pagination" aria-labelledby="{{ labelledby }}">
{{ utility.link_if(
content='« newer' if pagination.href.previous else 'newest',
url=pagination.href.previous,
class="pagination-link")
}}
{% if current %}
{{ page_current(pagination) }}
{% else %}
|
{% endif %}
{{ utility.link_if(
content='older »' if pagination.href.next else 'oldest',
url=pagination.href.next,
class="pagination-link")
}}
</nav>
{% endif %}
{% endmacro %}
{# @docs
label: page_current
category: pagination
note: |
Show the current active page number &
total page-count in a paginated list
params:
pagination:
type: 11ty pagination object
default: none
prefix:
type: string | none
default: none
note: Prefix "x of y" with more context, e.g. "page x of y"
#}
{% macro page_current(pagination, prefix=none) %}
{% if pagination %}
<span class="current">
{% if prefix %}<i>{{ prefix }}</i>{% endif %}
<strong>{{ pagination.pageNumber + 1 }}</strong>
<i>of</i>
<strong>{{ pagination.pages | length }}</strong>
</span>
{% endif %}
{% endmacro %}
{# @docs
label: homepage_posts
category: lists
note: |
Returns a given number of featured
posts from a collection for the
homepage "featured posts" section
params:
collections:
type: 11ty collections object
limit:
type: number
default: 4
banner_title:
type: string
default: 'Featured Posts'
class:
type: string
note: optional class added to wrapping `<ol>` element
default: none
#}
{% macro homepage_posts(
collections,
limit=4,
banner_title='Featured Posts',
class=none
) %}
{%- set featured = collections.posts | isHome(limit) -%}
{%- if featured | length -%}
{{ list(
featured,
collections=collections,
title=banner_title,
override_large=true,
class=class
) }}
{%- endif -%}
{% endmacro %}