This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
129 additions
and
132 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { cn } from "./src/cn"; | ||
export { cn } from "./src/cn" |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { clsx } from "clsx"; | ||
import type { ClassValue } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { clsx, type ClassValue } from "clsx" | ||
import { twMerge } from "tailwind-merge" | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
return twMerge(clsx(inputs)) | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src'; | ||
export * from "./src" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const name = 'markdoc-accordion'; | ||
export const name = "markdoc-accordion" |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Markdoc from "@markdoc/markdoc"; | ||
import Markdoc from "@markdoc/markdoc" | ||
|
||
import { components, config } from "./src"; | ||
import { components, config } from "./src" | ||
|
||
export { Markdoc, config, components }; | ||
export { Markdoc, config, components } | ||
|
||
export * from "./src/helpers"; | ||
export * from "./src/helpers" |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { z } from "zod"; | ||
import { parse as parseFrontmatter } from "zod-matter"; | ||
import { z } from "zod" | ||
import { parse as parseFrontmatter } from "zod-matter" | ||
|
||
export function getFrontmatter(fileContent: string) { | ||
const frontmatterProps = z.object({ | ||
title: z.string(), | ||
description: z.string().optional(), | ||
}); | ||
}) | ||
|
||
const { data: frontmatter } = parseFrontmatter(fileContent, frontmatterProps); | ||
const { data: frontmatter } = parseFrontmatter(fileContent, frontmatterProps) | ||
|
||
return frontmatter; | ||
return frontmatter | ||
} |
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 |
---|---|---|
@@ -1,92 +1,91 @@ | ||
import type { PathLike } from "node:fs"; | ||
import fs from "node:fs/promises"; | ||
import type { Node as MarkdocNode, Tag } from "@markdoc/markdoc"; | ||
import Markdoc from "@markdoc/markdoc"; | ||
import { stripHtml } from "string-strip-html"; | ||
import type { PathLike } from "node:fs" | ||
import fs from "node:fs/promises" | ||
import Markdoc, { type Node as MarkdocNode, type Tag } from "@markdoc/markdoc" | ||
import { stripHtml } from "string-strip-html" | ||
|
||
import { getFrontmatter } from "./frontmatter"; | ||
import { config } from "./index"; | ||
import { getFrontmatter } from "./frontmatter" | ||
import { config } from "./index" | ||
|
||
interface HeadingNode extends MarkdocNode { | ||
type: "heading"; | ||
type: "heading" | ||
attributes: { | ||
level: 1 | 2 | 3 | 4 | 5 | 6; | ||
id: string; | ||
[key: string]: unknown; | ||
}; | ||
level: 1 | 2 | 3 | 4 | 5 | 6 | ||
id: string | ||
[key: string]: unknown | ||
} | ||
} | ||
|
||
export interface TableOfContents { | ||
id: string; | ||
title: string; | ||
level: number; | ||
children?: TableOfContents[]; | ||
id: string | ||
title: string | ||
level: number | ||
children?: TableOfContents[] | ||
} | ||
|
||
export async function getFileContent(filePath: PathLike | fs.FileHandle) { | ||
try { | ||
return await fs.readFile(filePath, { encoding: "utf-8" }); | ||
return await fs.readFile(filePath, { encoding: "utf-8" }) | ||
} catch (e) { | ||
return null; | ||
return null | ||
} | ||
} | ||
|
||
export async function parseContent(filePath: PathLike | fs.FileHandle) { | ||
const fileContent = await getFileContent(filePath); | ||
const fileContent = await getFileContent(filePath) | ||
|
||
if (!fileContent) return; | ||
if (!fileContent) return | ||
|
||
const frontmatter = getFrontmatter(fileContent); | ||
const frontmatter = getFrontmatter(fileContent) | ||
|
||
const ast = Markdoc.parse(fileContent); | ||
const content = Markdoc.transform(ast, config); | ||
const ast = Markdoc.parse(fileContent) | ||
const content = Markdoc.transform(ast, config) | ||
|
||
// since RenderableTreeNode could be a string | ||
// we use the `Tag` type for content | ||
// otherwise we get some type issues and I have no idea | ||
// how I could solve it | ||
const headings = ((content as Tag).children as Tag[]).filter((ele) => { | ||
return ele.name == "Heading"; | ||
}); | ||
return ele.name == "Heading" | ||
}) | ||
|
||
return { | ||
content, | ||
...frontmatter, | ||
toc: generateTableOfContents(headings), | ||
}; | ||
} | ||
} | ||
|
||
export function generateTableOfContents(headings: Tag[]) { | ||
const sections: TableOfContents[] = []; | ||
const sections: TableOfContents[] = [] | ||
|
||
for (const heading of headings) { | ||
const attributes = (heading as unknown as HeadingNode).attributes; | ||
const attributes = (heading as unknown as HeadingNode).attributes | ||
|
||
if (attributes.level == 2 || attributes.level == 3) { | ||
const strippedTitle = stripHtml(Markdoc.renderers.html(heading)).result; | ||
const strippedTitle = stripHtml(Markdoc.renderers.html(heading)).result | ||
|
||
if (attributes.level == 3) { | ||
if (!sections[sections.length - 1]) { | ||
throw new Error( | ||
`Cannot add "h3" to table of contents without a preceding "h2" - Heading Title: ${strippedTitle}`, | ||
); | ||
) | ||
} | ||
|
||
sections[sections.length - 1]?.children!.push({ | ||
id: attributes.id, | ||
level: attributes.level, | ||
title: strippedTitle, | ||
}); | ||
}) | ||
} else { | ||
sections.push({ | ||
id: attributes.id, | ||
level: attributes.level, | ||
title: strippedTitle, | ||
children: [], | ||
}); | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return sections; | ||
return sections | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src'; | ||
export * from "./src" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const name = 'markdoc-callout'; | ||
export const name = "markdoc-callout" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src'; | ||
export * from "./src" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const name = 'markdoc-codeblock'; | ||
export const name = "markdoc-codeblock" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src'; | ||
export * from "./src" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const name = 'markdoc-custom-link'; | ||
export const name = "markdoc-custom-link" |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { config } from "./src/config"; | ||
export { Heading } from "./src/Heading"; | ||
export { config } from "./src/config" | ||
export { Heading } from "./src/Heading" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './src'; | ||
export * from "./src" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const name = 'markdoc-tabs'; | ||
export const name = "markdoc-tabs" |
Oops, something went wrong.