-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·81 lines (72 loc) · 1.88 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
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
'use strict'
const React = require('react')
const Settings = require('./Settings')
const Preview = require('./Preview')
const db = require('./db')
const { search } = require('cerebro-tools')
const icon = require('./icons/plugin.png')
const icons = require('./icons')
const keyword = 'devdocs'
const getIcon = (group) => (
icons[group] ? icons[group] : icon
)
const notMatch = term => item => (
item.group !== term && `${item.group} ` !== term
)
const autocomplete = ({term, display, actions}) => {
const autocompleteResults = db.searchDocumentation(term)
.filter(notMatch(term))
.map(item => ({
icon: getIcon(item.group),
title: `Search ${item.name} docs`,
term: `${item.group} `,
onSelect: (event) => {
event.preventDefault()
actions.replaceTerm(`${item.group} `)
}
}))
display(autocompleteResults)
}
const searchDoc = ({term, display, actions}) => {
const match = term.match(/(\w+)\s*(.*)$/)
if (match) {
const [_, group, term] = match
db.search({ group, term })
.then(results => results.map(x => (
{
title: x.name,
subtitle: x.type,
icon: getIcon(group),
onSelect: () => actions.open(`http://devdocs.io/${x.path}`),
getPreview: () => <Preview key={x.path} path={x.path} />
}
)))
.then(display)
}
}
const settings = ({ term, display, update }) => {
const match = term.match(/^devdocs\s*(.+)?$/)
if (match) {
display({
icon,
title: 'DevDocs Settings',
subtitle: 'Select docs that you want to use',
getPreview: () => (
<Settings
term={match[1]}
onInstall={db.install}
onUninstall={db.uninstall}
/>
)
})
}
}
const fn = (props) => {
autocomplete(props)
searchDoc(props)
settings(props)
}
module.exports = {
fn, icon, keyword,
name: 'DevDocs Settings',
}