-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
529 lines (367 loc) · 11.5 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
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
'use strict';
const _ = {
defaultsDeep: require('lodash.defaultsdeep'),
isEmpty : require('lodash.isempty'),
};
// -----------------------------------------------------------------------------
const LIB = require('./lib');
const UTIL = require('./lib/UTIL');
// -----------------------------------------------------------------------------
const {
name : PLUGIN_NAME,
homepage: HOMEPAGE,
} = require('./package.json');
/**
* holds relevant functions and data
*/
const PLUGIN = {
name : PLUGIN_NAME,
homepage : HOMEPAGE,
key : PLUGIN_NAME.replace('vuepress-plugin-', ''), // used in frontmatter
allowed_feed_types: ['rss2', 'atom1', 'json1'],
pages : [],
options : {},
};
// -----------------------------------------------------------------------------
/**
* @return {object}
*/
PLUGIN.get_options_defaults = ( context ) =>
{
const {
title,
description
} = context.getSiteData ? context.getSiteData() : context;
// ---------------------------------------------------------------------------
// Feed class options
// @see: https://github.com/jpmonette/feed#example
const feed_options = {
title,
description,
generator: PLUGIN.homepage,
// -------------------------------------------------------------------------
// the following are auto populated in PLUGIN.get_options()
// if they are not set as options
/*
id,
link,
feedLinks,
*/
// -------------------------------------------------------------------------
// ref:
/*
title: "Feed Title",
description: "This is my personal feed!",
id: "http://example.com/",
link: "http://example.com/",
image: "http://example.com/image.png",
favicon: "http://example.com/favicon.ico",
copyright: "All rights reserved 2013, John Doe",
updated: new Date(2013, 6, 14), // optional, default = today
generator: "awesome", // optional, default = 'Feed for Node.js'
feedLinks: {
json: "https://example.com/json",
atom: "https://example.com/atom"
},
author: {
name: "John Doe",
email: "[email protected]",
link: "https://example.com/johndoe"
}
*/
};
// ---------------------------------------------------------------------------
const out = {
// required; it can also be used as enable/disable
canonical_base: '',
// -------------------------------------------------------------------------
// Feed class options
feed_options,
// -------------------------------------------------------------------------
// @notes:
// property name is also the name of the FEED package function
feeds: {
rss2: {
enable : true,
file_name : 'rss.xml',
head_link : {
enable: true,
type : 'application/rss+xml',
title : '%%site_title%% RSS Feed',
}
},
// -----------------------------------------------------------------------
atom1: {
enable : true,
file_name : 'feed.atom',
head_link : {
enable: true,
type : 'application/atom+xml',
title : '%%site_title%% Atom Feed',
}
},
// -----------------------------------------------------------------------
json1: {
enable : true,
file_name : 'feed.json',
head_link : {
enable: true,
type : 'application/json',
title : '%%site_title%% JSON Feed',
}
},
},
// -------------------------------------------------------------------------
// order of what gets the highest priority:
//
// 1. frontmatter
// 2. page excerpt
// 3. content markdown paragraph
// 4. content regular html <p>
description_sources: [
'frontmatter',
'excerpt',
// markdown paragraph regex
// @todo: needs work
//
/^((?:(?!^#)(?!^\-|\+)(?!^[0-9]+\.)(?!^!\[.*?\]\((.*?)\))(?!^\[\[.*?\]\])(?!^\{\{.*?\}\})[^\n]|\n(?! *\n))+)(?:\n *)+\n/gim,
//
// this excludes blockquotes using `(?!^>)`
///^((?:(?!^#)(?!^\-|\+)(?!^[0-9]+\.)(?!^!\[.*?\]\((.*?)\))(?!^>)(?!^\[\[.*?\]\])(?!^\{\{.*?\}\})[^\n]|\n(?! *\n))+)(?:\n *)+\n/gim,
// html paragraph regex
/<p(?:.*?)>(.*?)<\/p>/i,
// -----------------------------------------------------------------------
// @notes: setting as array require escaping `\`
//['^((?:(?!^#)(?!^\-|\+)(?!^[0-9]+\.)(?!^\[\[.*?\]\])(?!^\{\{.*?\}\})[^\n]|\n(?! *\n))+)(?:\n *)+\n', 'gim'],
//['<p(?:.*?)>(.*?)<\/p>', 'i'],
],
// -------------------------------------------------------------------------
// @consider description max words/char
// -------------------------------------------------------------------------
// order of what gets the highest priority:
//
// 1. frontmatter
// 2. content markdown image such as `![alt text](http://url)`
// 3. content regular html img
image_sources: [
'frontmatter',
/!\[.*?\]\((.*?)\)/i, // markdown image regex
/<img.*?src=['"](.*?)['"]/i, // html image regex
// -----------------------------------------------------------------------
// @notes: setting as array require escaping `\`
//['!\[.*?\]\((.*?)\)', 'i'],
//['<img.*?src=[\'"](.*?)[\'"]', 'i'],
],
// -------------------------------------------------------------------------
// pages in current directories will be auto added as feed
// unless they are disabled using their frontmatter
// this option is used by the default is_feed_page function
posts_directories: ['/blog/', '/_posts/'],
// -------------------------------------------------------------------------
// function to check if the page is to be used in a feed item
is_feed_page: PLUGIN.is_feed_page, // function
// -------------------------------------------------------------------------
count: 20,
// optional sorting function for the entries.
// Gets the array entries as the input, expects the sorted array
// as its output.
// e.g.: sort: entries => _.reverse( _.sortBy( entries, 'date' ) ),
sort: entries => entries, // defaults to just returning it as it is
// -------------------------------------------------------------------------
// supported - use in config as needed
// category
// contributor
};
// ---------------------------------------------------------------------------
return out;
};
// PLUGIN.get_options_defaults()
/**
* @return {object}
*/
PLUGIN.get_options = ( plugin_options, context ) =>
{
if ( _.isEmpty( PLUGIN.options ) )
{
PLUGIN.options = _.defaultsDeep(
plugin_options,
PLUGIN.get_options_defaults( context )
);
// -------------------------------------------------------------------------
// default link and id
if ( ! PLUGIN.options.feed_options.hasOwnProperty('link') )
{
PLUGIN.options.feed_options.link = plugin_options.canonical_base;
}
if ( ! PLUGIN.options.feed_options.hasOwnProperty('id') )
{
PLUGIN.options.feed_options.id = plugin_options.canonical_base;
}
// -------------------------------------------------------------------------
// default feedLinks
if ( ! PLUGIN.options.feed_options.hasOwnProperty('feedLinks')
&& ! _.isEmpty( PLUGIN.options.feeds ) )
{
PLUGIN.options.feed_options.feedLinks = {};
const feeds = PLUGIN.options.feeds || {};
for ( let key of Object.keys( feeds ) )
{
if ( ! PLUGIN.allowed_feed_types.includes( key ) )
{
continue;
}
// ---------------------------------------------------------------------
const url = PLUGIN.get_feed_url( feeds[ key ] );
if ( ! url )
{
continue;
}
// ---------------------------------------------------------------------
key = key.replace(/[0-9]/g, ''); // remove numbers from key;
PLUGIN.options.feed_options.feedLinks[ key ] = url;
}
}
// -------------------------------------------------------------------------
// internal - used in other files/classes
PLUGIN.options._internal = {
name : PLUGIN.name,
homepage : PLUGIN.homepage,
key : PLUGIN.key,
allowed_feed_types: PLUGIN.allowed_feed_types,
};
}
// ---------------------------------------------------------------------------
return PLUGIN.options;
};
// PLUGIN.get_options()
/**
* @return {bool}
*/
PLUGIN.good_to_go = ( plugin_options, context ) =>
{
const options = PLUGIN.get_options( plugin_options, context );
// ---------------------------------------------------------------------------
return ( options.canonical_base
&& ! _.isEmpty( options.feeds )
&& ! _.isEmpty( PLUGIN.pages ) );
};
// PLUGIN.good_to_go()
/**
* @return {string}
*/
PLUGIN.get_feed_url = feed =>
{
if ( feed.enable && feed.file_name )
{
return UTIL.resolve_url(PLUGIN.options.canonical_base, feed.file_name);
}
};
// PLUGIN.get_feed_url()
/**
* @return {bool}
*/
PLUGIN.get_page_feed_settings = frontmatter => frontmatter.feed || {};
/**
* @return {bool}
*/
PLUGIN.get_page_type = frontmatter => frontmatter.type || '';
/**
* @return {bool}
*/
PLUGIN.is_page_type_post = frontmatter => ( 'post' === PLUGIN.get_page_type( frontmatter ).toLowerCase() );
/**
* @return {bool}
*/
PLUGIN.is_feed_page = ( page ) =>
{
const { frontmatter, path } = page;
// ---------------------------------------------------------------------------
if ( ! _.isEmpty( frontmatter ) )
{
// use `frontmatter.feed.enable` to exclude a particular page/post
// bailout if it is set to false
const page_feed_settings = PLUGIN.get_page_feed_settings( frontmatter );
/*
if ( page_feed_settings.hasOwnProperty('enable')
&& ! page_feed_settings.enable )
{
return false;
}
*/
// @notes:
// as opposed to the above way of bailing out if set to false
// the following means that any page that has `frontmatter.feed.enable`
// set to true will be added
if ( page_feed_settings.hasOwnProperty('enable') )
{
return ( page_feed_settings.enable );
}
// -------------------------------------------------------------------------
if ( PLUGIN.is_page_type_post( frontmatter ) )
{
return true;
}
}
// ---------------------------------------------------------------------------
const directories = PLUGIN.options.posts_directories || [];
if ( ! _.isEmpty( directories ) )
{
for ( const dir of directories )
{
if ( path.startsWith(`${dir}`) )
{
return true;
}
}
}
// ---------------------------------------------------------------------------
return false;
};
// PLUGIN.is_feed_page()
// -----------------------------------------------------------------------------
module.exports = ( plugin_options, context ) => ({
/**
* used for collecting pages that would be used in feed;
* the reason i'm using this, is that `getSiteData` only gets `page.toJson()`,
* which only assigns preperties that don't start with '_'
* and what i need is the $page._strippedContent to get content for the feed
*/
extendPageData ( $page ) {
try {
if ( PLUGIN.get_options( plugin_options, context ).is_feed_page( $page ) )
{
PLUGIN.pages.push( $page );
}
} catch ( err ) {
LIB.LOG.error( err.message );
}
},
// ---------------------------------------------------------------------------
/**
* used for adding head links
*/
async ready() {
try {
if ( PLUGIN.good_to_go( plugin_options, context ) )
{
await new LIB.Head( PLUGIN.options, context ).add_links();
}
} catch ( err ) {
LIB.LOG.error( err.message );
}
},
// ---------------------------------------------------------------------------
/**
* used for generating the feed files
*/
async generated ( pagePaths ) {
try {
if ( PLUGIN.good_to_go( plugin_options, context ) )
{
await new LIB.Generator( PLUGIN.pages, PLUGIN.options, context ).generate();
}
} catch ( err ) {
LIB.LOG.error( err.message );
}
}
});