add ids in markdown headers automatically

This commit is contained in:
Sean Yesmunt 2021-03-28 23:41:10 -04:00
parent e4204bd6fe
commit cd79eaf6a4
3 changed files with 28 additions and 6 deletions

3
component/footer.jsx Normal file
View file

@ -0,0 +1,3 @@
export function Footer(props) {
return <footer></footer>;
}

View file

@ -34,14 +34,17 @@ export function Header(props) {
<Link href="/"> <Link href="/">
<span className="title">{__(m.title)}</span> <span className="title">{__(m.title)}</span>
</Link> </Link>
{!faqPage && (
<div className="header__links"> <div className="header__links">
{!faqPage ? (
<Link href="/faq"> <Link href="/faq">
<span className="link">FAQ</span> <span className="link">FAQ</span>
</Link> </Link>
</div> ) : (
<Link href="/">
<span className="link">Go Home</span>
</Link>
)} )}
</div>
</header> </header>
</> </>
); );

View file

@ -1,8 +1,22 @@
import React from 'react';
import ReactMarkdown from 'react-markdown'; import ReactMarkdown from 'react-markdown';
import { Header } from '../component/header'; import { Header } from '../component/header';
import { Email } from '../component/email'; import { Email } from '../component/email';
import md from '../faq.md'; import md from '../faq.md';
function flatten(text, child) {
return typeof child === 'string'
? text + child
: React.Children.toArray(child.props.children).reduce(flatten, text);
}
function HeadingRenderer(props) {
var children = React.Children.toArray(props.children);
var text = children.reduce(flatten, '');
var slug = text.toLowerCase().replace(/\W/g, '-');
return React.createElement('h' + props.level, { id: slug }, props.children);
}
export default function Faq() { export default function Faq() {
return ( return (
<div> <div>
@ -10,7 +24,9 @@ export default function Faq() {
<main> <main>
<div className="content md"> <div className="content md">
<ReactMarkdown>{md}</ReactMarkdown> <ReactMarkdown renderers={{ heading: HeadingRenderer }}>
{md}
</ReactMarkdown>
</div> </div>
<Email /> <Email />