Skip to content

Commit

Permalink
move extractHeadings to non-client utils file to fix docs + blog tabl…
Browse files Browse the repository at this point in the history
…e of contents
  • Loading branch information
kennedybaird committed Aug 9, 2024
1 parent 06cce1a commit 1cbe903
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 128 deletions.
3 changes: 2 additions & 1 deletion docs/app/(site)/blog/[post]/page-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import Link from 'next/link'
import { useParams } from 'next/navigation'
import { parse, format } from 'date-fns'

import { extractHeadings, Markdoc } from '../../../../components/Markdoc'
import { Markdoc } from '../../../../components/Markdoc'
import { BlogPage } from '../../../../components/Page'
import { Heading } from '../../../../components/docs/Heading'
import { Type } from '../../../../components/primitives/Type'
import { type BlogPost } from './page'
import { extractHeadings } from '../../../../markdoc/headings'

export default function Page ({ post }: { post: BlogPost }) {
const params = useParams()
Expand Down
11 changes: 1 addition & 10 deletions docs/app/(site)/docs/[...rest]/page-client.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
'use client'

import { useParams } from 'next/navigation'

import { type Document } from './page'

import { extractHeadings, Markdoc } from '../../../../components/Markdoc'
import { Markdoc } from '../../../../components/Markdoc'

import { Heading } from '../../../../components/docs/Heading'

export default function PageClient ({ document }: { document: Document }) {
const params = useParams()

const headings = [
{ id: 'title', depth: 1, label: document.title },
...extractHeadings(document.content),
]

return (
<>
<Heading level={1} id="title">
Expand Down
4 changes: 2 additions & 2 deletions docs/app/(site)/docs/[...rest]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { type Tag, transform } from '@markdoc/markdoc'

import { reader } from '../../../../keystatic/reader'
import { baseMarkdocConfig } from '../../../../markdoc/config'
import { extractHeadings } from '../../../../markdoc/headings'
import PageClient from './page-client'
import { type EntryWithResolvedLinkedFiles } from '@keystatic/core/reader'
import type keystaticConfig from '../../../../keystatic.config'
import { DocsLayout } from '../../../../components/docs/DocsLayout'
import { extractHeadings } from '../../../../components/Markdoc'

export type Document = NonNullable<
Pick<
Expand All @@ -31,7 +31,7 @@ export default async function DocPage ({ params }) {

const headings = [
{ id: 'title', depth: 1, label: transformedDoc.title },
// ...extractHeadings(transformedDoc.content),
...extractHeadings(transformedDoc.content),
]

return (
Expand Down
35 changes: 1 addition & 34 deletions docs/components/Markdoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,4 @@ export function Markdoc (props: { content: RenderableTreeNodes }) {
}

return render(props.content) as JSX.Element
}

export type HeadingType = {
id: string
depth: number
label: string
}

export function extractHeadings (content: Tag): HeadingType[] {
const headings: HeadingType[] = []
for (const child of content.children) {
if (isTag(child) && child.name === 'Heading') {
headings.push({
id: child.attributes.id,
depth: child.attributes.level,
label: stringifyDocContent(child),
})
}
}
return headings
}

function stringifyDocContent (node: RenderableTreeNode): string {
if (typeof node === 'string') {
return node
}
if (Array.isArray(node)) {
return node.map(stringifyDocContent).join('')
}
if (!isTag(node)) {
return ''
}
return node.children.map(stringifyDocContent).join('')
}
}
2 changes: 1 addition & 1 deletion docs/components/docs/DocsLayoutClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use client'

import { type ReactNode, useRef } from 'react'
import { type HeadingType } from '../Markdoc'
import { usePathname } from 'next/navigation'
import { Header } from '../Header'
import { Wrapper } from '../primitives/Wrapper'
Expand All @@ -15,6 +14,7 @@ import { TableOfContents } from './TableOfContents'

import { useMediaQuery } from '../../lib/media'
import { DocsFooter } from '../Footer'
import { type HeadingType } from '../../markdoc/headings'

export function DocsLayoutClient ({
children,
Expand Down
38 changes: 38 additions & 0 deletions docs/markdoc/headings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@



import type { RenderableTreeNode, Tag } from '@markdoc/markdoc'
import { isTag } from './isTag'

export type HeadingType = {
id: string
depth: number
label: string
}

export function extractHeadings (content: Tag): HeadingType[] {
const headings: HeadingType[] = []
for (const child of content.children) {
if (isTag(child) && child.name === 'Heading') {
headings.push({
id: child.attributes.id,
depth: child.attributes.level,
label: stringifyDocContent(child),
})
}
}
return headings
}

function stringifyDocContent (node: RenderableTreeNode): string {
if (typeof node === 'string') {
return node
}
if (Array.isArray(node)) {
return node.map(stringifyDocContent).join('')
}
if (!isTag(node)) {
return ''
}
return node.children.map(stringifyDocContent).join('')
}
Loading

0 comments on commit 1cbe903

Please sign in to comment.