forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread-frontmatter.js
47 lines (42 loc) · 1.55 KB
/
read-frontmatter.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
const fs = require('fs')
const fm = require('./frontmatter')
const endLine = '\n---\n'
/**
* Reads the given filepath, but only up until `endLine`, using streams to
* read each chunk and close the stream early.
*/
async function readFrontmatter (filepath) {
const readStream = fs.createReadStream(filepath, { encoding: 'utf8', emitClose: true })
return new Promise((resolve, reject) => {
let frontmatter = ''
readStream
.on('data', function (chunk) {
const endOfFrontmatterIndex = chunk.indexOf(endLine)
if (endOfFrontmatterIndex !== -1) {
frontmatter += chunk.slice(0, endOfFrontmatterIndex + endLine.length)
// Stop early!
readStream.destroy()
} else {
frontmatter += chunk
}
})
.on('error', (error) => reject(error))
// Stream has been destroyed and file has been closed
.on('close', () => resolve(frontmatter))
})
}
/**
* Read only the frontmatter from a file
*/
module.exports = async function fmfromf (filepath, languageCode) {
let fileContent = filepath.endsWith('index.md')
// For index files, we need to read the whole file because they contain ToC info
? await fs.promises.readFile(filepath, 'utf8')
// For everything else, only read the frontmatter
: await readFrontmatter(filepath)
// TODO remove this when crowdin-support issue 66 has been resolved
if (languageCode !== 'en' && fileContent.includes(': verdadero')) {
fileContent = fileContent.replace(': verdadero', ': true')
}
return fm(fileContent, { filepath })
}