pidgin/nest
Clone
Summary
Browse
Changes
Graph
Fix the title of the index page.
2020-03-05, Elliott Sales de Andrade
03c86a48e9c0
Fix the title of the index page.
Currently, all breadcrumbs are subpages of "About", which is really
weird on the about subpages, since they become "About > About >
Subpage".
/**
* Runs after Hugo has built the site
* * Cleans, Minifies and Optimises hugo output
*
* USAGE: node tools/pre-prod [path to hugo output]
*/
/*****************************************************************************
* Imports
*****************************************************************************/
const
fs
=
require
(
'fs'
)
const
path
=
require
(
'path'
)
const
{
html
:
beautify_html
}
=
require
(
'js-beautify'
)
/*****************************************************************************
* Set Up
*****************************************************************************/
const
target
=
// looks for a path in first arg or default to `./public/`
path
.
join
(
__dirname
,
`..`
,
process
.
argv
[
2
]
||
'public'
)
+
path
.
sep
const
xmlRegex
=
/\.(html|xml)$/
const
jsonRegex
=
/\.json$/
const
svgRegex
=
/\.svg$/
const
htmlBeautifyOptions
=
{
max_preserve_newlines
:
0
}
/*****************************************************************************
* Execution
*****************************************************************************/
getFilePaths
(
target
).
forEach
(
path
=>
{
if
(
path
.
match
(
xmlRegex
))
{
tidyXML
(
path
)
}
else
if
(
path
.
match
(
jsonRegex
))
{
minifyJSON
(
path
)
}
})
/*****************************************************************************
* File Handlers
*****************************************************************************/
/**
* Format XML and HTML, removing hugo template artifacts,
* like whitespace chasms
* @param {string} path
*/
function
tidyXML
(
path
)
{
const
data
=
fs
.
readFileSync
(
path
,
'utf8'
)
const
formatted
=
beautify_html
(
data
,
htmlBeautifyOptions
)
fs
.
writeFile
(
path
,
formatted
,
err
=>
{
if
(
err
)
{
console
.
log
(
`Error Tidying
${
path
}
`
,
err
)
}
console
.
log
(
`Tidied path:
${
path
.
replace
(
target
,
''
)
}
`
)
})
}
/**
* Minify JSON
* @param {string} path
*/
function
minifyJSON
(
path
)
{
const
data
=
fs
.
readFileSync
(
path
,
'utf8'
)
const
formatted
=
JSON
.
stringify
(
JSON
.
parse
(
data
))
fs
.
writeFile
(
path
,
formatted
,
err
=>
{
if
(
err
)
{
console
.
log
(
`Error minifying
${
path
}
`
,
err
)
}
console
.
log
(
`Minified path:
${
path
.
replace
(
target
,
''
)
}
`
)
})
}
/*****************************************************************************
* Helpers
*****************************************************************************/
function
getFilePaths
(
input
)
{
const
items
=
readDir
(
input
)
const
ret
=
[]
while
(
items
.
length
)
{
const
item
=
items
.
pop
()
const
stat
=
fs
.
statSync
(
item
)
if
(
stat
.
isDirectory
())
{
items
.
push
(...
readDir
(
item
))
}
else
if
(
stat
.
isFile
())
{
ret
.
push
(
item
)
}
}
return
ret
}
function
readDir
(
item
)
{
return
fs
.
readdirSync
(
item
).
map
(
i
=>
path
.
join
(
item
,
i
))
}