-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
210 lines (138 loc) · 4.51 KB
/
index.js
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
'use strict';
const PATH = require('path');
const _ = {
defaultsDeep: require('lodash.defaultsdeep'),
isEmpty : require('lodash.isempty'),
orderBy : require('lodash.orderby'),
};
// -----------------------------------------------------------------------------
/**
* holds relevant functions and data
*/
const PLUGIN = {
name: 'autonav', // the would also be the key used in frontmatter
};
// -----------------------------------------------------------------------------
/**
* @return {object}
*/
PLUGIN.get_options_defaults = () =>
{
const out = {
enable: true, // enables/disables everything - control per page using frontmatter
};
// ---------------------------------------------------------------------------
return out;
};
// PLUGIN.get_options_defaults()
/**
* @return {object}
*/
PLUGIN.get_page_options = ( page, plugin_options ) =>
{
const { frontmatter } = page;
// ---------------------------------------------------------------------------
// order of options override:
// - defaults -> gets set in this file by `PLUGIN.get_default_options()`
// - plugin options -> gets set in `config.js`
// - frontmatter -> gets set in each page
const options = _.defaultsDeep(
frontmatter[ PLUGIN.name ],
plugin_options,
PLUGIN.get_options_defaults()
);
// ---------------------------------------------------------------------------
return options;
};
// PLUGIN.get_page_options()
/**
* @return {array}
*/
PLUGIN.get_auto_nav_items = async ( pages, plugin_options ) =>
{
try {
const items = [];
for ( const page of pages )
{
const page_options = PLUGIN.get_page_options( page, plugin_options );
if ( ! page_options.enable )
{
continue;
}
// -----------------------------------------------------------------------
const { title, path } = page;
if ( ! path )
{
continue;
}
// -----------------------------------------------------------------------
const text = page_options.text || title;
const link = path;
const order = page_options.order|| 0;
// -----------------------------------------------------------------------
if ( text && link )
{
const parse_path = PATH.parse( path );
const parent = ( parse_path.root === parse_path.dir ) ? false : parse_path.dir;
// ---------------------------------------------------------------------
const item = { text, link, order, parent };
// ---------------------------------------------------------------------
items.push( item );
}
}
// -------------------------------------------------------------------------
if ( _.isEmpty( items ) )
{
return;
}
// -------------------------------------------------------------------------
// @notes: only top level pages are supported for now
const top_level_items = items
.filter( item => ! item.parent )
.map( item => {
const { text, link, order } = item;
return { text, link, order };
});
// -------------------------------------------------------------------------
const nav_items = _.orderBy( top_level_items, ['order'], ['asc'] );
// -------------------------------------------------------------------------
return nav_items;
} catch ( err ) {
console.error( err.message );
}
};
// PLUGIN.get_auto_nav_items()
// -----------------------------------------------------------------------------
module.exports = ( plugin_options, context ) => ({
async ready() {
const options = _.defaultsDeep(
plugin_options,
PLUGIN.get_options_defaults()
);
// -------------------------------------------------------------------------
if ( ! options.enable )
{
return;
}
// -------------------------------------------------------------------------
const { pages, themeConfig } = context.getSiteData ? context.getSiteData() : context;
if ( _.isEmpty( pages ) )
{
return;
}
// -------------------------------------------------------------------------
// bailout if nav already has already been set
if ( ! _.isEmpty( themeConfig.nav ) )
{
return;
}
// -------------------------------------------------------------------------
const nav_items = await PLUGIN.get_auto_nav_items( pages, plugin_options);
if ( _.isEmpty( nav_items ) )
{
return;
}
// -------------------------------------------------------------------------
themeConfig.nav = nav_items;
}
});