-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfilter.js
More file actions
65 lines (56 loc) · 1.67 KB
/
Copy pathfilter.js
File metadata and controls
65 lines (56 loc) · 1.67 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
module.exports = {
/**
* Convert page.date directly to 'YYYY/MM/DD' slug.
* (Liquid's native `date` filter converts date to locale time and loses a day.)
*
* @param {object} pageDate Original page.date value.
*
* @returns {string} Slugified date.
*/
dateSlug: (pageDate) => pageDate.toISOString().slice(0, 10).replace(/-/g, '/'),
/**
* Reverse array without mutating the original.
* (The 'reverse' filter in LiquidJS v6 mutates the original array.)
*
* @param {Array} collection Array to be reversed.
*
* @return {Array} Reversed array.
*/
flip: (collection) => {
if (!Array.isArray(collection)) return collection;
return [...collection].reverse();
},
/**
* Parse Markdown content to HTML.
*
* @param {string} content Incoming content (expecting Markdown).
*
* @return {string} Content rendered as HTML.
*/
markdown: (content) => {
const md = require('markdown-it')({
html: true,
linkify: true,
typographer: true,
});
if (content) {
return md.render(content);
}
return '';
},
/**
* Allow regex in replace filter. Global flag set by default,
*
* @param {string} content Content to search/replace.
* @param {string} rePattern RegExp patter to use.
* @param {string} replacement Replacement text.
* @return {string} Replaced content.
*
*/
regexReplace: (content, rePattern, replacement) => {
if (replacement === undefined || rePattern === undefined) return content;
const re = new RegExp(rePattern, 'g');
return content.replace(re, replacement);
},
UTCString: (date) => date.toUTCString(),
};