@aedart/vuepress-utils
Version:
A few utilities for Vuepress.
137 lines (128 loc) • 3.69 kB
JavaScript
/**
* @aedart/vuepress-utils
*
* BSD-3-Clause, Copyright (c) 2023-present Alin Eugen Deac <aedart@gmail.com>.
*/
;
var vue = require('vue');
/**
* Prefix given path
*
* @param {string} prefix
* @param {string} path
*
* @returns {string}
*/
function prefixPath(prefix, path) {
if (!path.startsWith('/')) {
path = '/' + path;
}
return (prefix + path).replaceAll('//', '/');
}
/**
* Determine if "current" pages collection is being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
*
* @returns {boolean}
*/
function isViewingCurrent(page, archive) {
const current = archive.currentFullPath;
const path = page.value.path;
return path.startsWith(current);
}
/**
* Returns a computed property that determines if "current" pages collection is being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
*
* @returns {import('vue').ComputedRef<boolean>}
*/
function isViewingCurrentRef(page, archive) {
return vue.computed(() => {
return isViewingCurrent(page, archive);
});
}
/**
* Determine if "next" pages collection is being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
*
* @returns {boolean}
*/
function isViewingNext(page, archive) {
const next = archive.nextFullPath;
const path = page.value.path;
return path.startsWith(next);
}
/**
* Returns a computed property that determines if "next" pages collection is being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
*
* @returns {import('vue').ComputedRef<boolean>}
*/
function isViewingNextRef(page, archive) {
return vue.computed(() => {
return isViewingNext(page, archive);
});
}
/**
* Determine if neither "current" nor "next" pages collection are being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
* @param {string[]} [exclude=[ '/' ]] Paths to exclude from result
*
* @returns {boolean}
*/
function isViewingOther(page, archive, exclude = ['/']) {
const path = page.value.path;
return !exclude.includes(path)
&& path !== archive.path + '/'
&& !isViewingNext(page, archive)
&& !isViewingCurrent(page, archive);
}
/**
* Returns a computed property that determines if neither "current" nor "next" pages
* collection are being viewed
*
* @param {import('@vuepress/client').PageDataRef} page
* @param {Archive} archive
* @param {string[]} [exclude=[ '/' ]] Paths to exclude from result
*
* @returns {import('vue').ComputedRef<boolean>}
*/
function isViewingOtherRef(page, archive, exclude = ['/']) {
return vue.computed(() => {
return isViewingOther(page, archive, exclude);
});
}
/**
* Resolves the base URL of vuepress site, based on {@link process.env.NODE_ENV}
*
* @param {string} path
* @param {string} [productionEnv='production'] Name of a production environment
*
* @returns {"/" | `/${string}/`} '/' when not in production
*/
function baseURL(path, productionEnv = 'production') {
if (process.env.NODE_ENV !== productionEnv) {
return '/';
}
return `/${path}/`;
}
exports.baseURL = baseURL;
exports.isViewingCurrent = isViewingCurrent;
exports.isViewingCurrentRef = isViewingCurrentRef;
exports.isViewingNext = isViewingNext;
exports.isViewingNextRef = isViewingNextRef;
exports.isViewingOther = isViewingOther;
exports.isViewingOtherRef = isViewingOtherRef;
exports.prefixPath = prefixPath;
module.exports = Object.assign(exports.default, exports);
//# sourceMappingURL=vuepress-utils.cjs.map