Skip to content

Commit b1d2d26

Browse files
committed
Merge branch 'master' into algolia-docsearch
2 parents 55826c5 + d026801 commit b1d2d26

File tree

10 files changed

+145
-15
lines changed

10 files changed

+145
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ https://vuepress.vuejs.org/
1717
- [Vue-powered custom theme system](https://vuepress.vuejs.org/guide/custom-themes.html)
1818
- Service Worker Integration
1919
- Google Analytics Integration
20+
- Multi-Language Support
2021
- A default theme with:
2122
- Responsive layout
2223
- Optional Homepage
@@ -28,7 +29,6 @@ https://vuepress.vuejs.org/
2829

2930
VuePress is still a work in progress. There are a few things that it currently does not support but are planned:
3031

31-
- Multi-Language Support
3232
- Algolia DocSearch Integration
3333
- Blogging support
3434

docs/guide/custom-themes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ The compiled content of the current `.md` file being rendered will be available
6666
</template>
6767
```
6868

69+
## Theme Level Enhancements
70+
71+
Themes can extend the Vue app that VuePress uses by exposing an `index.js` file at the root of the theme. The file should `export default` a hook function which will receive an object containing some app level values. You can use this hook to install additional Vue plugins, register global components, or add additional router hooks:
72+
73+
``` js
74+
export default ({
75+
Vue, // the version of Vue being used in the VuePress app
76+
options, // the options for the root Vue instance
77+
router // the router instance for the app
78+
}) => {
79+
// ...apply enhancements to the app
80+
}
81+
```
82+
6983
## Using Theme from a Dependency
7084

7185
Themes can be published on npm in raw Vue SFC format as `vuepress-theme-xxx`.

docs/guide/using-vue.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ Inside any markdown file you can then directly use the components (names are inf
121121
Make sure a custom component's name either contains a hyphen or is in PascalCase. Otherwise it will be treated as an inline element and wrapped inside a `<p>` tag, which will lead to hydration mismatch because `<p>` does not allow block elements to be placed inside it.
122122
:::
123123

124+
### Using Pre-processors
125+
126+
VuePress has built-in webpack config for the following pre-processors: `sass`, `scss`, `less`, `stylus` and `pug`. All you need to do is installing the corresposnding dependencies. For example, to enable `sass`, install the following in your project:
127+
128+
``` bash
129+
yarn add -D sass-loader node-sass
130+
```
131+
132+
Now you can use the following in markdown and theme components:
133+
134+
``` vue
135+
<style lang="sass">
136+
.title
137+
font-size: 20px
138+
</style>
139+
```
140+
141+
Using `<template lang="pug">` requires installing `pug` and `pug-plain-loader`:
142+
143+
``` bash
144+
yarn add -D pug pug-plain-loader
145+
```
146+
147+
::: tip
148+
If you are a Stylus user, you don't need to install `stylus` and `stylus-loader` in your project because VuePress uses Stylus internally.
149+
150+
For pre-processors that do not have built-in webpack config support, you will need to [extend the internal webpack config](../config/#configurewebpack) in addition to installing the necessary dependencies.
151+
:::
152+
124153
## Script & Style Hoisting
125154

126155
Sometimes you may need to apply some JavaScript or CSS only to the current page. In those cases you can directly write root-level `<script>` or `<style>` blocks in the markdown file, and they will be hoisted out of the compiled HTML and used as the `<script>` and `<style>` blocks for the resulting Vue single-file component.

docs/zh/guide/using-vue.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ export default {
121121
请确保一个自定义组件的名字包含连接符或者是 PascalCase,否则,它将会被视为一个内联元素,并被包裹在一个 `<p>` 标签中,这将会导致 HTML 渲染紊乱,因为 HTML 标准规定, `<p>` 标签中不允许放置任何块级元素。
122122
:::
123123

124+
125+
## 使用预处理器
126+
127+
VuePress 对以下预处理器已经内置相关的 webpack 配置:`sass``scss``less``stylus``pug`。要使用它们你只需要在项目中安装对应的依赖即可。例如,要使用 `sass`,需要安装:
128+
129+
```bash
130+
yarn add -D sass-loader node-sass
131+
```
132+
133+
然后你就可以在 Markdown 或是组件中使用如下代码:
134+
135+
```vue
136+
<style lang="sass">
137+
.title
138+
font-size: 20px
139+
</style>
140+
```
141+
142+
要在组件中使用 `<template lang="pug">`,则需要安装 `pug``pug-plain-loader`:
143+
144+
```bash
145+
yarn add -D pug pug-plain-loader
146+
```
147+
148+
::: tip
149+
需要指出的是,如果你是一个 `stylus` 用户,你并不需要在你的项目中安装 `stylus``stylus-loader`,因为 VuePress 已经内置了它们。
150+
151+
对于那些没有内置的预处理器,除了安装对应的依赖,你还需要 [拓展内部的 Webpack 配置](../config/#configurewebpack)
152+
:::
153+
154+
124155
## 脚本和样式提升
125156

126157
有时,你可以只想在当前页面应用一些 JavaScript 或者 CSS,在这种情况下,你可以直接在 Markdown 文件中使用原生的 `<script>` 或者 `<style>` 标签,它们将会从编译后的 HTML 文件中提取出来,并作为生成的 Vue 单文件组件的 `<script>``<style>` 标签。

lib/app/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import NotFound from '@notFound'
77
import { routes } from '@temp/routes'
88
import { siteData } from '@temp/siteData'
99
import enhanceApp from '@temp/enhanceApp'
10+
import themeEnhanceApp from '@temp/themeEnhanceApp'
1011

1112
// suggest dev server restart on base change
1213
if (module.hot) {
@@ -75,7 +76,8 @@ export function createApp () {
7576
})
7677

7778
const options = {}
78-
79+
80+
themeEnhanceApp({ Vue, options, router })
7981
enhanceApp({ Vue, options, router })
8082

8183
const app = new Vue(

lib/default-theme/styles/code.styl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
code
2424
color #fff
2525
padding 0
26-
background-color none
26+
background-color transparent
2727
border-radius 0
2828
&:before
2929
position absolute
@@ -34,8 +34,8 @@
3434
.highlighted-line
3535
background-color rgba(0, 0, 0, 66%)
3636
display block
37-
margin 0.1rem -1.5rem 0
38-
padding 0.1rem 1.5rem
37+
margin 0 -1.5rem
38+
padding 0 1.5rem
3939

4040
pre[class="language-js"], pre[class="language-javascript"]
4141
&:before

lib/prepare.js

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const fs = require('fs-extra')
33
const globby = require('globby')
44
const yamlParser = require('js-yaml')
5+
const tomlParser = require('toml')
56
const yaml = require('yaml-front-matter')
67
const createMarkdown = require('./markdown')
78
const tempPath = path.resolve(__dirname, 'app/.temp')
@@ -52,15 +53,21 @@ if (!Object.assign) Object.assign = require('object-assign')`
5253
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
5354
}
5455

56+
async function writeEnhanceTemp (destName, srcPath, isEnhanceExist) {
57+
await writeTemp(
58+
destName,
59+
isEnhanceExist
60+
? `export { default } from ${JSON.stringify(srcPath)}`
61+
: `export default function () {}`
62+
)
63+
}
64+
5565
// 6. handle enhanceApp.js
56-
const enhancePath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
57-
const hasEnhancePath = fs.existsSync(enhancePath)
58-
await writeTemp(
59-
'enhanceApp.js',
60-
hasEnhancePath
61-
? `export { default } from ${JSON.stringify(enhancePath)}`
62-
: `export default function () {}`
63-
)
66+
const enhanceAppPath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
67+
writeEnhanceTemp('enhanceApp.js', enhanceAppPath, fs.existsSync(enhanceAppPath))
68+
69+
// 7. handle the theme index.js
70+
writeEnhanceTemp('themeEnhanceApp.js', options.themeApp, fs.existsSync(options.themeApp))
6471

6572
return options
6673
}
@@ -69,12 +76,14 @@ async function resolveOptions (sourceDir) {
6976
const vuepressDir = path.resolve(sourceDir, '.vuepress')
7077
const configPath = path.resolve(vuepressDir, 'config.js')
7178
const configYmlPath = path.resolve(vuepressDir, 'config.yml')
79+
const configTomlPath = path.resolve(vuepressDir, 'config.toml')
7280

7381
delete require.cache[configPath]
7482
let siteConfig = {}
7583
if (fs.existsSync(configYmlPath)) {
76-
const content = await fs.readFile(configYmlPath, 'utf-8')
77-
siteConfig = yamlParser.safeLoad(content)
84+
siteConfig = await parseConfig(configYmlPath)
85+
} else if (fs.existsSync(configTomlPath)) {
86+
siteConfig = await parseConfig(configTomlPath)
7887
} else if (fs.existsSync(configPath)) {
7988
siteConfig = require(configPath)
8089
}
@@ -150,6 +159,11 @@ async function resolveOptions (sourceDir) {
150159
} else {
151160
options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
152161
}
162+
163+
const themeApp = path.resolve(themeDir, 'index.js')
164+
if (fs.existsSync(themeApp)) {
165+
options.themeApp = themeApp
166+
}
153167
}
154168

155169
// resolve pages
@@ -283,3 +297,31 @@ function sort (arr) {
283297
return 0
284298
})
285299
}
300+
301+
async function parseConfig (file) {
302+
const content = await fs.readFile(file, 'utf-8')
303+
const [extension] = /.\w+$/.exec(file)
304+
let data
305+
306+
switch (extension) {
307+
case '.yml':
308+
case '.yaml':
309+
data = yamlParser.safeLoad(content)
310+
break
311+
312+
case '.toml':
313+
data = tomlParser.parse(content)
314+
// reformat to match config since TOML does not allow different data type
315+
// https://github.com/toml-lang/toml#array
316+
const format = []
317+
Object.keys(data.head).forEach(meta => {
318+
data.head[meta].forEach(values => {
319+
format.push([meta, values])
320+
})
321+
})
322+
data.head = format
323+
break
324+
}
325+
326+
return data || {}
327+
}

lib/webpack/createBaseConfig.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ module.exports = function createBaseConfig ({
7171
}
7272
})
7373

74+
config.module
75+
.rule('pug')
76+
.test(/\.pug$/)
77+
.use('pug-plain-loader')
78+
.loader('pug-plain-loader')
79+
.end()
80+
7481
if (!siteConfig.evergreen) {
7582
const libDir = path.join(__dirname, '..')
7683
config.module

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"semver": "^5.5.0",
8080
"stylus": "^0.54.5",
8181
"stylus-loader": "^3.0.2",
82+
"toml": "^2.3.3",
8283
"url-loader": "^1.0.1",
8384
"vue": "^2.5.16",
8485
"vue-loader": "^15.0.0-rc.1",

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,6 +6141,10 @@ to-regex@^3.0.1, to-regex@^3.0.2:
61416141
regex-not "^1.0.2"
61426142
safe-regex "^1.1.0"
61436143

6144+
toml@^2.3.3:
6145+
version "2.3.3"
6146+
resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb"
6147+
61446148
61456149
version "2.0.2"
61466150
resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"

0 commit comments

Comments
 (0)