Skip to content

Commit 689c776

Browse files
committed
chore: 兼容cnb访问
1 parent 63c4b34 commit 689c776

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/cherry-markdown/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@
7777

7878
let CONFIG ={};
7979

80-
switch (window.location.pathname) {
80+
// 使用注入的原始路径或当前路径
81+
const currentPath = window.__ORIGINAL_PATH__ || window.location.pathname;
82+
83+
switch (currentPath) {
8184
case '/':
8285
CONFIG = Object.assign({}, basicConfig, { value: indexMd, ...devCompatibleConfig });
8386
break;

packages/cherry-markdown/vite.config.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ function printLinks() {
4040
};
4141
}
4242

43+
function spaFallback() {
44+
return {
45+
name: 'spa-fallback',
46+
transformIndexHtml: (html: string, ctx: { path: string; filename?: string; server?: any }) => {
47+
const path = ctx.path;
48+
// 如果是配置的路径之一,注入脚本设置原始路径
49+
// 优化的路径匹配逻辑
50+
const isExactMatch = paths.includes(path);
51+
const isPathWithoutHtmlMatch = path.endsWith('.html') && paths.includes(path.slice(0, -5));
52+
const matchedPath = isExactMatch || isPathWithoutHtmlMatch ? (isExactMatch ? path : path.slice(0, -5)) : null;
53+
54+
if (matchedPath && matchedPath !== '/index.html') {
55+
// 转义路径中的特殊字符以防止XSS注入
56+
const escapedPath = matchedPath.replace(/['"\\]/g, '\\$&');
57+
const script = `<script>window.__ORIGINAL_PATH__ = '${escapedPath}';</script>`;
58+
return html.replace('</head>', `${script}\n </head>`);
59+
}
60+
return html;
61+
},
62+
};
63+
}
64+
4365
export default defineConfig({
4466
root: process.cwd(),
4567
base: '/',
@@ -69,5 +91,5 @@ export default defineConfig({
6991
BUILD_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
7092
__EXAMPLES_PATH__: JSON.stringify(path.resolve(__dirname, '../../examples').replace(/\\/g, '/')),
7193
},
72-
plugins: [printLinks()],
94+
plugins: [spaFallback(), printLinks()],
7395
});

0 commit comments

Comments
 (0)