Skip to content

Commit

Permalink
fixed static paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Oct 8, 2023
1 parent 4b5e61f commit d6796fd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 229 deletions.
176 changes: 0 additions & 176 deletions docusaurus.config.js

This file was deleted.

8 changes: 1 addition & 7 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import withMarkdoc from "@markdoc/next.js";

import withSearch from "./src/markdoc/search.mjs";

/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ["js", "jsx", "md", "ts", "tsx"],
Expand All @@ -15,6 +11,4 @@ const nextConfig = {
},
};

export default withSearch(
withMarkdoc({ schemaPath: "./src/markdoc" })(nextConfig),
);
export default nextConfig;
9 changes: 1 addition & 8 deletions src/app/docs/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export async function generateMetadata({
params: { slug: string[] };
}): Promise<Metadata> {
const result = await getContentBySlug<ArticleFrontMatter>({
dir: "docs",
baseUrlPath: "/docs",
slug: params.slug,
});
if (!result) {
Expand All @@ -30,8 +28,6 @@ export async function generateMetadata({

export default async function Page({ params }: { params: { slug: string[] } }) {
const result = await getContentBySlug<ArticleFrontMatter>({
dir: "docs",
baseUrlPath: "/docs",
slug: params.slug,
});
if (!result) {
Expand All @@ -48,10 +44,7 @@ export default async function Page({ params }: { params: { slug: string[] } }) {
}

export async function generateStaticParams() {
const posts = await getAllContent({
dir: "docs",
baseUrlPath: "/docs",
});
const posts = await getAllContent();
return posts.map((post) => ({
slug: post.slug,
}));
Expand Down
32 changes: 17 additions & 15 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { NavItem, NavSection, navigation } from "@/lib/navigation";
import { type Result } from "@/markdoc/search.mjs";
import {
createAutocomplete,
type AutocompleteApi,
Expand All @@ -23,6 +22,8 @@ import {
} from "react";
import Highlighter from "react-highlight-words";

type Result = any;

type EmptyObject = Record<string, never>;

type Autocomplete = AutocompleteApi<
Expand Down Expand Up @@ -86,20 +87,21 @@ function useAutocomplete({
navigate,
},
getSources({ query }) {
return import("@/markdoc/search.mjs").then(({ search }) => {
return [
{
sourceId: "documentation",
getItems() {
return search(query, { limit: 5 });
},
getItemUrl({ item }) {
return `/docs/${item.url.replace(".md", "")}`;
},
onSelect: navigate,
},
];
});
return [];
// return import("@/markdoc/search.mjs").then(({ search }) => {
// return [
// {
// sourceId: "documentation",
// getItems() {
// return search(query, { limit: 5 });
// },
// getItemUrl({ item }) {
// return `/docs/${item.url.replace(".md", "")}`;
// },
// onSelect: navigate,
// },
// ];
// });
},
}),
);
Expand Down
32 changes: 9 additions & 23 deletions src/lib/content.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync } from "fs";
import fs from "fs/promises";
import { glob } from "glob";
import matter from "gray-matter";
import path from "path";

export interface BaseContent<Data = Record<string, any>> {
source: string;
data: Data;
Expand All @@ -18,18 +18,12 @@ export interface Content<Data = Record<string, any>> {
}

export async function getContentBySlug<Data = Record<string, any>>({
dir,
baseUrlPath,
slug,
}: {
dir: string;
baseUrlPath: string;
slug: string[];
}): Promise<Content<Data> | undefined> {
const fileName = `${slug.join("/")}.md`;
console.log(fileName);
const directory = path.join(process.cwd(), dir);
const filepath = path.join(directory, fileName);
const filepath = path.join(process.cwd(), "docs", fileName);
if (!existsSync(filepath)) {
return undefined;
}
Expand All @@ -41,47 +35,39 @@ export async function getContentBySlug<Data = Record<string, any>>({
const result: Content<Data> = {
source: content,
data: data as Data,
href: `${baseUrlPath}/${slug}`,
href: `/docs/${slug}`,
slug,
};
return result;
}

export async function getAllContent<Data = Record<string, any>>({
dir,
baseUrlPath,
limit,
}: {
dir: string;
baseUrlPath: string;
export async function getAllContent<Data = Record<string, any>>(options?: {
limit?: number;
}): Promise<Content<Data>[]> {
const directory = path.join(process.cwd(), dir);
const contentFiles = await fs.readdir(directory);

const contentFiles = await glob(`docs/**/*.md`);
const results = await Promise.all(
contentFiles
.filter((f) => f.endsWith(".md"))
.sort()
.reverse()
.map(async (file) => {
const filepath = path.join(directory, file);
const filepath = path.join(process.cwd(), file);
const source = await fs.readFile(filepath);

const { data, content } = matter(source);

const result: Content<Data> = {
source: content,
data: data as Data,
href: `${baseUrlPath}/${file}`,
href: file.replace(".md", ""),
slug: file.replace(".md", "").split("/"),
};
return result;
}),
);

if (limit) {
return results.splice(0, limit);
if (options?.limit) {
return results.splice(0, options.limit);
}

return results;
Expand Down

0 comments on commit d6796fd

Please sign in to comment.