From f4a0c8d883871795508510627e8ecbf434100a05 Mon Sep 17 00:00:00 2001 From: polv Date: Tue, 28 Jul 2026 13:53:21 +0700 Subject: [PATCH] build with redirected images --- .gitignore | 3 +- .vscode/settings.json | 4 +++ docs/.vitepress/config.mts | 72 ++++++++++++++++++++++++++++---------- postvite-build.js | 1 + 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index b512c09d..37d7e734 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +.env diff --git a/.vscode/settings.json b/.vscode/settings.json index b91095ac..88cc734c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,10 @@ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.maxTokenizationLineLength": 2500, }, + "[typescript]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", + }, "[vue]": { "editor.formatOnSave": true, "editor.defaultFormatter": "esbenp.prettier-vscode", diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 494debcf..86cce7bb 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -1,35 +1,71 @@ -import { defineConfig } from 'vitepress' +import { defineConfig } from "vitepress"; +import { execSync } from "node:child_process"; const host = process.env.TAURI_DEV_HOST; +const { + IMG_ORIGIN = `https://git.polv.cc/polv/yt2press/raw/branch/${ + execSync("git branch --show-current").toString().trimEnd() + }/docs/public`, +} = process.env; + // https://vitepress.dev/reference/site-config export default defineConfig({ title: "yt2press", description: "video to vitepress", themeConfig: { // https://vitepress.dev/reference/default-theme-config - nav: [ - { text: 'Home', link: '/' }, - ], + nav: [{ text: "Home", link: "/" }], sidebar: [ { - text: 'Preview', + text: "Preview", // items: [ // { text: 'Markdown Examples', link: '/markdown-examples' }, // { text: 'Runtime API Examples', link: '/api-examples' } // ] - } + }, ], socialLinks: [ - { icon: 'github', link: 'https://git.polv.cc/polv/yt2press' } - ] + { icon: "github", link: "https://git.polv.cc/polv/yt2press" }, + ], }, markdown: { image: { lazyLoading: true, }, + config(md) { + // production-only gate + const isProd = + process.env.NODE_ENV === "production" || + process.env.VITEPRESS_ENV === "production"; // extra safety + + if (isProd) { + md.core.ruler.push("rewrite-image-urls", (state) => { + if (!state.tokens) return; + + for (const token of state.tokens) { + if (token.type !== "inline") continue; + + for (const child of token.children ?? []) { + // images: ![alt](src) + if ( + child.type === "image" && + typeof child.attrGet("src") === "string" + ) { + const src = child.attrGet("src"); + + // rewrite only /images/* (adjust if your paths differ) + if (src && src.startsWith("/images/")) { + child.attrSet("src", `${IMG_ORIGIN}${src}`); + } + } + } + } + }); + } + }, }, vite: { clearScreen: false, @@ -42,28 +78,26 @@ export default defineConfig({ host: host || false, watch: { // tell vite to ignore watching `src-tauri` - ignored: ['**/src-tauri/**'], + ignored: ["**/src-tauri/**"], }, proxy: { - '/socket': { - target: 'ws://localhost:8080', + "/socket": { + target: "ws://localhost:8080", ws: true, rewriteWsOrigin: true, - } + }, }, }, // Env variables starting with the item of `envPrefix` will be exposed in tauri's source code through `import.meta.env`. - envPrefix: ['VITE_', 'TAURI_ENV_*'], + envPrefix: ["VITE_", "TAURI_ENV_*"], build: { // Tauri uses Chromium on Windows and WebKit on macOS and Linux target: - process.env.TAURI_ENV_PLATFORM == 'windows' - ? 'chrome105' - : 'safari13', + process.env.TAURI_ENV_PLATFORM == "windows" ? "chrome105" : "safari13", // don't minify for debug builds - minify: !process.env.TAURI_ENV_DEBUG ? 'esbuild' : false, + minify: !process.env.TAURI_ENV_DEBUG ? "esbuild" : false, // produce sourcemaps for debug builds sourcemap: !!process.env.TAURI_ENV_DEBUG, }, - } -}) + }, +}); diff --git a/postvite-build.js b/postvite-build.js index 20bd8bbb..b7e6312e 100644 --- a/postvite-build.js +++ b/postvite-build.js @@ -14,3 +14,4 @@ async function removeAllVideos(dir) { } await removeAllVideos("docs/.vitepress/dist"); +await fs.rm("docs/.vitepress/dist/images", { recursive: true });