-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move extractHeadings to non-client utils file to fix docs + blog tabl…
…e of contents
- Loading branch information
1 parent
06cce1a
commit 1cbe903
Showing
7 changed files
with
124 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('') | ||
} |
Oops, something went wrong.