From eded03562cdb2831f1e30266bcb159ccf988f821 Mon Sep 17 00:00:00 2001 From: Shawn Allen Date: Mon, 3 Dec 2018 15:53:47 -0800 Subject: [PATCH] add metadata support --- docs/pages/_app.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/pages/_app.js b/docs/pages/_app.js index 9e03fac6..5bc49c53 100644 --- a/docs/pages/_app.js +++ b/docs/pages/_app.js @@ -5,16 +5,23 @@ import Head from 'next/head' import {withMDXLive} from 'mdx-live' import getConfig from 'next/config' import Octicon, {Pencil} from '@githubprimer/octicons-react' +import {basename, join} from 'path' import * as docComponents from './doc-components' import * as primerComponents from '@primer/components' -import {repository} from 'primer/package.json' -const {pageMap} = getConfig().publicRuntimeConfig const {BaseStyles, Box, Flex, Link, Text, theme} = primerComponents const {SideNav, Header, IndexHero} = docComponents const DocLink = props => -const editLinkBase = `https://github.com/${repository}/edit/master/pages` + +const ext = /\.mdx?$/ +const context = require.context('.', true, /\.mdx?$/) +const pathMap = context.keys().reduce((map, key) => { + const base = key.replace(ext, '').replace(/\/index$/, '') + const path = base.substr(1) // strip the leading "." + map[path] = key + return map +}, {}) const components = { ...docComponents, @@ -39,19 +46,24 @@ export default class MyApp extends App { render() { const {pathname} = this.props.router - const filename = pageMap[pathname] + const filename = pathMap[pathname] if (!filename) { // eslint-disable-next-line no-console - console.warn(`pathname "${pathname}" doesn't exist in pageMap:`, pageMap) + console.warn(`pathname "${pathname}" doesn't exist in:`, Object.keys(pathMap)) } const {Component, page} = this.props const hasHero = ['/css', '/css/'].includes(pathname) + const meta = {} + if (filename) { + Object.assign(meta, context(filename).meta) + } + return ( - Primer CSS + Primer CSS{meta.title ? ` / ${meta.title}` : null}
@@ -60,18 +72,9 @@ export default class MyApp extends App { +
meta: {JSON.stringify(meta, null, 2)}
- {filename && ( - - - - - - Edit this page - {' '} - on GitHub - - )} + {/* TODO: bring back edit link! */}