-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 2.03 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
const Hulipaa = require('hulipaa')
const fs = require('fs')
const path = require('path')
const dataFolder = 'data'
const pagesFolder = 'pages'
// Clean pages directory
if (fs.existsSync(pagesFolder)) {
fs.rmSync(pagesFolder,{ recursive: true })
}
fs.mkdirSync(pagesFolder)
// Generate web pages
const dataFiles = fs.readdirSync(dataFolder)
const createdPages = []
for (let dataFileName of dataFiles) {
const dataFullPath = path.join(dataFolder,dataFileName)
const dataFileContent = fs.readFileSync(dataFullPath,'utf8')
const dataOfPage = JSON.parse(dataFileContent)
const { title,content } = dataOfPage
const html = `<html><head><title>${title}</title></head><body><h1>${title}</h1>${content}</body></html>`
const pageName = `${path.parse(dataFileName).name}.html`
const pageFullPath = path.join(pagesFolder,pageName)
fs.writeFileSync(pageFullPath,html)
createdPages.push([pageName,pageFullPath])
}
// Generate main web page
let htmlFiles = fs.readdirSync('.').filter(fileName => path.extname(fileName) === '.html')
htmlFiles = htmlFiles.filter(fileName => fileName !== 'index.html')
const htmlListHtmlFiles = htmlFiles.map(fileName => `<li><a href="${fileName}">${fileName}</a></li>`)
const htmlListGeneratedPages = createdPages.map(([page,path]) => `<li><a href="${path}">${page}</a></li>`).join('')
const htmlIndexFile = `<html><head><title>Main page</title></head><body>
<p>Html Pages</p><ul>${htmlListHtmlFiles}</ul>
<p>Generated Pages</p><ul>${htmlListGeneratedPages}</ul>
</body></html>`
fs.writeFileSync('index.html',htmlIndexFile)
Hulipaa({
inputFolder: 'data',
parseData: function (fileContent,filePath) {
const parsedFile = JSON.parse(fileContent)
return {
text: parsedFile.content,
title: parsedFile.title,
path: filePath
}
},
generateLink: function (fileName,inputFolder) {
const nameWithoutExtension = path.parse(fileName).name
return `/pages/${nameWithoutExtension}.html`
},
outputFolder: 'search'
})