forked from devrim/stack-docs-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.coffee
99 lines (75 loc) · 2.53 KB
/
build.coffee
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
recursive = require('recursive-readdir')
fs = require('fs')
marked = require('marked')
hcl2json = require('hcl-to-json')
child_process = require('child_process')
json2yml = require('json2yaml')
fuzzy = require('fuzzy')
path = require('path')
getTokenFromMarkdown = require('./utils/getTokenFromMarkdown')
replaceHclToYaml = (title, str) ->
str.replace(/---(.*?(\n))+.*---/g, '').replace(/\.\.\./g, '# ...').replace /```(hcl|)(\n(?:\n|.)*?)```/g, (match) ->
match = match.replace(/\`\`\`(hcl|)/g, '')
if /^\n\$/.test(match)
return '```bash\n' + match + '\n```'
try
return '```yaml' + json2yml.stringify(hcl2json(match)).replace(/^---/, '') + '\n```'
catch err
console.log 'Failed on title:', title
return match
return
recreateDocs = (callback) ->
child_process.execSync 'git clone https://github.com/hashicorp/terraform.git --depth 1; rm -rf ./terraform/.git'
recursive 'terraform/website/source/docs/providers', (err, files) ->
content = []
files.forEach (path, k) ->
data = fs.readFileSync(path, 'utf-8')
if path.indexOf('.md') > 0 or path.indexOf('.markdown') > 0
title = data.substring(data.indexOf('page_title:') + 13, data.indexOf('sidebar_current:') - 2)
description = getTokenFromMarkdown(data, [
'heading'
'code'
]).trim()
return content.push(
value: title
path: path
data: replaceHclToYaml(title, data)
description: description
marked: marked(data))
child_process.execSync 'rm -rf ./terraform', cwd: __dirname
fs.writeFileSync './website/docs.json', JSON.stringify(content, null, '\u0009')
fs.writeFileSync './docs.json', JSON.stringify(content, null, '\u0009')
callback null
docs = JSON.parse(fs.readFileSync(path.join(__dirname, './docs.json')))
searchDocs = (query) ->
results = []
docs.forEach (content) ->
if content.data.indexOf(query) > 0
return results.push([ content.path ])
return
results
getContent = (title) ->
content = null
length = docs.length
i = 0
while i < length
doc = docs[i]
if docs[i].value == title
content = docs[i].data
break
i++
return content
search = (query) ->
options =
pre: ''
post: ''
extract: (el) ->
el.value
results = fuzzy.filter(query, docs, options)
return results.map (el) ->
{ title: el.string, description: el.original.description }
module.exports =
recreateDocs: recreateDocs
searchDocs: searchDocs
search: search
getContent: getContent