vuepress-theme-medium
Version:
Medium-Like blog theme for VuePress
58 lines (50 loc) • 1.25 kB
JavaScript
export const hashRE = /#.*$/
export const extRE = /\.(md|html)$/
export const endingSlashRE = /\/$/
export const outboundRE = /^(https?:|mailto:|tel:)/
export function normalize (path) {
return decodeURI(path)
.replace(hashRE, '')
.replace(extRE, '')
}
export function isExternal (path) {
return outboundRE.test(path)
}
export function isMailto (path) {
return /^mailto:/.test(path)
}
export function isTel (path) {
return /^tel:/.test(path)
}
export function ensureExt (path) {
if (isExternal(path)) {
return path
}
const hashMatch = path.match(hashRE)
const hash = hashMatch ? hashMatch[0] : ''
const normalized = normalize(path)
if (endingSlashRE.test(normalized)) {
return path
}
return normalized + '.html' + hash
}
/*
* find parent vm by ref
* @param {String} ref
* @param {Vue} vm
* @param {any} def default value
* @returns {Element}
*/
export function findContainerInVm(ref, vm, def) {
if (!ref) return def;
let container;
let parent = vm;
while ((parent = parent.$parent) && !container) {
container = parent.$refs[ref];
}
// Ensure it's html element (ref could be component)
if (container && container.$el) {
container = container.$el;
}
return container || def;
}