Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Implement a TOC component #1127

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const SidebarLayout = ({ children }: { children: React.ReactNode }) => {
variant='sidebar'
/>
</div>
<div className='col-span-4 md:col-span-3 lg:mt-20 lg:w-5/6 mx-4 md:mx-0'>
<div className='col-span-4 md:col-span-3 lg:mt-20 mx-4 md:mx-0'>
{children}
</div>
</div>
Expand Down
115 changes: 50 additions & 65 deletions components/StyledMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,91 +574,76 @@ export function TableOfContentMarkdown({
h2:
depth === 0
? {
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
return (
<a
href={`#${slug}`}
className='block cursor-pointer mb-3 ml- text-slate-600 hover:text-primary active:text-primary dark:text-slate-300 leading-4 font-medium'
>
{children}
</a>
);
},
}
: depth >= 2
? {
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
const [isChrome, setIsChrome] = useState(false);

useEffect(() => {
const chromeCheck =
/Chrome/.test(navigator.userAgent) &&
/Google Inc/.test(navigator.vendor);
setIsChrome(chromeCheck);
}, []);

return (
// chromeClass
<a
href={`#${slug}`}
className='block cursor-pointer mb-3 text-slate-600 dark:text-slate-300 leading-5 font-medium ml-4'
>

{children}
</a>
);
},
}
: depth >= 2
? {
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
const [isChrome, setIsChrome] = useState(false);

useEffect(() => {
const chromeCheck =
/Chrome/.test(navigator.userAgent) &&
/Google Inc/.test(navigator.vendor);
setIsChrome(chromeCheck);
}, []);

return (
// chromeClass
<a
href={`#${slug}`}
className={`block cursor-pointer mb-3 max-sm:text-sm text-slate-600 dark:text-slate-300 leading-4 ] max-sm:-ml-[6px] font-medium ${isChrome ? '-ml-[4.8px]' : '-ml-[6.5px]'}`}
>
<span className='mr-1 text-blue-400 text-[0.7em]'>
&#9679;
</span>
{children}
</a>
);
},
}
: { component: () => null },
h3:
depth >= 3
? {
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
return (
<a
href={`#${slug}`}
className='flex flex-row items-center cursor-pointer mb-3 max-sm:text-sm text-slate-600 dark:text-slate-300 leading-4 ml-[-0.25rem]'
>
<span className='text-blue-400/40 font-extrabold text-[0.8em] max-sm:text-[1.2em] ml-1'>
&mdash;&mdash;
</span>
<span className='mr-1 text-blue-400/90 text-[0.7em] flex justify-center items-center'>
&#9679;
</span>
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
return (
<a
href={`#${slug}`}
className='flex flex-row items-center cursor-pointer mb-3 max-sm:text-sm text-slate-600 dark:text-slate-300 leading-4 ml-4'
>

{children}
</a>
);
},
}
{children}
</a>
);
},
}
: { component: () => null },
h4:
depth >= 4
depth <= 4
? {
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
return (
<a
href={`#${slug}`}
className='flex flex-row items-center cursor-pointer mb-3 max-sm:text-sm text-slate-600 dark:text-slate-300 leading-4 ml-[-0.25rem] '
>
<span className='text-blue-400/40 font-extrabold text-[0.8em] ml-1 max-sm:text-[1.2em]'>
&mdash;&mdash;&mdash;&mdash;
</span>
<span className='mr-1 text-blue-400/90 text-[0.7em] flex justify-center items-center'>
&#9679;
</span>

{children}
</a>
);
},
} /* eslint-enable */
component: ({ children }) => {
const slug = slugifyMarkdownHeadline(children);
return (
<a
href={`#${slug}`}
className='flex flex-row items-center cursor-pointer mb-3 max-sm:text-sm text-slate-600 dark:text-slate-300 leading-4 ml-8 '
>
{children}
</a>
);
},
} /* eslint-enable */
: { component: () => null },
...hiddenElements(
'strong',
Expand Down
25 changes: 19 additions & 6 deletions pages/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getStaticMarkdownProps from '~/lib/getStaticMarkdownProps';
import { Headline1 } from '~/components/Headlines';
import { SectionContext } from '~/context';
import { DocsHelp } from '~/components/DocsHelp';
import { TableOfContentMarkdown } from '~/components/StyledMarkdown';

export async function getStaticPaths() {
return getStaticMarkdownPaths('pages');
Expand All @@ -26,12 +27,24 @@ export default function StaticMarkdownPage({
const newTitle = 'JSON Schema - ' + frontmatter.title;
return (
<SectionContext.Provider value={frontmatter.section || null}>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<div className='flex pt-4'>
<div className='w-full pr-5'>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
</div>
<div className='w-2/5 lg:block mt-10 hidden sticky top-24 h-[calc(100vh-6rem)] overflow-hidden'>
<div className='h-full overflow-y-auto scrollbar-hidden pl-5'>
<div className='uppercase text-xs text-slate-400 mb-4'>
On this page
</div>
<TableOfContentMarkdown markdown={content} depth={3} />
</div>
</div>
</div>
</SectionContext.Provider>
);
}
Expand Down
28 changes: 19 additions & 9 deletions pages/draft-05/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import { SectionContext } from '~/context';
import DocTable from '~/components/DocTable';
import { Headline1 } from '~/components/Headlines';
import { DocsHelp } from '~/components/DocsHelp';
import { TableOfContentMarkdown } from '~/components/StyledMarkdown';

export async function getStaticProps() {
const index = fs.readFileSync('pages/draft-05/index.md', 'utf-8');
const main = fs.readFileSync('pages/draft-05/release-notes.md', 'utf-8');
const index = fs.readFileSync('pages/draft/2020-12/index.md', 'utf-8');
const { content: indexContent, data: indexData } = matter(index);
const { content: bodyContent } = matter(main);

const frontmatter = { ...indexData };
return {
props: {
blocks: {
index: indexContent,
body: bodyContent,
},
frontmatter,
},
Expand All @@ -35,12 +33,24 @@ export default function ImplementationsPages({
}) {
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<StyledMarkdown markdown={blocks.body} />
<DocsHelp />
<div className='flex pt-4'>
<div className='w-full pr-5'>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
</div>
<div className='w-2/5 lg:block mt-10 hidden sticky top-24 h-[calc(100vh-6rem)] overflow-hidden'>
<div className='h-full overflow-y-auto scrollbar-hidden pl-5'>
<div className='uppercase text-xs text-slate-400 mb-4'>
On this page
</div>
<TableOfContentMarkdown markdown={blocks.index} depth={3} />
</div>
</div>
</div>
</SectionContext.Provider>
);
}

ImplementationsPages.getLayout = getLayout;
29 changes: 22 additions & 7 deletions pages/draft-06/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import getStaticMarkdownProps from '~/lib/getStaticMarkdownProps';
import { Headline1 } from '~/components/Headlines';
import { SectionContext } from '~/context';
import { DocsHelp } from '~/components/DocsHelp';
import { TableOfContentMarkdown } from '~/components/StyledMarkdown';

export async function getStaticPaths() {
return getStaticMarkdownPaths('pages/draft-06');
}

export async function getStaticProps(args: any) {
return getStaticMarkdownProps(args, 'pages/draft-06');
}
Expand All @@ -23,17 +25,30 @@ export default function StaticMarkdownPage({
content: any;
}) {
const markdownFile = '_index';
const newTitle = 'JSON Schema - ' + frontmatter.title;
const newTitle = `JSON Schema - ${frontmatter.title}`;

return (
<SectionContext.Provider value={frontmatter.section || null}>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<div className='flex pt-4'>
<div className='w-full pr-5'>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
</div>
<div className='w-2/5 lg:block mt-10 hidden sticky top-24 h-[calc(100vh-6rem)] overflow-hidden'>
<div className='h-full overflow-y-auto scrollbar-hidden pl-5'>
<div className='uppercase text-xs text-slate-400 mb-4'>
On this page
</div>
<TableOfContentMarkdown markdown={content} depth={3} />
</div>
</div>
</div>
</SectionContext.Provider>
);
}

StaticMarkdownPage.getLayout = getLayout;
22 changes: 17 additions & 5 deletions pages/draft-06/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SectionContext } from '~/context';
import DocTable from '~/components/DocTable';
import { Headline1 } from '~/components/Headlines';
import { DocsHelp } from '~/components/DocsHelp';

import { TableOfContentMarkdown } from '~/components/StyledMarkdown';
export async function getStaticProps() {
const index = fs.readFileSync('pages/draft-06/index.md', 'utf-8');

Expand All @@ -33,10 +33,22 @@ export default function ImplementationsPages({
}) {
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
<div className='flex pt-4'>
<div className='w-full pr-5'>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
</div>
<div className='w-2/5 lg:block mt-10 hidden sticky top-24 h-[calc(100vh-6rem)] overflow-hidden'>
<div className='h-full overflow-y-auto scrollbar-hidden pl-5'>
<div className='uppercase text-xs text-slate-400 mb-4'>
On this page
</div>
<TableOfContentMarkdown markdown={blocks.index} depth={3} />
</div>
</div>
</div>
</SectionContext.Provider>
);
}
Expand Down
26 changes: 19 additions & 7 deletions pages/draft-07/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getStaticMarkdownProps from '~/lib/getStaticMarkdownProps';
import { Headline1 } from '~/components/Headlines';
import { SectionContext } from '~/context';
import { DocsHelp } from '~/components/DocsHelp';

import { TableOfContentMarkdown } from '~/components/StyledMarkdown';
export async function getStaticPaths() {
return getStaticMarkdownPaths('pages/draft-07');
}
Expand All @@ -27,12 +27,24 @@ export default function StaticMarkdownPage({

return (
<SectionContext.Provider value={frontmatter.section || null}>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<div className='flex pt-4'>
<div className='w-full pr-5'>
<Head>
<title>{newTitle}</title>
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
</div>
<div className='w-2/5 lg:block mt-10 hidden sticky top-24 h-[calc(100vh-6rem)] overflow-hidden'>
<div className='h-full overflow-y-auto scrollbar-hidden pl-5'>
<div className='uppercase text-xs text-slate-400 mb-4'>
On this page
</div>
<TableOfContentMarkdown markdown={content} depth={3} />
</div>
</div>
</div>
</SectionContext.Provider>
);
}
Expand Down
Loading
Loading