-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
47 lines (39 loc) · 1.05 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
"use strict";
const alfy = require("alfy");
const mdnDocBase = "https://developer.mozilla.org";
https: alfy
.fetch(`${mdnDocBase}/api/v1/search?q=${alfy.input}`, { transform })
.then((results) => {
const items = (results || []).map((result) => {
const { title, highlight, mdn_url } = result;
const excerpt =
highlight && highlight.body && highlight.body.length > 0
? highlight.body[0]
: "";
const subtitle = stripHtml(excerpt);
const url = `${mdnDocBase}${mdn_url}`;
return {
title,
subtitle,
autoComplete: title,
arg: url,
quicklookurl: url,
};
});
// No results
if (items.length === 0) {
const url = `${mdnDocBase}/en-US/search?q=${alfy.input}`;
items.push({
title: `Show all results for '${alfy.input}'`,
arg: url,
quicklookurl: url,
});
}
alfy.output(items);
});
function transform(body) {
return body.documents;
}
function stripHtml(text) {
return text.replace(/<[^>]+>/g, "");
}