-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
nav.jsx
79 lines (70 loc) · 2.1 KB
/
nav.jsx
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
import React from 'react'
// @ts-expect-error: untyped.
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
import {apStyleTitleCase} from 'ap-style-title-case'
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
import {sortItems} from './sort.js'
const runtime = {Fragment, jsx, jsxs}
const dateTimeFormat = new Intl.DateTimeFormat('en', {dateStyle: 'long'})
export function NavGroup(props) {
const {items, className, sort = 'navSortSelf,meta.title', ...rest} = props
return (
<ol {...{className}}>
{sortItems(items, sort).map((d) => (
<NavItem key={d.name} {...rest} item={d} />
))}
</ol>
)
}
export function NavItem(props) {
const {item, name: activeName, includeDescription, includePublished} = props
const {name, children, data = {}} = item
const {matter = {}, meta = {}, navExcludeGroup, navSortItems} = data
const title = matter.title || meta.title
const defaultTitle = apStyleTitleCase(
name.replace(/\/$/, '').split('/').pop()
)
let description
let published
if (includeDescription) {
if (meta.descriptionHast) {
description = toJsxRuntime(
{
type: 'element',
tagName: 'div',
properties: {className: ['nav-description']},
children: meta.descriptionHast.children
},
runtime
)
} else {
description = matter.description || meta.description || null
if (description) {
description = (
<div className="nav-description">
<p>{description}</p>
</div>
)
}
}
}
if (includePublished && (matter.published || meta.published)) {
published = dateTimeFormat.format(matter.published || meta.published)
}
return (
<li>
{title ? (
<a href={name} aria-current={name === activeName ? 'page' : undefined}>
{title}
</a>
) : (
defaultTitle
)}
{published ? ' — ' + published : null}
{description || null}
{!navExcludeGroup && children.length > 0 ? (
<NavGroup items={children} sort={navSortItems} name={activeName} />
) : null}
</li>
)
}