Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
noxify committed Dec 14, 2023
1 parent 238c2e8 commit 4d1862c
Show file tree
Hide file tree
Showing 24 changed files with 129 additions and 132 deletions.
10 changes: 5 additions & 5 deletions apps/nextjs/src/content/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Corporis cognosces Peleus tibi
Lorem markdownum tu peto? Fessa peteretur tyranni nota aquaticus iussere, io
vivere, conditur meum ut lassaque. Adsiduo perveni Atrides postquam alte illa et
spernenda, late ferarum, modo. Reticere munus sui vertit est nescius adhuc
insuperabile nidos! Tumidam *hospes* ferrum a pulsat inferius: aut es marmoris!
insuperabile nidos! Tumidam _hospes_ ferrum a pulsat inferius: aut es marmoris!

> Pectore ursaeque, fuit prolisque erit amnesque: nam datis viro caecaque
> resupino notavi. Quoque dolentius multi, has quicquam magni, ruunt omnia
> imagine? Delphice quae illo est cruor adhuc telisque et manu, amor,
> *causamque* te vestes; rerum.
> _causamque_ te vestes; rerum.
## Carituraque debet penetrabile alta imagine

Expand All @@ -28,13 +28,13 @@ vidit, profatur potes promptu manu!
Cadit **tangit quo** sine valuere nulla, Philemon quoque luctantiaque. [Ante
exsecrantia](http://orant.com/sed.php) alis iam! Fluctusque utinam pennis deum
pocula cum. Tetigit avis aequoreis posses alto Tyriis, voce reor habet solet
postquam *adsuetaque* regnat medium locis.
postquam _adsuetaque_ regnat medium locis.

## Tanto ortas harenis collaque dant

Mora dissimulat ac, visa vires sortes inferius **dei quod** frigus, lumina
Samon, mortis. **Parte fluctus** tum, in invitam satis, *nominat retentis
videre* finis tu et mendacia nomen. Huic illic abdita Terram.
Samon, mortis. **Parte fluctus** tum, in invitam satis, _nominat retentis
videre_ finis tu et mendacia nomen. Huic illic abdita Terram.

Est verso praedam repetitaque mons esse, mora petenti haec scopulus ille
medioque, ne verumque, ales. Sideraque invidit. Habet et, quod piae aquis
Expand Down
2 changes: 1 addition & 1 deletion packages/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { cn } from "./src/cn";
export { cn } from "./src/cn"
7 changes: 3 additions & 4 deletions packages/helpers/src/cn.ts
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))
}
2 changes: 1 addition & 1 deletion packages/markdoc-accordion/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from "./src"
2 changes: 1 addition & 1 deletion packages/markdoc-accordion/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const name = 'markdoc-accordion';
export const name = "markdoc-accordion"
8 changes: 4 additions & 4 deletions packages/markdoc-base/index.ts
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"
10 changes: 5 additions & 5 deletions packages/markdoc-base/src/frontmatter.ts
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
}
65 changes: 32 additions & 33 deletions packages/markdoc-base/src/helpers.ts
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
}
10 changes: 5 additions & 5 deletions packages/markdoc-base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Config } from "@markdoc/markdoc";
import type { Config } from "@markdoc/markdoc"

import { Heading, config as headingConfig } from "@acme/markdoc-heading";
import { Heading, config as headingConfig } from "@acme/markdoc-heading"

const config: Config = {
nodes: {
Expand Down Expand Up @@ -129,7 +129,7 @@ const config: Config = {
// },
// },
},
};
}

const components = {
// Paragraph: TypographyP,
Expand All @@ -149,6 +149,6 @@ const components = {
// Accordion: MarkdocAccordion,
// AccordionItem: MarkdocAccordionItem,
// CustomLink: CustomLink,
};
}

export { config, components };
export { config, components }
2 changes: 1 addition & 1 deletion packages/markdoc-callout/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from "./src"
2 changes: 1 addition & 1 deletion packages/markdoc-callout/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const name = 'markdoc-callout';
export const name = "markdoc-callout"
2 changes: 1 addition & 1 deletion packages/markdoc-codeblock/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from "./src"
2 changes: 1 addition & 1 deletion packages/markdoc-codeblock/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const name = 'markdoc-codeblock';
export const name = "markdoc-codeblock"
2 changes: 1 addition & 1 deletion packages/markdoc-custom-link/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from "./src"
2 changes: 1 addition & 1 deletion packages/markdoc-custom-link/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const name = 'markdoc-custom-link';
export const name = "markdoc-custom-link"
4 changes: 2 additions & 2 deletions packages/markdoc-heading/index.ts
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"
19 changes: 9 additions & 10 deletions packages/markdoc-heading/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Markdoc, { Tag } from "@markdoc/markdoc";
import type { Schema } from "@markdoc/markdoc";
import { slugifyWithCounter } from "@sindresorhus/slugify";
import Markdoc, { Tag, type Schema } from "@markdoc/markdoc"
import { slugifyWithCounter } from "@sindresorhus/slugify"

const slugify = slugifyWithCounter();
const slugify = slugifyWithCounter()

export const config: Schema = {
render: "Heading",
Expand All @@ -11,12 +10,12 @@ export const config: Schema = {
level: { type: Number, required: true },
},
transform(node, config) {
const attributes = node.transformAttributes(config);
const children = node.transformChildren(config);
const renderedContent = Markdoc.renderers.html(children);
const attributes = node.transformAttributes(config)
const children = node.transformChildren(config)
const renderedContent = Markdoc.renderers.html(children)
if (!attributes.id) {
attributes.id = slugify(renderedContent);
attributes.id = slugify(renderedContent)
}
return new Tag(this.render, attributes, children);
return new Tag(this.render, attributes, children)
},
};
}
2 changes: 1 addition & 1 deletion packages/markdoc-tabs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from "./src"
2 changes: 1 addition & 1 deletion packages/markdoc-tabs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const name = 'markdoc-tabs';
export const name = "markdoc-tabs"
Loading

0 comments on commit 4d1862c

Please sign in to comment.