Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
  • Loading branch information
hngngn committed Nov 25, 2024
1 parent 7fdd550 commit 3c705f3
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 200 deletions.
190 changes: 94 additions & 96 deletions docs/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,126 +1,124 @@
import { existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { mdx } from "@cyco130/vite-plugin-mdx";
import { defineConfig } from "@solidjs/start/config";
//@ts-expect-error
import pkg from "@vinxi/plugin-mdx";
import rehypeSlug from "rehype-slug";
import remarkFrontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import { readSync } from "to-vfile";
import { matter } from "vfile-matter";
const { default: mdx } = pkg;

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default defineConfig({
extensions: ["mdx"],
vite: () => {
// https://andi.dev/blog/how-solid-start-blog
const processFiles = () => {
const docs: {
headings?: { depth: number; slug: string; text: string }[];
frontmatter?: {
title: string;
description: string;
component: boolean;
link: {
doc: string;
api: string;
};
toc: boolean;
};
slug?: string;
}[] = [];

const outputFile = resolve(".content/index.js");
const docsDir = resolve("src/routes/docs");
// https://andi.dev/blog/how-solid-start-blog
const processFiles = () => {
const docs: {
headings?: { depth: number; slug: string; text: string }[];
frontmatter?: {
title: string;
description: string;
component: boolean;
link: {
doc: string;
api: string;
};
toc: boolean;
};
slug?: string;
}[] = [];

if (!existsSync(".content")) {
mkdirSync(".content", { recursive: true });
}
const outputFile = resolve(".content/index.js");
const docsDir = resolve("src/routes/docs");

(function processDirectory(currentDir: string) {
const items = readdirSync(currentDir, { withFileTypes: true });
if (!existsSync(".content")) {
mkdirSync(".content", { recursive: true });
}

for (const item of items) {
const fullPath = join(currentDir, item.name);
(function processDirectory(currentDir: string) {
const items = readdirSync(currentDir, { withFileTypes: true });

if (item.isDirectory()) {
processDirectory(fullPath);
} else if (item.isFile() && item.name.endsWith(".mdx")) {
const fileContent = readSync(fullPath, "utf-8");
matter(fileContent);
for (const item of items) {
const fullPath = join(currentDir, item.name);

const relativePath = relative(docsDir, fullPath);
const slug = `/docs/${relativePath.replace(/\.mdx$/, "").replace(/\\/g, "/")}`;
if (item.isDirectory()) {
processDirectory(fullPath);
} else if (item.isFile() && item.name.endsWith(".mdx")) {
const fileContent = readSync(fullPath, "utf-8");
matter(fileContent);

const headings: { depth: number; slug: string; text: string }[] =
[];
const regex = /^(#{1,6})\s+(.+)$/gm;
let match: RegExpExecArray | null;
const relativePath = relative(docsDir, fullPath);
const slug = `/docs/${relativePath.replace(/\.mdx$/, "").replace(/\\/g, "/")}`;

// biome-ignore lint/suspicious/noAssignInExpressions:
while ((match = regex.exec(String(fileContent))) !== null) {
const depth = match[1].length;
const text = match[2].trim();
const headings: { depth: number; slug: string; text: string }[] = [];
const regex = /^(#{1,6})\s+(.+)$/gm;
let match: RegExpExecArray | null;

headings.push({
depth,
text,
slug: text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)/g, ""),
});
}
// biome-ignore lint/suspicious/noAssignInExpressions:
while ((match = regex.exec(String(fileContent))) !== null) {
const depth = match[1].length;
const text = match[2].trim();

docs.push({
frontmatter: fileContent.data
.matter as (typeof docs)[0]["frontmatter"],
headings,
slug,
});
}
headings.push({
depth,
text,
slug: text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)/g, ""),
});
}
})(docsDir);

writeFileSync(
outputFile,
`export const allDocs = ${JSON.stringify(docs, null, 2)}`,
"utf-8",
);
};
docs.push({
frontmatter: fileContent.data
.matter as (typeof docs)[0]["frontmatter"],
headings,
slug,
});
}
}
})(docsDir);

return {
plugins: [
{
name: "posts-gen",
buildStart() {
processFiles();
},
configureServer(server) {
server.watcher.on("change", (filePath) => {
if (filePath.includes("/src/routes/docs")) {
processFiles();
}
});
},
writeFileSync(
outputFile,
`export const allDocs = ${JSON.stringify(docs, null, 2)}`,
"utf-8",
);
};

export default defineConfig({
extensions: ["mdx"],
vite: {
plugins: [
{
name: "posts-gen",
buildStart() {
processFiles();
},
mdx({
jsx: true,
jsxImportSource: "solid-js",
providerImportSource: "@/components/mdx",
remarkPlugins: [remarkGfm, remarkFrontmatter],
rehypePlugins: [rehypeSlug],
}),
],
resolve: {
alias: {
"@": resolve(__dirname, "./src"),
"#content": resolve(__dirname, "./.content"),
configureServer(server) {
server.watcher.on("change", (filePath) => {
if (filePath.includes("/src/routes/docs")) {
processFiles();
}
});
},
},
};
mdx.withImports({})({
jsx: true,
jsxImportSource: "solid-js",
providerImportSource: "@/components/mdx",
remarkPlugins: [remarkFrontmatter],
rehypePlugins: [rehypeSlug],
}),
],
resolve: {
alias: {
"@": resolve(__dirname, "./src"),
"#content": resolve(__dirname, "./.content"),
},
},
},
server: {
prerender: {
Expand Down
13 changes: 2 additions & 11 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,21 @@
"node": ">=18"
},
"devDependencies": {
"@cyco130/vite-plugin-mdx": "^2.1.6",
"@vinxi/plugin-mdx": "^3.7.2",
"@eslint/js": "^9.15.0",
"@repo/tailwindcss": "workspace:*",
"@typescript-eslint/parser": "^8.15.0",
"autoprefixer": "^10.4.20",
"eslint": "^9.15.0",
"eslint-config-biome": "^1.9.3",
"eslint-plugin-solid": "^0.14.4",
"mdast-util-from-markdown": "^2.0.2",
"mdast-util-frontmatter": "^2.0.1",
"micromark-extension-frontmatter": "^2.0.0",
"postcss": "^8.4.49",
"rehype-autolink-headings": "^7.1.0",
"rehype-slug": "^6.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"remark-rehype": "^11.1.1",
"tailwindcss": "^3.4.15",
"to-vfile": "^8.0.0",
"typescript-eslint": "^8.15.0",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.3",
"vfile-matter": "^5.0.0",
"vite-plugin-virtual": "^0.3.0",
"yaml": "^2.6.1"
"vfile-matter": "^5.0.0"
}
}
1 change: 1 addition & 0 deletions docs/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "./app.css";
import "./mdx.css";

const Toaster = clientOnly(
// @ts-expect-error
async () => (await import("@repo/tailwindcss/ui/sonner")).Toaster,
);

Expand Down
5 changes: 2 additions & 3 deletions docs/src/routes/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mdxComponents } from "@/components/mdx-components";
import { PageNavigation } from "@/components/page-navigation";
import { Sidebar } from "@/components/sidebar";
import { Toc } from "@/components/toc";
import { badgeVariants } from "@repo/tailwindcss/ui/badge";
import type { RouteSectionProps } from "@solidjs/router";
import { Show } from "solid-js";
// @ts-expect-error
Expand Down Expand Up @@ -57,7 +56,7 @@ const DocsLayout = (props: RouteSectionProps) => {
</p>
</Show>
</div>
<Show when={data().frontmatter.link}>
{/* <Show when={data().frontmatter.link}>
<div class="flex items-center space-x-2 pt-4">
<Show when={data().frontmatter.link?.doc}>
<a
Expand Down Expand Up @@ -117,7 +116,7 @@ const DocsLayout = (props: RouteSectionProps) => {
</a>
</Show>
</div>
</Show>
</Show> */}
<div class="max-w-full pb-12 pt-8">
<MDXProvider components={mdxComponents}>
{props.children}
Expand Down
Loading

0 comments on commit 3c705f3

Please sign in to comment.