Skip to content
Snippets Groups Projects
Commit 45e00c35 authored by Joseph Zeng's avatar Joseph Zeng
Browse files

fix heading errors

parent 7f963d67
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import { onlyText } from "react-children-utilities";
import { ContentLayout } from "../layouts/contentLayout";
import { Image } from "./media";
import { useRouter } from "next/router";
const H2IdContext = createContext({
store: [],
......@@ -29,12 +30,13 @@ export const AppProvider = ({ children }) => {
const text = onlyText(props.children);
// get id or generate id from children
const id = (props.id || text.replace(/\s/g, "-")).toLowerCase();
const { asPath } = useRouter();
useEffect(() => {
// add {id, text} to store if not present
if (!store.find((item) => item.id === id)) {
setStore([...store, { level, id, text }]);
setStore([...store, { level, id, text, route:asPath }]);
}
}, [id, text]);
}, [id, text, asPath]);
return <Component {...{ id, ...props }} />;
};
......
import { useMemo } from "react";
import { BasicLayout } from "./basicLayout";
import { useH2Id } from "../components/components";
import { useRouter } from "next/router";
const TOC = () => {
const store = useH2Id();
const { asPath } = useRouter();
// map store to layered headings for nested lists, levels 0 and 1
const headings = useMemo(
() =>
store
.filter((item) => item.route === asPath)
.map(({ text, id, level }) => {
return { text, id, level, children: [] };
})
......@@ -19,13 +22,19 @@ const TOC = () => {
try {
acc[acc.length - 1].children.push(heading);
} catch (error) {
acc.push(heading);
acc.push({
text: "",
id: "",
level: 0,
children: [heading],
});
}
}
return acc;
}, []),
[store]
[store, asPath]
);
console.log({store})
// https://static.igem.wiki/teams/4275/wiki/toc-celly.png
// add this image of celly hanging on toc to top right corner
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment