-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathbuild-snippets.js
122 lines (122 loc) · 4.43 KB
/
build-snippets.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const folder = process.argv.pop()
const path = require('path')
const {readdirSync, readFileSync, writeFileSync, mkdirSync, unlinkSync, existsSync} = require('fs')
const basePath = path.join(__dirname, '') + '/' + folder
const outputPath = basePath + '/embed-snippets'
try {
if (!folder || !existsSync(outputPath))
mkdirSync(outputPath)
} catch(e) {
return
}
const docsBaseUrl = 'https://docs.thoughtspot.com/latest'
const embedTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
</head>
<body>
$html
<p><a href="$docsUrl" target="_blank">View full documentation.</a></p>
</body>
</html>
`
const getFilesAndDirectories = (directoryPath, includeFiles = false) => {
return readdirSync(directoryPath, {withFileTypes: true})
.filter(file => {
// Don't do the embed snippets dir
if (file.name == 'embed-snippets')
return false
if (!file.isDirectory() && path.extname(file.name).toLowerCase() !== '.html')
return false
// Include files if flag is true, otherwise only include if is directory.
return includeFiles ? true : file.isDirectory()
})
.map(file => {
if (file.isDirectory()) {
return {dirname: file.name, filesAndFolders: getFilesAndDirectories(`${directoryPath}/${file.name}`, true)}
} else {
return file.name
}
})
}
const generateFileSnippets = (path, file) => {
let sourceHtml = readFileSync(`${basePath}${path}/${file}`).toString()
const htmlRows = sourceHtml.split('\n')
const shortcodeRows = []
htmlRows.forEach((row, i) => {
const matches = row.trim().match(/<p>.*?\[embed.+?label\=\"(.*?)\"\].*?<\/p>|\[\/embed\]/)
if (matches) {
// Open tag
if (matches[1]) {
shortcodeRows.push({label: matches[1], startIndex: i})
// Close tag
} else {
shortcodeRows[shortcodeRows.length-1].endIndex = i
}
}
})
// Generate output HTML
shortcodeRows.forEach(shortcodeRow => {
// If we don't have a start and an end index, skip it.
if (!(shortcodeRow.startIndex && shortcodeRow.endIndex))
return
const outputHtmlRows = htmlRows.slice(shortcodeRow.startIndex+1, shortcodeRow.endIndex)
let outputHtml = embedTemplate
.replace('$title', shortcodeRow.label)
.replace('$html', outputHtmlRows.join('\n'))
.replace('$docsUrl', `${docsBaseUrl}${path}/${file}#${shortcodeRow.label}`)
const fileNameArray = file.split('.')
fileNameArray[fileNameArray.length-2] += `__${shortcodeRow.label}`
const snippetFileName = fileNameArray.join('.')
mkdirSync(`${outputPath}${path}`, {recursive: true})
writeFileSync(`${outputPath}${path}/${snippetFileName}`, outputHtml)
})
if (shortcodeRows.length) {
console.log('generateFileSnippets', path, file)
// Clean up source HTML
shortcodeRows.forEach(shortcodeRow => {
htmlRows[shortcodeRow.startIndex] = ''
htmlRows[shortcodeRow.endIndex] = ''
})
sourceHtml = htmlRows.filter(row => row).join('\n')
writeFileSync(`${basePath}${path}/${file}`, sourceHtml)
}
}
const generateSnippets = (filesAndFolders, currentPath) => {
filesAndFolders.forEach(fileOrFolder => {
if (fileOrFolder.dirname) {
const newCurrentPath = `${currentPath}/${fileOrFolder.dirname}`
generateSnippets(fileOrFolder.filesAndFolders, newCurrentPath)
} else {
try {
generateFileSnippets(currentPath, fileOrFolder)
} catch (e) {}
}
})
}
// const generateTestData = () => {
// let filePath = `${basePath}/admin/mobile/use-mobile.html`
// let sourceHtml = readFileSync(filePath).toString()
// sourceHtml = sourceHtml.replace('<h3 id="for-administrators">For administrators:</h3>', `
// <p>[embed label="for-administrators"]</p>
// <h3 id="for-administrators">For administrators:</h3>`)
// sourceHtml = sourceHtml.replace('<h3 id="for-users">For users:</h3>', `
// <p>[/embed]</p>
// <h3 id="for-users">For users:</h3>`)
// writeFileSync(filePath, sourceHtml)
// filePath = `${basePath}/admin/mobile/install-mobile.html`
// sourceHtml = readFileSync(filePath).toString()
// sourceHtml = sourceHtml.replace('<h2 id="install-the-app">Install the app</h2>', `
// <p>[embed label="for-administrators"]</p>
// <h2 id="install-the-app">Install the app</h2>`)
// sourceHtml = sourceHtml.replace('<h2 id="set-up-the-app">Set up the app</h2>', `
// <p>[/embed]</p>
// <h2 id="set-up-the-app">Set up the app</h2>`)
// writeFileSync(filePath, sourceHtml)
// }
// generateTestData()
const filesAndFoldersToCrawl = getFilesAndDirectories(basePath)
// console.log('filesAndFoldersToCrawl', filesAndFoldersToCrawl)
generateSnippets(filesAndFoldersToCrawl, '')