1
1
mirror of https://github.com/primer/css.git synced 2024-11-26 12:14:22 +03:00
css/__tests__/utils/docs.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-04-01 20:56:12 +03:00
const fs = require('fs')
2021-03-31 02:53:47 +03:00
const frontMatter = require('front-matter')
const yaml = require('js-yaml')
const globby = require('globby')
const {join} = require('path')
const docsPath = join(__dirname, '../../docs')
function collectNavLinks(links) {
let foundLinks = []
2021-04-01 20:56:12 +03:00
for (const link of links) {
2021-03-31 02:53:47 +03:00
foundLinks.push({
title: link['title'],
url: link['url']
})
2021-04-01 20:56:12 +03:00
if (link['children']) {
2021-03-31 02:53:47 +03:00
foundLinks = foundLinks.concat(collectNavLinks(link['children']))
}
2021-04-01 20:56:12 +03:00
}
2021-03-31 02:53:47 +03:00
return foundLinks
}
function getNavigationLinks() {
const nav = yaml.load(fs.readFileSync(join(docsPath, './src/@primer/gatsby-theme-doctocat/nav.yml'), 'utf8'))
return collectNavLinks(nav)
}
async function getContentFrontmatter() {
2021-04-01 20:56:12 +03:00
const fm = {}
2021-03-31 02:53:47 +03:00
const paths = await globby(join(docsPath, './content/**/*.md*'))
2021-04-01 20:56:12 +03:00
for (const path of paths) {
2021-03-31 02:53:47 +03:00
const contents = fs.readFileSync(path, 'utf8')
const fmContents = frontMatter(contents)
2021-04-01 20:56:12 +03:00
fm[path.replace(join(docsPath, './content'), '').replace(/(\/index)?\.mdx?/, '')] = fmContents['attributes']
}
2021-03-31 02:53:47 +03:00
return fm
}
module.exports = {
getContentFrontmatter,
getNavigationLinks
}