Skip to content

Commit b46bf6a

Browse files
author
sqrtthree
committed
♻️ refactor(config): Refactor configuration helper
Signed-off-by: sqrtthree <[email protected]>
1 parent 8c86f2b commit b46bf6a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Layout.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@ export default {
9797
}
9898
</script>
9999

100-
<style src="prismjs/themes/prism-tomorrow.css"></style>
100+
<style src="prismjs/themes/prism-tomorrow.css">
101+
</style>
101102
<style src="./styles/theme.styl" lang="stylus"></style>

config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const hasValue = target => target !== undefined && target !== null
2+
3+
const getWithPath = (object, path) => {
4+
const p = path.split('.')
5+
const length = p.length
6+
let index = 0
7+
8+
while (hasValue(object) && index < length) {
9+
object = object[p[index++]]
10+
}
11+
12+
return index && index === length ? object : undefined
13+
}
14+
15+
const get = ($site, name, locale) => {
16+
if (locale) {
17+
return (
18+
getWithPath($site, `themeConfig.locales.${locale}.${name}`) ||
19+
getWithPath($site, `themeConfig.${name}`) ||
20+
getWithPath($site, `locales.${locale}.${name}`) ||
21+
$site[name]
22+
)
23+
}
24+
25+
return getWithPath($site, `themeConfig.${name}`) || $site[name]
26+
}
27+
28+
export default {
29+
get,
30+
}

utils.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import config from './config'
2+
13
const isHomePage = (path, base) => {
24
return path === base
35
}
@@ -99,12 +101,7 @@ export function resolveSidebarItems($page, $site, $localePath) {
99101
}
100102
}
101103

102-
const sidebarConfig =
103-
themeConfig.locales &&
104-
themeConfig.locales[$localePath] &&
105-
themeConfig.locales[$localePath].sidebar
106-
? themeConfig.locales[$localePath].sidebar
107-
: themeConfig.sidebar
104+
const sidebarConfig = config.get($site, 'sidebar', $localePath)
108105

109106
if (sidebarConfig) {
110107
return Object.assign(sidebars, sidebarConfig)

0 commit comments

Comments
 (0)