import "dotenv/config"; import { fileURLToPath, URL } from "node:url"; import { defineConfig } from "vitepress"; import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"; import Icons from "unplugin-icons/vite"; import { FileSystemIconLoader } from "unplugin-icons/loaders"; import { SVG, cleanupSVG, parseColors, runSVGO } from "@iconify/tools"; import { S3Client, ListObjectsCommand, GetObjectCommand, } from "@aws-sdk/client-s3"; import fs from "node:fs"; import path from "node:path"; import { exec } from "child_process"; import { buildSideBar } from "../utils/sideBar"; const DOC_BASE_PATH = "pages"; if (process.env.NODE_ENV === "production") { // 执行清理pages命令 await new Promise((resolve, reject) => exec( `git checkout -- ${DOC_BASE_PATH} & git clean -df ${DOC_BASE_PATH}`, (error, stdout, stderr) => { if (error) { console.error(`清理pages时出错: ${error.message}`); reject(stderr); } console.log(`清理pages结果: ${stdout}`); resolve(stdout); } ) ); } // 从S3构建pages里面的markdown文件 const s3 = new S3Client({ credentials: { accessKeyId: process.env.VITE_AWS_S3_ACCESS_KEY_ID!, secretAccessKey: process.env.VITE_AWS_S3_SECRET_ACCESS_KEY!, }, region: process.env.VITE_AWS_S3_REGION!, }); const command = new ListObjectsCommand({ Bucket: process.env.VITE_DOCS_LIST_BUCKET, }); const { Contents: contents } = await s3.send(command); await Promise.all( contents!.map((content) => { return new Promise(async (resolve, reject) => { try { const command = new GetObjectCommand({ Bucket: process.env.VITE_DOCS_LIST_BUCKET, Key: content.Key, ResponseCacheControl: "no-cache", }); const file = await s3.send(command); const writePath = path.join(DOC_BASE_PATH, content.Key!); const directory = path.dirname(writePath); fs.mkdirSync(directory, { recursive: true }); fs.writeFileSync(writePath, await file.Body!.transformToString()); resolve(content.Key); } catch (e) { reject(e); } fs.writeFile; }); }) ); // 构建sideBar数据 let { rootSideBar, zhHKSideBar } = buildSideBar(contents); import util from "util"; console.log( util.inspect(rootSideBar, { showHidden: false, depth: null, colors: true }), util.inspect(zhHKSideBar, { showHidden: false, depth: null, colors: true }) ); // process.exit(); // https://vitepress.dev/reference/site-config export default defineConfig({ title: "CocoBlockly帮助文档", description: "Cococlass help documents", srcDir: DOC_BASE_PATH, // themeConfig: { // search: { // provider: "local", // }, // }, appearance: false, vite: { publicDir: "../public", envDir: "../", // optimizeDeps: { // include: ["vue-i18n"], // }, ssr: { // SSG Vue-i18n workaround noExternal: [/vue-i18n/], }, plugins: [ VueI18nPlugin({ /* options */ // locale messages resource pre-compile option // include: resolve(dirname(fileURLToPath(import.meta.url)), './path/to/src/locales/**'), }), Icons({ compiler: "vue3", customCollections: { "ccrbi-plain": FileSystemIconLoader( "assets/icons/plain", async (svgStr) => { const svg = new SVG(svgStr); cleanupSVG(svg); parseColors(svg, { defaultColor: "currentColor", callback: (attr, colorStr, color) => { // console.log('Color:', colorStr, color); // 普通图标 return "currentColor"; // Change black to 'currentColor' // const blackColor = stringToColor("black"); // if (color && compareColors(color, blackColor!)) { // return "currentColor"; // } // switch (color?.type) { // case "none": // case "current": // return color; // } // throw new Error( // `Unexpected color "${colorStr}" in attribute ${attr}` // ); }, }); // Optimise, but do not change shapes because they are animated runSVGO(svg, { keepShapes: true, }); return svg.toMinifiedString({}); } ), "ccrbi-colored": FileSystemIconLoader( "assets/icons/colored", async (svgStr) => { const svg = new SVG(svgStr); cleanupSVG(svg); // Optimise, but do not change shapes because they are animated runSVGO(svg, { keepShapes: true, }); return svg.toMinifiedString({}); } ), }, }), ], resolve: { alias: [ { find: /^.*\/VPNavBar\.vue$/, replacement: fileURLToPath( new URL("../components/CustomNavBar.vue", import.meta.url) ), }, { find: "@/", replacement: fileURLToPath(new URL("../", import.meta.url)), }, ], }, }, locales: { root: { label: "简体中文", lang: "zh-CN", themeConfig: { logo: "/logo.png", siteTitle: false, // https://vitepress.dev/reference/default-theme-config // nav: [ // { text: 'Home', link: '/' }, // { text: 'Examples', link: '/markdown-examples' } // ], sidebar: [ { text: "关于CocoBlockly X", link: "/docs" }, { text: "常见问题解答", link: "/docs/常见问题解答" }, { text: "开始使用CocoBlockly X", link: "/docs/start-using-cocoblockly-x", collapsed: true, items: [], }, { text: "电子模块基本教学", collapsed: true, items: [{ text: "todo", link: "/docs" }], }, ], // sidebar: rootSideBar, // socialLinks: [ // { icon: 'github', link: 'https://github.com/vuejs/vitepress' } // ] }, }, "zh-HK": { label: "繁体中文", lang: "zh-HK", themeConfig: { logo: "/logo.png", siteTitle: false, // https://vitepress.dev/reference/default-theme-config // nav: [ // { text: 'Home', link: '/' }, // { text: 'Examples', link: '/markdown-examples' } // ], sidebar: [ { text: "什麽是CocoBlockly X", link: "/zh-HK/docs" }, { text: "常見問題解答", link: "/zh-HK/docs/faq" }, { text: "開始使用CocoBlockly X", link: "/zh-HK/docs/start-using-cocoblockly-x", }, { text: "電子模組基本教學", collapsed: true, items: [{ text: "todo", link: "/zh-HK/docs" }], }, ], // socialLinks: [ // { icon: 'github', link: 'https://github.com/vuejs/vitepress' } // ] }, }, }, });