This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghost-hunter.js
173 lines (148 loc) · 4.55 KB
/
ghost-hunter.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
/*
* Modified version of ghostHunter 0.0.4
*/
const assert = require('assert');
// eslint-disable-next-line node/no-deprecated-api
const {resolve} = require('url');
const lunr = require('lunr');
const got = require('got');
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
function normalizeURL(url) {
// Disallow trailing slash
url = url.replace(/\/$/, '/');
if (url.indexOf('/ghost') > 0) {
url = url.replace(/\/ghost.*$/, '/ghost/api/v0.1');
} else {
url = `${url}/ghost/api/v0.1`;
}
return url;
}
function prettyDate(date) {
const d = new Date(date);
return `${d.getDate()} ${months[d.getMonth()]} ${d.getFullYear()}`;
}
class GhostHunter {
constructor(_options = {}) {
const options = Object.assign({
instance: {},
includePages: false,
absolute: true
}, _options);
this.initialized = false;
this.blogData = {};
this.includePages = options.includePages;
this.instance = options.instance;
this.absolute = options.absolute;
[['client_id', 'clientID'], ['client_secret', 'clientSecret']].forEach(([deprecatedInstanceParameter, normalParameter]) => {
if (this.instance[deprecatedInstanceParameter]) {
console.warn(`[deprecation] - ${deprecatedInstanceParameter} is no longer supported. Please use ${normalParameter} instead. See https://git.io/f4dV9`);
this.instance[normalParameter] = this.instance[deprecatedInstanceParameter];
this.instance[deprecatedInstanceParameter] = undefined;
}
});
['clientID', 'clientSecret', 'url'].forEach(requiredInstanceParameter => {
assert.ok(this.instance[requiredInstanceParameter]);
});
this.instance.url = normalizeURL(this.instance.url);
}
createIndex(refresh = false) {
if (this.initialized && !refresh) {
return false;
}
// Load all of the blog posts to the index.
const parameters = {
limit: 'all',
include: 'tags',
formats: ['plaintext']
};
if (this.includePages) {
parameters.filter = '(page:true,page:false)';
}
return got(this.url('posts', parameters)).then(response => {
const posts = JSON.parse(response.body).posts.map(post => {
let tags = post.tags.map(tag => tag.name).join(', ');
if (tags.length === 0) {
tags = 'undefined';
}
/* eslint-disable camelcase */
if (post.meta_description === null) {
post.meta_description = '';
}
/* eslint-enable camelcase */
const parsedData = {
id: String(post.id),
title: String(post.title),
description: String(post.meta_description),
plaintext: String(post.plaintext),
pubDate: String(post.created_at),
tag: tags,
featureImage: String(post.feature_image),
// We don't want this to be an absolute url because it's of no use to the index
link: String(post.url)
};
parsedData.prettyPubDate = prettyDate(parsedData.pubDate);
this.blogData[post.id] = {
title: post.title,
description: post.meta_description,
pubDate: parsedData.prettyPubDate,
featureImage: post.feature_image,
link: this.absolute ? resolve(this.instance.url.replace('/ghost/api/v0.1', ''),
post.url.replace(/^\//, '')) : post.url
};
return parsedData;
});
// This is where we'll build the index for later searching. It's not a big deal to build it on every load as it takes almost no space without data
this.index = lunr(function initializeLunr() {
this.field('title', {boost: 10});
this.field('description');
this.field('link');
this.field('plaintext', {boost: 5});
this.field('pubDate');
this.field('tag');
this.ref('id');
posts.forEach(post => this.add(post));
});
this.initialized = true;
}).catch(error => {
throw error;
});
}
url(location, parameters) {
const qs = require('querystring').encode;
Object.assign(parameters, {
/* eslint-disable camelcase */
client_id: this.instance.clientID,
client_secret: this.instance.clientSecret
/* eslint-enable camelcase */
});
return `${this.instance.url}/${location}/?${qs(parameters)}`;
}
find(value) {
console.log(`[index] searching for "${value}"`);
if (!this.index) {
return {
data: 'An error occurred',
errors: [{
details: 'Unable to search at this time. Please try again later',
code: 'NO_INDEX'
}]
};
}
if (value === '') {
return {
meta: {
count: 0
},
data: []
};
}
const results = this.index.search(value);
return {
meta: {
count: results.length
},
data: results.map(result => this.blogData[result.ref])
};
}
}
module.exports = GhostHunter;