Skip to content

Commit 9f2eca9

Browse files
committed
feat:优化阅读全文插件
1 parent 8e8575f commit 9f2eca9

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

docs/.vuepress/components/unlock/GlobalUnlock.vue

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,36 @@ const globalUnlockKey = `javaguide_site_unlocked_${config.unlockVersion ?? "v1"}
8585
const normalizePath = (path: string) =>
8686
path.replace(/\/$/, "").replace(".html", "").toLowerCase();
8787
88+
const isPathInPrefix = (currentPath: string, prefix: string) => {
89+
return currentPath === prefix || currentPath.startsWith(`${prefix}/`);
90+
};
91+
8892
const isLockedPage = computed(() => {
8993
const currentPath = normalizePath(pageData.value.path);
90-
return Object.keys(config.protectedPaths)
94+
const byExactPath = Object.keys(config.protectedPaths)
9195
.map((p) => normalizePath(p))
9296
.includes(currentPath);
97+
if (byExactPath) return true;
98+
99+
const prefixes = Object.keys(config.protectedPrefixes ?? {}).map((p) =>
100+
normalizePath(p),
101+
);
102+
return prefixes.some((prefix) => isPathInPrefix(currentPath, prefix));
93103
});
94104
95105
const visibleHeight = computed(() => {
96106
const currentPath = normalizePath(pageData.value.path);
97-
const matched = Object.keys(config.protectedPaths).find(
107+
const matchedPath = Object.keys(config.protectedPaths).find(
98108
(p) => normalizePath(p) === currentPath,
99109
);
100-
return matched ? config.protectedPaths[matched] : PREVIEW_HEIGHT.LONG;
110+
if (matchedPath) return config.protectedPaths[matchedPath];
111+
112+
const matchedPrefix = Object.keys(config.protectedPrefixes ?? {}).find(
113+
(prefix) => isPathInPrefix(currentPath, normalizePath(prefix)),
114+
);
115+
if (matchedPrefix) return config.protectedPrefixes[matchedPrefix];
116+
117+
return PREVIEW_HEIGHT.LONG;
101118
});
102119
103120
const toPx = (value: string) => {

docs/.vuepress/features/unlock/config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { PREVIEW_HEIGHT } from "./heights";
22

33
const withDefaultHeight = (
44
paths: readonly string[],
5-
height: string = PREVIEW_HEIGHT.XXL,
5+
height: string = PREVIEW_HEIGHT.XL,
66
): Record<string, string> =>
77
Object.fromEntries(paths.map((path) => [path, height]));
88

99
export const unlockConfig = {
1010
// 版本号变更可强制用户重新验证
11-
unlockVersion: "v5",
11+
unlockVersion: "v1",
1212
// 调试用:设为 true 时无视本地已解锁状态,始终触发限制
13-
forceLock: false,
13+
forceLock: true,
1414
code: "8888",
1515
// 使用相对路径,图片放在 docs/.vuepress/public/images 下
1616
qrCodeUrl: "/images/qrcode-javaguide.jpg",
@@ -23,12 +23,15 @@ export const unlockConfig = {
2323
"/cs-basics/network/tcp-connection-and-disconnection.html",
2424
"/cs-basics/network/http-vs-https.html",
2525
"/cs-basics/network/dns.html",
26-
"/database/mysql/mysql-questions-01.html",
27-
"/high-performance/sql-optimization.html",
2826
]),
2927
// 如需特殊高度,再单独覆盖
3028
// "/some/page.html": PREVIEW_HEIGHT.MEDIUM,
3129
},
30+
// 目录前缀 -> 可见高度(该目录下所有文章都触发验证)
31+
// 例如 "/java/collection/" 会匹配 "/java/collection/**"
32+
protectedPrefixes: {
33+
...withDefaultHeight(["/database/", "/high-performance/"]),
34+
},
3235
} as const;
3336

3437
export { PREVIEW_HEIGHT };

0 commit comments

Comments
 (0)