|
| 1 | +import {defineConfig, DefaultTheme} from "vitepress"; |
| 2 | +import path from "path"; |
| 3 | +import fs from "fs-extra"; |
| 4 | +import {fileURLToPath} from "url"; |
| 5 | +import typedocSidebar from "../docs/api/typedoc-sidebar.json"; // if this import fails, run `npm run docs:generateTypedoc` |
| 6 | +import envVar from "env-var"; |
| 7 | +import process from "process"; |
| 8 | + |
| 9 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const packageJson: typeof import("../package.json") = fs.readJsonSync(path.join(__dirname, "..", "package.json")); |
| 11 | +const env = envVar.from(process.env); |
| 12 | + |
| 13 | +const urlBase = env.get("DOCS_URL_BASE").asString(); |
| 14 | +const packageVersion = env.get("DOCS_PACKAGE_VERSION").default(packageJson.version).asString(); |
| 15 | + |
| 16 | +const chatWrappersOrder = [ |
| 17 | + "GeneralChatPromptWrapper", |
| 18 | + "LlamaChatPromptWrapper", |
| 19 | + "ChatMLChatPromptWrapper", |
| 20 | + "FalconChatPromptWrapper" |
| 21 | +] as const; |
| 22 | + |
| 23 | +function resolveHref(href: string) { |
| 24 | + if (urlBase == null) |
| 25 | + return href; |
| 26 | + |
| 27 | + if (urlBase.endsWith("/") && href.startsWith("/")) |
| 28 | + return urlBase.slice(0, -1) + href; |
| 29 | + |
| 30 | + return urlBase + href; |
| 31 | +} |
| 32 | + |
| 33 | +export default defineConfig({ |
| 34 | + title: "node-llama-cpp", |
| 35 | + description: "Run AI models locally on your machine with node.js bindings for llama.cpp", |
| 36 | + |
| 37 | + srcDir: "./docs", |
| 38 | + outDir: "./docs-site", |
| 39 | + cacheDir: "./.vitepress/.cache", |
| 40 | + |
| 41 | + cleanUrls: true, |
| 42 | + lastUpdated: true, |
| 43 | + |
| 44 | + base: urlBase, |
| 45 | + sitemap: { |
| 46 | + hostname: "https://withcatai.github.io/node-llama-cpp/" |
| 47 | + }, |
| 48 | + head: [ |
| 49 | + ["link", {rel: "icon", type: "image/svg+xml", href: resolveHref("/favicon.svg")}], |
| 50 | + ["link", {rel: "icon", type: "image/png", href: resolveHref("/favicon.png")}], |
| 51 | + ["meta", {name: "theme-color", content: "#cd8156"}], |
| 52 | + ["meta", {name: "theme-color", content: "#dd773e", media: "(prefers-color-scheme: dark)"}], |
| 53 | + ["meta", {name: "og:type", content: "website"}], |
| 54 | + ["meta", {name: "og:locale", content: "en"}], |
| 55 | + ["meta", {name: "og:site_name", content: "node-llama-cpp"}] |
| 56 | + ], |
| 57 | + transformPageData(pageData) { |
| 58 | + if (pageData.filePath.startsWith("api/") || pageData.filePath.startsWith("guide/cli/")) { |
| 59 | + pageData.frontmatter.editLink = false; |
| 60 | + pageData.frontmatter.lastUpdated = false; |
| 61 | + } |
| 62 | + }, |
| 63 | + themeConfig: { |
| 64 | + editLink: { |
| 65 | + pattern: "https://github.com/withcatai/node-llama-cpp/edit/main/docs/:path" |
| 66 | + }, |
| 67 | + nav: [ |
| 68 | + {text: "Guide", link: "/guide/", activeMatch: "/guide/"}, |
| 69 | + {text: "API Reference", link: "/api/classes/LlamaModel", activeMatch: "/api/"}, |
| 70 | + { |
| 71 | + text: packageVersion, |
| 72 | + items: [{ |
| 73 | + text: "Changelog", |
| 74 | + link: "https://github.com/withcatai/node-llama-cpp/releases" |
| 75 | + }, { |
| 76 | + text: "npm", |
| 77 | + link: "https://www.npmjs.com/package/node-llama-cpp" |
| 78 | + }, { |
| 79 | + text: "Contributing", |
| 80 | + link: "/guide/contributing" |
| 81 | + }] |
| 82 | + } |
| 83 | + ], |
| 84 | + search: { |
| 85 | + provider: "local" |
| 86 | + }, |
| 87 | + sidebar: { |
| 88 | + "/api/": orderApiReferenceSidebar(getApiReferenceSidebar()), |
| 89 | + |
| 90 | + "/guide/": [{ |
| 91 | + text: "Guide", |
| 92 | + base: "/guide", |
| 93 | + items: [ |
| 94 | + {text: "Getting started", link: "/"}, |
| 95 | + {text: "Chat session", link: "/chat-session"}, |
| 96 | + {text: "Chat prompt wrapper", link: "/chat-prompt-wrapper"}, |
| 97 | + {text: "Using grammar", link: "/grammar"} |
| 98 | + ] |
| 99 | + }, { |
| 100 | + text: "Advanced", |
| 101 | + base: "/guide", |
| 102 | + items: [ |
| 103 | + {text: "Building from source", link: "/building-from-source"}, |
| 104 | + {text: "Metal support", link: "/Metal"}, |
| 105 | + {text: "CUDA support", link: "/CUDA"} |
| 106 | + ] |
| 107 | + }, { |
| 108 | + text: "Contributing", |
| 109 | + base: "/guide", |
| 110 | + items: [ |
| 111 | + {text: "Setting up a dev environment", link: "/development"}, |
| 112 | + {text: "Pull request guidelines", link: "/contributing"} |
| 113 | + ] |
| 114 | + }, { |
| 115 | + text: "CLI", |
| 116 | + base: "/guide/cli", |
| 117 | + collapsed: true, |
| 118 | + link: "/", |
| 119 | + items: [ |
| 120 | + {text: "Chat", link: "/chat"}, |
| 121 | + {text: "Download", link: "/download"}, |
| 122 | + {text: "Build", link: "/build"}, |
| 123 | + {text: "Clear", link: "/clear"} |
| 124 | + ] |
| 125 | + }] |
| 126 | + }, |
| 127 | + socialLinks: [ |
| 128 | + {icon: "github", link: "https://github.com/withcatai/node-llama-cpp"} |
| 129 | + ] |
| 130 | + } |
| 131 | +}); |
| 132 | + |
| 133 | +function getApiReferenceSidebar(): typeof typedocSidebar { |
| 134 | + return structuredClone(typedocSidebar) |
| 135 | + .map((item) => { |
| 136 | + switch (item.text) { |
| 137 | + case "README": |
| 138 | + case "API": |
| 139 | + return null; |
| 140 | + case "Classes": |
| 141 | + case "Type Aliases": |
| 142 | + case "Functions": |
| 143 | + if (item.text === "Type Aliases") |
| 144 | + item.text = "Types"; |
| 145 | + |
| 146 | + if (item.collapsed) |
| 147 | + item.collapsed = false; |
| 148 | + |
| 149 | + if (item.items instanceof Array) |
| 150 | + item.items = item.items.map((subItem) => { |
| 151 | + if (subItem.collapsed) |
| 152 | + // @ts-ignore |
| 153 | + delete subItem.collapsed; |
| 154 | + |
| 155 | + return subItem; |
| 156 | + }) |
| 157 | + return item; |
| 158 | + } |
| 159 | + |
| 160 | + return item; |
| 161 | + }) |
| 162 | + .filter((item) => item != null) as typeof typedocSidebar; |
| 163 | +} |
| 164 | + |
| 165 | +function orderApiReferenceSidebar(sidebar: typeof typedocSidebar): typeof typedocSidebar { |
| 166 | + const baseChatPromptWrapper = "ChatPromptWrapper"; |
| 167 | + const chatPromptWrapperItems: DefaultTheme.SidebarItem[] = []; |
| 168 | + |
| 169 | + const classes = sidebar.find((item) => item.text === "Classes"); |
| 170 | + |
| 171 | + if (classes == null || !(classes.items instanceof Array)) |
| 172 | + return sidebar; |
| 173 | + |
| 174 | + (classes.items as DefaultTheme.SidebarItem[]).unshift({ |
| 175 | + text: "Chat wrappers", |
| 176 | + collapsed: false, |
| 177 | + items: chatPromptWrapperItems |
| 178 | + }); |
| 179 | + |
| 180 | + const chatPromptWrapper = classes.items.find((item) => item.text === baseChatPromptWrapper); |
| 181 | + if (chatPromptWrapper != null) { |
| 182 | + classes.items.splice(classes.items.indexOf(chatPromptWrapper), 1); |
| 183 | + classes.items.unshift(chatPromptWrapper); |
| 184 | + } |
| 185 | + |
| 186 | + for (const item of classes.items.slice()) { |
| 187 | + if (item.text === baseChatPromptWrapper || !item.text.endsWith(baseChatPromptWrapper)) |
| 188 | + continue; |
| 189 | + |
| 190 | + classes.items.splice(classes.items.indexOf(item), 1); |
| 191 | + chatPromptWrapperItems.push(item); |
| 192 | + } |
| 193 | + |
| 194 | + chatPromptWrapperItems.sort((a, b) => { |
| 195 | + const aIndex = chatWrappersOrder.indexOf(a.text as typeof chatWrappersOrder[number]); |
| 196 | + const bIndex = chatWrappersOrder.indexOf(b.text as typeof chatWrappersOrder[number]); |
| 197 | + |
| 198 | + if (aIndex < 0 && bIndex < 0) |
| 199 | + return 0; |
| 200 | + if (aIndex < 0) |
| 201 | + return 1; |
| 202 | + if (bIndex < 0) |
| 203 | + return -1; |
| 204 | + |
| 205 | + return aIndex - bIndex; |
| 206 | + }); |
| 207 | + |
| 208 | + return sidebar; |
| 209 | +} |
| 210 | + |
0 commit comments