import React, {
useState,
useEffect,
useMemo,
useContext,
createContext,
} from 'react';
import { useRouter } from 'next/router';
import Head from 'next/head';
import Link from 'next/link';
import slugify from '@sindresorhus/slugify';
import 'focus-visible';
import cn from 'classnames';
import { SkipNavContent } from '@reach/skip-nav';
import Theme from './theme';
import SSGContext from './ssg';
import Search from './search';
// import DocSearch from './docsearch'
import GitHubIcon from './github-icon';
import ArrowRight from './arrow-right';
import getDirectories from './directories';
import getConfig from './config';
import * as ReactDOM from 'react-dom/server';
import stripHtml from 'string-strip-html';
const config = getConfig();
const directories = getDirectories();
const TreeState = new Map();
const titleType = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
const MenuContext = createContext(false);
const flatten = list => {
return list.reduce((flat, toFlatten) => {
return flat.concat(
toFlatten.children ? flatten(toFlatten.children) : toFlatten
);
}, []);
};
const flatDirectories = flatten(directories);
function Folder({ item, anchors }) {
const route = useRouter().route + '/';
const active = route.startsWith(item.route + '/');
const open = TreeState[item.route] ?? true;
const [_, render] = useState(false);
useEffect(() => {
if (active) {
TreeState[item.route] = true;
}
}, [active]);
return (
);
}
function File({ item, anchors }) {
const { setMenu } = useContext(MenuContext);
const route = useRouter().route + '/';
const active = route.startsWith(item.route + '/');
let title = item.title;
// if (item.title.startsWith('> ')) {
// title = title.substr(2)
if (anchors?.length) {
if (active) {
return (
{title}
);
}
}
return (
setMenu(false)} className="focus:shadow-outline">
{title}
);
}
function Menu({ dir, anchors }) {
return (
{dir.map(item => {
if (item.children) {
return ;
}
return ;
})}
);
}
function Sidebar({ show, anchors }) {
return (
);
}
const NextLink = ({ currentIndex }) => {
let next = flatDirectories[currentIndex + 1];
if (!config.nextLinks || !next) {
return null;
}
return (
{next.title}
);
};
const PrevLink = ({ currentIndex }) => {
let prev = flatDirectories[currentIndex - 1];
if (!config.prevLinks || !prev) {
return null;
}
return (
{prev.title}
);
};
const Layout = ({ filename, full, title: _title, ssg = {}, children }) => {
const [menu, setMenu] = useState(false);
const router = useRouter();
const { route, pathname } = router;
const filepath = route.slice(0, route.lastIndexOf('/') + 1);
const titles = React.Children.toArray(children).filter(child =>
titleType.includes(child.props.mdxType)
);
const anchors = titles
.filter(child => child.props.mdxType === 'h2')
.map(child => child.props.children);
useEffect(() => {
if (menu) {
document.body.classList.add('overflow-hidden');
} else {
document.body.classList.remove('overflow-hidden');
}
}, [menu]);
const currentIndex = useMemo(
() => flatDirectories.findIndex(dir => dir.route === pathname),
[flatDirectories, pathname]
);
const title =
flatDirectories[currentIndex]?.title ||
titles.find(child => child.props.mdxType === 'h1')?.props.children ||
'Untitled';
const props = {
filepath: filepath + filename,
route,
};
return (
<>
{title}
{config.titleSuffix || ''}
{config.head ? config.head(props) : null}
{full ? (
{children}
) : (
<>
{children}
{' '}
>
)}
>
);
};
export default filename => {
return props => ;
};