forked from act-rules/act-rules.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-markdown-data.js
More file actions
37 lines (33 loc) · 1.09 KB
/
Copy pathget-markdown-data.js
File metadata and controls
37 lines (33 loc) · 1.09 KB
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
const fs = require('fs')
const globby = require('globby')
const path = require('path')
const fastmatter = require('fastmatter')
const unified = require('unified')
const remarkParse = require('remark-parse')
const remarkFrontmatter = require('remark-frontmatter')
/**
* Parse all markdown files in a given directory and construct metadata of each markdown file
*
* @param {String} dir path to directory containing markdown files
* @param {Array} exclude (Optional) list of paths to exclude
* @returns {Object}
*/
const getMarkdownData = (dir, exclude = []) => {
return globby.sync([`${dir}/**/*.md`, ...exclude]).map(markdownPath => {
const filename = path.parse(markdownPath).base
const fileContents = fs.readFileSync(markdownPath, { encoding: 'utf-8' })
const unifiedProcesser = unified()
.use(remarkParse)
.use(remarkFrontmatter)
const markdownAST = unifiedProcesser.parse(fileContents)
const { attributes: frontmatter, body } = fastmatter(fileContents)
return {
path: markdownPath,
filename,
frontmatter,
body,
markdownAST,
}
})
}
module.exports = getMarkdownData