Compare commits
2 Commits
e10d447873
...
48973560d1
Author | SHA1 | Date | |
---|---|---|---|
48973560d1 | |||
d022209305 |
@ -34,7 +34,7 @@ const DocsPage = async ({
|
||||
const decodedSlug: string = decodeURIComponent(slug || "");
|
||||
const page: DocsContentMetadata | undefined = pages.find(
|
||||
(metadata: DocsContentMetadata): boolean =>
|
||||
metadata.slug === (decodedSlug || "intro")
|
||||
metadata.slug === (decodedSlug || pages[0].slug)
|
||||
);
|
||||
if (!page) {
|
||||
notFound();
|
||||
|
@ -17,7 +17,8 @@ const DocsFooter = ({
|
||||
|
||||
const current: number = pages.findIndex(
|
||||
(page: DocsContentMetadata) =>
|
||||
(path === "/" && page.slug === "intro") || path === `/${page.slug}`
|
||||
(path === "/" && page.slug === pages[0].slug) ||
|
||||
path === `/${page.slug}`
|
||||
);
|
||||
const previous: DocsContentMetadata | undefined =
|
||||
current > 0 ? pages[current - 1] : undefined;
|
||||
|
@ -22,7 +22,7 @@ const SidebarLinks = ({
|
||||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
{Object.values(tree).map((node: TreeNode) => (
|
||||
<CategoryItem key={node.slug} node={node} />
|
||||
<CategoryItem key={node.slug} pages={pages} node={node} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
@ -36,17 +36,20 @@ type TreeNode = {
|
||||
};
|
||||
|
||||
const CategoryItem = ({
|
||||
pages,
|
||||
node,
|
||||
depth = 0,
|
||||
isLast = true,
|
||||
}: {
|
||||
pages: DocsContentMetadata[];
|
||||
node: TreeNode;
|
||||
depth?: number;
|
||||
isLast?: boolean;
|
||||
}) => {
|
||||
const path = decodeURIComponent(usePathname());
|
||||
const active =
|
||||
(path === "/" && node.slug === "intro") || path === `/${node.slug}`;
|
||||
(path === "/" && node.slug === pages[0].slug) ||
|
||||
path === `/${node.slug}`;
|
||||
const [isOpen, setIsOpen] = useState(true);
|
||||
const hasChildren = Object.keys(node.children).length > 0;
|
||||
|
||||
@ -121,6 +124,7 @@ const CategoryItem = ({
|
||||
(child, index, array) => (
|
||||
<CategoryItem
|
||||
key={child.slug}
|
||||
pages={pages}
|
||||
node={child}
|
||||
depth={depth + 1}
|
||||
isLast={index === array.length - 1}
|
||||
|
@ -24,6 +24,13 @@ const DOCS_DIR: string = isGitUrl(config.contentSource)
|
||||
? config.contentSource
|
||||
: path.join(config.contentSource.replace("{process}", process.cwd()));
|
||||
|
||||
const LAST_UPDATE_FILE = path.join(
|
||||
os.tmpdir(),
|
||||
"docs_cache",
|
||||
"last_update.json"
|
||||
);
|
||||
const UPDATE_INTERVAL_MS: number = 10 * 60 * 1000; // 10 minutes in milliseconds
|
||||
|
||||
/**
|
||||
* Clone the Git repository if DOCS_DIR is a URL, else use the local directory.
|
||||
* If it's a Git URL, clone it to a cache directory and reuse it.
|
||||
@ -35,15 +42,40 @@ const getDocsDirectory = async (): Promise<string> => {
|
||||
|
||||
// Pull the latest changes from the repo if we don't have it
|
||||
if (!fs.existsSync(cacheDir) || fs.readdirSync(cacheDir).length < 1) {
|
||||
console.log("Fetching initial docs from Git...");
|
||||
try {
|
||||
await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 });
|
||||
} catch (error) {}
|
||||
} catch (error) {
|
||||
// Simply ignore this error. When cloning the repo for
|
||||
// the first time, it'll sometimes error saying the dir
|
||||
// is already created.
|
||||
}
|
||||
} else if (shouldUpdateRepo()) {
|
||||
// Pull the latest changes from Git
|
||||
console.log("Updating docs content from Git...");
|
||||
await simpleGit().pull(cacheDir);
|
||||
fs.writeFileSync(
|
||||
LAST_UPDATE_FILE,
|
||||
JSON.stringify({ lastUpdate: Date.now() }),
|
||||
"utf-8"
|
||||
);
|
||||
}
|
||||
return cacheDir;
|
||||
}
|
||||
return DOCS_DIR;
|
||||
};
|
||||
|
||||
const shouldUpdateRepo = (): boolean => {
|
||||
if (!fs.existsSync(LAST_UPDATE_FILE)) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
Date.now() -
|
||||
JSON.parse(fs.readFileSync(LAST_UPDATE_FILE, "utf-8")).lastUpdate >
|
||||
UPDATE_INTERVAL_MS
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the content to display in the docs.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user