Files
vuepress/packages/@vuepress/core/lib/app/util.js
T
ULIVZ 7f7fe18d40 feat: support clean and multiple custom layout. / theme default layouts dir
1. Refine ExtendPageDataOption, simplify the implmentation
2. Introduce LayoutDistributor as the unique route layout entry
3. Inject layout components to LayoutDistributor
4. Register layout components located at themedir/layouts
5. Reorganize the default theme's structure
2018-09-05 01:02:19 +08:00

90 lines
2.0 KiB
JavaScript

import Vue from 'vue'
import { loadComponent } from '@internal/async-component'
/**
* Inject option to Vue SFC
* @param {object} options
* @param {string} key
* @param {any} value
*/
export function injectComponentOption (options, key, value) {
const arrayInject = () => {
if (!options[key]) options[key] = []
options[key].push(...value)
}
const objectInject = () => {
if (!options[key]) options[key] = {}
Object.assign(options[key], value)
}
// const primitiveInject = () => options[key] = value
switch (key) {
case 'components': objectInject(); break
case 'mixins': arrayInject(); break
default: throw new Error('Unknown option name.')
}
}
export function findPageForPath (pages, path) {
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === path) {
return page
}
}
return {
path: '',
frontmatter: {}
}
}
export function registerComponent (key) {
return loadComponent(key).then(comp => {
Vue.component(key, comp.default)
return comp.default
})
}
/**
* Normalize config.
* This utility is mainly for plugin developers. For some
* plugins that need internationalize the text. but it's
* not recommenbded to let plugin care about to the internal
* i18n implementation, so this utility was born.
*
*
* Usage:
*
* import { normalizeConfig } from '@app/util'
* export default {
* data () {
* return { config }
* }
* computed: {
* normalizedConfig() {
* return normalizeConfig(this, config)
* }
* }
* }
*
*
* e.g.
*
* Config: : 'Text'
* Normalized Config: 'Text'
*
* Config: : { '/': 'Text', '/zh/': '文本' }
* Normalized Config: 'Text' or '文本'
*
* @param {Vue} component
* @param {any} rawConfig
* @returns {any}
*/
export function normalizeConfig (component, rawConfig) {
const { $localePath } = component
if (typeof rawConfig === 'object' && rawConfig[$localePath]) {
return rawConfig[$localePath]
}
return rawConfig
}