-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost-utils.js
More file actions
69 lines (64 loc) · 1.79 KB
/
ghost-utils.js
File metadata and controls
69 lines (64 loc) · 1.79 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
66
67
68
69
const debug = require("debug")("hellomeinte:ghost-utils");
const filterOnGhostTitle = (pages, title) =>
pages.find(page => page.title === title);
const grabProject = (api, project) => {
debug(`grabbing project: tag:hash-project-${project}`);
return api.pages
.browse({
fields: "html,title",
limit: "1",
filter: `tag:hash-project-${project}`
})
.then(pages => {
if (!pages.length) return null;
return pages[0];
})
.catch(err => {
throw err; //main render function will deal with this
});
};
const grabPortfolio = (api, contentIds) => {
return api.pages
.browse({
fields: "html,slug,title,custom_excerpt,feature_image",
include: "tags",
limit: "all",
filter: "tag:hash-website",
order: "title ASC"
})
.then(pages => {
debug(pages);
/**
* Search content through their page titles.
* In this case, titles are stored in the contentIds array.
* Content is then mapped(reduced) in an object through its title
*/
return Object.keys(contentIds).reduce((acc, title) => {
const ghostContent = filterOnGhostTitle(pages, title) || {};
return {
...acc,
[contentIds[title]]: ghostContent
};
}, {});
})
.catch(err => {
throw err; //main render function will deal with this
});
};
//bundle similar content keys into one array
const bundleContent = (content, withTitle) =>
Object.keys(content).reduce((acc, itemKey) => {
const item = content[itemKey];
const arr = [...acc];
if (!item.title) return arr;
if (item.title.indexOf(withTitle) > -1) {
arr.push(item);
delete content[itemKey];
}
return arr;
}, []);
module.exports = {
grabProject,
grabPortfolio,
bundleContent
};