-
Notifications
You must be signed in to change notification settings - Fork 25
/
md-to-redirects.js
89 lines (78 loc) · 2.04 KB
/
md-to-redirects.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
// @ts-check
const path = require('path')
const fs = require('fs')
const globby = require('globby')
const pluralize = require('pluralize')
const { stripIndent } = require('common-tags')
// converts Markdown files into HTML file that redirects to version-specific page
// for example the top level "index.html" should redirect to "<version>/index.html"
const baseUrl = process.argv[2]
if (!baseUrl) {
console.error('Missing the top-level domain argument')
process.exit(1)
}
console.log(
'Each spec file will visit %s before tests in each spec',
baseUrl,
)
const docsFolder = 'docs'
const markdownFiles = globby.sync(
[
// create HTML redirect pages for all Markdown documents
// so that we can send URL like cypress-examples/commands/actions
// and it would redirect the user to the latest
// cypress-examples/x.y.z/commands/actions.html
'**/*.md',
],
{
cwd: docsFolder,
},
)
console.log(
'%d Markdown %s',
markdownFiles.length,
pluralize('file', markdownFiles.length, false),
)
console.log(markdownFiles.join('\n'))
const makeFolder = (filename) => {
const dir = path.dirname(filename)
try {
fs.mkdirSync(dir, { recursive: true })
} catch {}
}
const getHtml = (baseUrl, mdFile) => {
let dir
if (mdFile.endsWith('index.md')) {
dir = path.dirname(mdFile)
if (dir === '.') {
dir = ''
}
} else {
dir =
path.dirname(mdFile) +
'/' +
path.basename(mdFile, '.md') +
'.html'
}
const redirectUrl = baseUrl + '/' + dir
const html = stripIndent`
<html>
<head>
<meta
http-equiv="Refresh"
content="0; url='${redirectUrl}'"
/>
</head>
</html>
`
return html
}
const destinationFolder = 'redirects'
markdownFiles.forEach((mdFile) => {
const filename = mdFile.replace('.md', '.html')
const destinationFile = path.join(destinationFolder, filename)
console.log('output: %s', destinationFile)
makeFolder(destinationFile)
const html = getHtml(baseUrl, mdFile)
fs.writeFileSync(destinationFile, html + '\n', 'utf8')
})