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

42 lines
1.1 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
import frontMatter from 'front-matter'
import yaml from 'js-yaml'
import {globby} from 'globby'
import { fileURLToPath } from 'url'
import {join, dirname} from 'path'
const __dirname = dirname(fileURLToPath(import.meta.url))
2021-03-31 02:53:47 +03:00
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
}
export function getNavigationLinks() {
2021-03-31 02:53:47 +03:00
const nav = yaml.load(fs.readFileSync(join(docsPath, './src/@primer/gatsby-theme-doctocat/nav.yml'), 'utf8'))
return collectNavLinks(nav)
}
export 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
}