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 27ea065
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 126 deletions.
192 changes: 90 additions & 102 deletions docs/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,123 +12,111 @@ import { matter } from "vfile-matter";
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({
ssr: false,
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();
}
});
},
},
};
},
server: {
prerender: {
crawlLinks: true,
},
esbuild: {
options: {
target: "esnext",
mdx({
jsx: true,
jsxImportSource: "solid-js",
providerImportSource: "@/components/mdx",
remarkPlugins: [remarkGfm, remarkFrontmatter],
rehypePlugins: [rehypeSlug],
}),
],
resolve: {
alias: {
"@": resolve(__dirname, "./src"),
"#content": resolve(__dirname, "./.content"),
},
},
},
Expand Down
39 changes: 15 additions & 24 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [
"**/.env.*local"
],
"tasks": {
"build": {
"dependsOn": [
"^build"
],
"outputs": [
".next/**",
"!.next/cache/**"
]
},
"check": {
"dependsOn": [
"^check"
]
},
"dev": {
"cache": false,
"persistent": true
}
}
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["**/.env.*local"],
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": [".vercel/output/**", ".vinxi/**"]
},
"check": {
"dependsOn": ["^check"]
},
"dev": {
"cache": false,
"persistent": true
}
}
}

0 comments on commit 27ea065

Please sign in to comment.