UNPKG

vuepress-theme-hope

Version:

A light vuepress theme with tons of features

59 lines 2.61 kB
import { isPlainObject, keys, startsWith } from "@vuepress/helper/client"; import { useSessionStorage, useStorage } from "@vueuse/core"; import { computed } from "vue"; import { usePage } from "vuepress/client"; import { useEncryptConfig } from "@theme-hope/composables/encrypt/useEncryptConfig"; import { isTokenMatched } from "@theme-hope/utils/encrypt/isTokenMatched"; const STORAGE_KEY = "VUEPRESS_HOPE_PATH_TOKEN"; export const usePathEncrypt = () => { const page = usePage(); const encryptData = useEncryptConfig(); const localTokenConfig = useStorage(STORAGE_KEY, {}); const sessionTokenConfig = useSessionStorage(STORAGE_KEY, {}); const getPathMatchedKeys = (path) => isPlainObject(encryptData.value.config) ? keys(encryptData.value.config) .filter((key) => startsWith(decodeURI(path), key)) .sort((a, b) => b.length - a.length) : []; const getStatus = (path) => { const { config = {} } = encryptData.value; const matchedKeys = getPathMatchedKeys(path); if (matchedKeys.length > 0) { const firstKeyWithHint = matchedKeys.find((key) => config[key].hint); return { isEncrypted: true, isLocked: matchedKeys.some((key) => (localTokenConfig.value[key] ? config[key].tokens.every((token) => !isTokenMatched(token, localTokenConfig.value[key])) : true) && (sessionTokenConfig.value[key] ? config[key].tokens.every((token) => !isTokenMatched(token, sessionTokenConfig.value[key])) : true)), // oxlint-disable-next-line typescript/no-non-null-assertion hint: firstKeyWithHint ? config[firstKeyWithHint].hint : "", }; } return { isEncrypted: false, isLocked: false, hint: "", }; }; const status = computed(() => getStatus(page.value.path)); const validate = (inputToken, keep = false) => { const { config = {} } = encryptData.value; const matchedKeys = getPathMatchedKeys(page.value.path); // Some of the tokens matches for (const hitKey of matchedKeys) { if (config[hitKey].tokens.some((token) => isTokenMatched(token, inputToken))) { (keep ? localTokenConfig : sessionTokenConfig).value[hitKey] = inputToken; break; } } }; return { status, getStatus, validate, }; }; //# sourceMappingURL=usePathEncrypt.js.map