add tutorials to the search index

This commit is contained in:
Zeke Sikelianos 2018-05-14 10:54:05 -07:00
Родитель 05f6328bc4
Коммит 7e6b4ee518
8 изменённых файлов: 1661 добавлений и 579 удалений

35
demo.css Normal file
Просмотреть файл

@ -0,0 +1,35 @@
* {
box-sizing: border-box;
}
body {
padding: 50px;
font-family: 'helvetica neue', helvetica, arial;
}
#search-box input {
padding: 10px;
width: 100%;
}
.ais-hits--item {
padding: 10px;
}
.ais-hits--item em {
background-color: yellow;
}
.ais-search-box--magnifier {
display: none;
}
.ais-search-box--reset {
top: 12px;
width: 30px;
height: 30px;
border: none;
position: absolute;
right: 10px;
opacity: 0.2;
}

38
demo.js
Просмотреть файл

@ -4,44 +4,6 @@ document.title = 'Electron Search'
const $main = html` const $main = html`
<main> <main>
<style>
* {
box-sizing: border-box;
}
body {
padding: 50px;
font-family: 'helvetica neue', helvetica, arial;
}
#search-box input {
padding: 10px;
width: 100%;
}
.ais-hits--item {
padding: 10px;
}
.ais-hits--item em {
background-color: yellow;
}
.ais-search-box--magnifier {
display: none;
}
.ais-search-box--reset {
top: 12px;
width: 30px;
height: 30px;
border: none;
position: absolute;
right: 10px;
opacity: 0.2;
}
</style>
<div id="search-box"></div> <div id="search-box"></div>
<div id="refinement-list"></div> <div id="refinement-list"></div>
<div id="hits"></div> <div id="hits"></div>

1973
index.json

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

67
lib/electron-apis.js Normal file
Просмотреть файл

@ -0,0 +1,67 @@
const apis = require('../electron-api.json')
const results = []
apis.forEach(api => {
// TODO constructorMethod
const staticMethods = api.staticMethods || []
staticMethods.forEach(method => {
method.type = 'api'
method.apiType = 'staticMethod'
method.title = `${api.name}.${method.name}${method.signature}`
method.tldr = getTLDR(method)
const slug = method.name.replace(/\W/g, '').toLowerCase()
method.url = `https://electronjs.org/docs/api/${api.slug}#${api.slug}${slug}`
delete method.signature
results.push(method)
})
const instanceMethods = api.instanceMethods || []
instanceMethods.forEach(method => {
method.type = 'api'
method.apiType = 'instanceMethod'
method.title = `${api.instanceName}.${method.name}${method.signature}`
method.tldr = getTLDR(method)
const slug = method.name.replace(/\W/g, '').toLowerCase()
method.url = `https://electronjs.org/docs/api/${api.slug}#${api.slug}${slug}`
delete method.signature
results.push(method)
})
const events = api.events || []
events.forEach(event => {
event.type = 'api'
event.apiType = 'event'
event.title = `${api.name}.on('${event.name}')`
event.url = `https://electronjs.org/docs/api/${api.slug}#event-${event.name}`
event.tldr = getTLDR(event)
results.push(event)
})
const instanceEvents = api.instanceEvents || []
instanceEvents.forEach(event => {
event.type = 'api'
event.apiType = 'event'
event.title = `${api.instanceName}.on('${event.name}')`
event.url = `https://electronjs.org/docs/api/${api.slug}#event-${event.name}`
event.tldr = getTLDR(event)
results.push(event)
})
})
function getTLDR (api) {
const { description, returns } = api
if (!description && returns && returns.description) {
return (
'Returns ' +
returns.description.charAt(0).toLowerCase() +
returns.description.slice(1)
)
}
if (typeof description !== 'string' || !description.length) return null
return description.split('. ')[0] + '.'
}
module.exports = results

16
lib/tutorials.js Normal file
Просмотреть файл

@ -0,0 +1,16 @@
const {chain} = require('lodash')
const cheerio = require('cheerio')
module.exports = chain(Object.values(require('electron-i18n').docs['en-US']))
.filter(tutorial => !tutorial.isApiDoc && !tutorial.isApiStructureDoc)
.map(tutorial => {
const {title, githubUrl, slug, sections} = tutorial
const type = 'tutorial'
const html = sections.map(section => section.html).join('\n\n')
const body = cheerio.load(html).text()
if (!title && body.startsWith('Moved to')) return
const url = `https://electronjs.org/docs/tutorial/${slug}`
return {type, title, githubUrl, url, slug, body}
})
.compact() // remove nulls
.value()

Просмотреть файл

@ -11,18 +11,21 @@
"pretest": "npm run build", "pretest": "npm run build",
"test": "tape test.js | tap-spec && standard --fix", "test": "tape test.js | tap-spec && standard --fix",
"lint": "standard --fix", "lint": "standard --fix",
"demo": "budo demo.js --live --no-debug --open", "demo": "budo demo.js --live --no-debug --open --css demo.css",
"repl": "local-repl" "repl": "local-repl"
}, },
"devDependencies": { "devDependencies": {
"budo": "^11.2.0", "budo": "^11.2.0",
"cheerio": "^1.0.0-rc.2",
"electron-i18n": "^1.86.0",
"instantsearch.js": "^2.7.4", "instantsearch.js": "^2.7.4",
"is-url": "^1.2.4", "is-url": "^1.2.4",
"local-repl": "^4.0.0", "local-repl": "^4.0.0",
"lodash": "^4.17.10",
"nanohtml": "^1.2.4", "nanohtml": "^1.2.4",
"prettier-standard": "^8.0.1", "prettier-standard": "^8.0.1",
"standard": "^11.0.1", "standard": "^11.0.1",
"tap-spec": "^4.1.1", "tap-spec": "^4.1.1",
"tape": "^4.9.0" "tape": "^4.9.0"
} }
} }

Просмотреть файл

@ -1,65 +1,8 @@
#!/usr/bin/env node #!/usr/bin/env node
const apis = require('../electron-api.json') const {chain} = require('lodash')
const results = [] const apis = require('../lib/electron-apis')
const tutorials = require('../lib/tutorials.js')
apis.forEach(api => { const results = chain([apis, tutorials]).flatten().value()
// TODO constructorMethod
const staticMethods = api.staticMethods || []
staticMethods.forEach(method => {
method.title = `${api.name}.${method.name}${method.signature}`
method.type = 'staticMethod'
method.tldr = getTLDR(method)
const slug = method.name.replace(/\W/g, '').toLowerCase()
method.url = `https://electronjs.org/docs/api/${api.slug}#${api.slug}${slug}`
delete method.signature
results.push(method)
})
const instanceMethods = api.instanceMethods || []
instanceMethods.forEach(method => {
method.title = `${api.instanceName}.${method.name}${method.signature}`
method.type = 'instanceMethod'
method.tldr = getTLDR(method)
const slug = method.name.replace(/\W/g, '').toLowerCase()
method.url = `https://electronjs.org/docs/api/${api.slug}#${api.slug}${slug}`
delete method.signature
results.push(method)
})
const events = api.events || []
events.forEach(event => {
event.title = `${api.name}.on('${event.name}')`
event.type = 'event'
event.url = `https://electronjs.org/docs/api/${api.slug}#event-${event.name}`
event.tldr = getTLDR(event)
results.push(event)
})
const instanceEvents = api.instanceEvents || []
instanceEvents.forEach(event => {
event.title = `${api.instanceName}.on('${event.name}')`
event.type = 'event'
event.url = `https://electronjs.org/docs/api/${api.slug}#event-${event.name}`
event.tldr = getTLDR(event)
results.push(event)
})
})
process.stdout.write(JSON.stringify(results, null, 2)) process.stdout.write(JSON.stringify(results, null, 2))
function getTLDR (api) {
const { description, returns } = api
if (!description && returns && returns.description) {
return (
'Returns ' +
returns.description.charAt(0).toLowerCase() +
returns.description.slice(1)
)
}
if (typeof description !== 'string' || !description.length) return null
return description.split('. ')[0] + '.'
}

39
test.js
Просмотреть файл

@ -1,12 +1,22 @@
const apis = require('.') const entries = require('.')
const test = require('tape') const test = require('tape')
// const isURL = require('is-url') const isURL = require('is-url')
const types = ['api', 'tutorial']
test('electron-search', t => { test('electron-search', t => {
t.ok(Array.isArray(apis), 'is an array') // All Entries
t.ok(apis.length > 5, 'with hella entries') // ----------------------------------------------------------------------
t.ok(Array.isArray(entries), 'is an array')
t.ok(entries.length > 5, 'with hella entries')
entries.forEach(entry => {
t.ok(types.includes(entry.type), `${entry.title} has a known type`)
})
// console.log(apis.map(api => api.title)) // APIs
// ----------------------------------------------------------------------
const apis = entries.filter(entry => entry.type === 'api')
t.ok(apis.length > 5, 'lots of APIs')
let staticMethod = apis.find(api => api.title === 'Menu.getApplicationMenu()') let staticMethod = apis.find(api => api.title === 'Menu.getApplicationMenu()')
t.equal(staticMethod.url, 'https://electronjs.org/docs/api/menu#menugetapplicationmenu', 'sets proper URL on static methods') t.equal(staticMethod.url, 'https://electronjs.org/docs/api/menu#menugetapplicationmenu', 'sets proper URL on static methods')
@ -20,7 +30,22 @@ test('electron-search', t => {
// t.ok(isURL(api.url), `${api.title} has a valid URL`) // t.ok(isURL(api.url), `${api.title} has a valid URL`)
}) })
// Tutorials
// ----------------------------------------------------------------------
const tutorials = entries.filter(entry => entry.type === 'tutorial')
tutorials.forEach(tutorial => {
if (!tutorial.title) console.log(tutorial)
t.equal(typeof tutorial.title, 'string', `${tutorial.title} has a title`)
t.equal(typeof tutorial.body, 'string', `${tutorial.title} has a body`)
t.ok(isURL(tutorial.githubUrl), `${tutorial.title} has a valid GitHub URL`)
})
// Packages
// ----------------------------------------------------------------------
// Repos
// ----------------------------------------------------------------------
t.end() t.end()
}) })
// https://electronjs.org/docs/api/browser-window#event-page-title-updated