forked from hashicorp/next-mdx-enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
39 lines (33 loc) · 1017 Bytes
/
util.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
const path = require('path')
const crypto = require('crypto')
async function extendFrontMatter({
content,
frontMatter,
phase,
extendFm,
} = {}) {
if (!extendFm || !extendFm.process) return {}
if (extendFm.phase !== 'both' && extendFm.phase !== phase) return {}
return extendFm.process(content, frontMatter)
}
module.exports.extendFrontMatter = extendFrontMatter
function generateFrontmatterPath(filePath, root) {
const filePathNormalized = normalizeToUnixPath(filePath)
const dirnameNormalized = normalizeToUnixPath(__dirname)
return normalizeToUnixPath(
path.join(
root,
'.next/cache/mdx-data',
`${md5(filePathNormalized.replace(dirnameNormalized, ''))}.json`
)
)
}
module.exports.generateFrontmatterPath = generateFrontmatterPath
// md5 hash a string
function md5(str) {
return crypto.createHash('md5').update(str).digest('hex')
}
function normalizeToUnixPath(str) {
return str.replace(/\\/g, '/')
}
module.exports.normalizeToUnixPath = normalizeToUnixPath