-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-list.js
executable file
·75 lines (70 loc) · 2.14 KB
/
file-list.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
'use strict'
const fs = require('graceful-fs')
const path = require('path')
const utils = require('./utils')
const directoryToItem = function (basePath, relativeDirectory) {
// Make the id so that it's the relative directory name, but sanitised
// without spaces or slashes that you can't use in a url.
const id = relativeDirectory.replace(/[ \\\\/]/g, '_').toLowerCase()
const item = {
label: path.basename(relativeDirectory),
id,
files: [],
directories: []
}
const dir = path.join(basePath, relativeDirectory)
const files = fs.readdirSync(dir)
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
item.directories.push(directoryToItem(basePath, path.join(relativeDirectory, file)))
} else {
const pth = path.join(dir, file)
if (utils.recipeFile(pth)) {
if (pth.substr(pth.length - 1) !== '~') {
item.files.push({
path: pth,
label: utils.pathToLabel(pth)
})
}
}
}
})
return item
}
const generateFileList = function () {
const fileList = []
const files = [
'Breakfast',
'Mains',
'Dessert',
'Other'
]
files.forEach(function (file) {
fileList.push(directoryToItem('public/recipes', file))
})
return fileList
}
const filterNewRecipes = function (index) {
const orderPredicate = (a, b) => {
if (a.date !== b.date) {
return a.date > b.date ? -1 : a.date < b.date ? 1 : 0
}
// If the dates are equal, sort by file
const labelA = utils.pathToLabel(a.file)
const labelB = utils.pathToLabel(b.file)
return labelA < labelB ? -1 : labelA > labelB ? 1 : 0
}
const orderedRecipes = []
const orderedIndex = index.slice()
orderedIndex.sort(orderPredicate)
for (let i = 0; i < orderedIndex.length; ++i) {
const item = orderedIndex[i]
const firstLines = item.content.split(/\r?\n/).slice(0, 5)
const resultItem = utils.searchItemToResult(item)
resultItem.context = firstLines
orderedRecipes.push(resultItem)
}
return orderedRecipes
}
module.exports.generateFileList = generateFileList
module.exports.filterNewRecipes = filterNewRecipes