Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
feat: spread out blog plugin options to theme options
Browse files Browse the repository at this point in the history
  • Loading branch information
billyyyyy3320 committed Jan 13, 2020
1 parent dd6bb0d commit ee5101b
Show file tree
Hide file tree
Showing 6 changed files with 1,474 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = {
root: true,

env: {
"node": true
"node": true,
"jest": true
},

extends: [
Expand Down
48 changes: 37 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const removeMd = require('remove-markdown')
const path = require('path')
module.exports = (themeConfig, ctx) => {
const pick = require('lodash/pick')

module.exports = themeConfig => {
/**
* Default theme configuration
*/
themeConfig = Object.assign(themeConfig, {
nav: themeConfig.nav || [
{
Expand All @@ -20,36 +25,54 @@ module.exports = (themeConfig, ctx) => {
pwa: !!themeConfig.pwa,
})

/**
* Configure blog plugin
*/
const defaultBlogPluginOptions = {
directories: [
{
id: 'post',
dirname: '_posts',
path: '/',
pagination: {
lengthPerPage: 5,
},
},
],
frontmatters: [
{
id: 'tag',
keys: ['tag', 'tags'],
path: '/tag/',
pagination: {
lengthPerPage: 5,
},
},
],
globalPagination: {
lengthPerPage: 5,
},
}

const { modifyBlogPluginOptions } = themeConfig

const blogPluginOptions =
typeof modifyBlogPluginOptions === 'function'
? modifyBlogPluginOptions(defaultBlogPluginOptions)
: defaultBlogPluginOptions
let blogPluginOptions

if (modifyBlogPluginOptions === 'function') {
blogPluginOptions = modifyBlogPluginOptions(defaultBlogPluginOptions)
} else {
const properties = [
'directories',
'frontmatters',
'globalPagination',
'sitemap',
'comment',
'newsletter',
]
blogPluginOptions = Object.assign(
{},
defaultBlogPluginOptions,
pick(themeConfig, properties)
)
}

/**
* Integrate plugins
*/
const plugins = [
'@vuepress/plugin-nprogress',
['@vuepress/medium-zoom', true],
Expand All @@ -62,6 +85,9 @@ module.exports = (themeConfig, ctx) => {
['@vuepress/blog', blogPluginOptions],
]

/**
* Enable pwa
*/
if (themeConfig.pwa) {
plugins.push([
'@vuepress/pwa',
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"example:build": "vuepress build example --temp example/.temp",
"example:dev": "vuepress dev example --temp example/.temp",
"lint": "eslint . --ext .js,.vue",
"prepublishOnly": "npm run example:build && conventional-changelog -p angular -r 2 -i CHANGELOG.md -s",
"test": "npm run lint && jest"
"prepublishOnly": "npm run test && npm run example:build && conventional-changelog -p angular -r 2 -i CHANGELOG.md -s",
"test": "jest"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -60,6 +60,7 @@
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-vue": "^6.0.1",
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
"sort-package-json": "^1.31.0",
"vuepress": "^1.2.0"
Expand Down
69 changes: 69 additions & 0 deletions test/__snapshots__/config.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should match snapshot without any configuration 1`] = `
Object {
"nav": Array [
Object {
"link": "/",
"text": "Blog",
},
Object {
"link": "/tag/",
"text": "Tags",
},
],
"pwa": false,
"summary": true,
"summaryLength": 200,
}
`;

exports[`should match snapshot without any configuration 2`] = `
Object {
"alias": Object {
"fonts": "/Users/billy/Desktop/vuepress-projects/vuepress-theme-blog/fonts",
},
"define": Object {
"THEME_BLOG_PAGINATION_COMPONENT": "Pagination",
},
"extendPageData": [Function],
"plugins": Array [
"@vuepress/plugin-nprogress",
Array [
"@vuepress/medium-zoom",
true,
],
Array [
"@vuepress/search",
Object {
"searchMaxSuggestions": 10,
},
],
Array [
"@vuepress/blog",
Object {
"directories": Array [
Object {
"dirname": "_posts",
"id": "post",
"path": "/",
},
],
"frontmatters": Array [
Object {
"id": "tag",
"keys": Array [
"tag",
"tags",
],
"path": "/tag/",
},
],
"globalPagination": Object {
"lengthPerPage": 5,
},
},
],
],
}
`;
16 changes: 16 additions & 0 deletions test/config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @jest-environment node
*/

const vuepressThemeBlog = require('../')

test('should be a function', () => {
expect(typeof vuepressThemeBlog).toBe('function')
})

test('should match snapshot without any configuration', () => {
const themeConfig = {}
const config = vuepressThemeBlog(themeConfig)
expect(themeConfig).toMatchSnapshot()
expect(config).toMatchSnapshot()
})
Loading

0 comments on commit ee5101b

Please sign in to comment.