Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit 286e35b

Browse files
committed
chore: enhance infrastructure
1 parent 36eba5d commit 286e35b

14 files changed

+2517
-106
lines changed

.eslintrc.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true,
6+
jest: true,
7+
browser: true
8+
},
9+
plugins: ['prettier'],
10+
extends: ['eslint:recommended', require.resolve('eslint-config-prettier')],
11+
rules: {
12+
'prettier/prettier': 'warn',
13+
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
14+
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
15+
},
16+
parserOptions: {
17+
parser: 'babel-eslint',
18+
sourceType: 'module'
19+
}
20+
}

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.prettierrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"printWidth": 80,
3+
"trailingComma": "none",
4+
"tabWidth": 2,
5+
"semi": false,
6+
"singleQuote": true,
7+
"useTabs": false,
8+
"bracketSpacing": true,
9+
"insertPragma": false,
10+
"arrowParens": "avoid",
11+
"htmlWhitespaceSensitivity": "ignore"
12+
}

lib/compileTemplate.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ var chalk = require('chalk')
55
var vueCompiler = require('vue-template-compiler')
66
var transpile = require('vue-template-es2015-compiler')
77

8-
module.exports = function compileTemplate (template, compiler) {
8+
module.exports = function compileTemplate(template) {
99
var compiled = vueCompiler.compile(template)
1010
if (compiled.errors.length) {
11-
compiled.errors.forEach(function (msg) {
11+
compiled.errors.forEach(function(msg) {
1212
console.error('\n' + chalk.red(msg) + '\n')
1313
})
1414
throw new Error('Vue template compilation failed')
1515
} else {
1616
return {
1717
render: toFunction(compiled.render),
18-
staticRenderFns: '[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
18+
staticRenderFns:
19+
'[' + compiled.staticRenderFns.map(toFunction).join(',') + ']'
1920
}
2021
}
2122
}
2223

23-
function toFunction (code) {
24+
function toFunction(code) {
2425
return transpile('function render () {' + code + '}')
2526
}

lib/customTOC.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const select = require('unist-util-select')
22
const { slugify } = require('transliteration')
33
const toString = require('mdast-util-to-string')
44

5-
module.exports = function yaml () {
6-
return function transformer (tree, file) {
5+
module.exports = function yaml() {
6+
return function transformer(tree, file) {
77
let lastH2 = null
88
const level2 = []
99

@@ -19,7 +19,7 @@ module.exports = function yaml () {
1919

2020
// TOC
2121
if (depth === 2) {
22-
level2.push(lastH2 = node)
22+
level2.push((lastH2 = node))
2323
} else if (depth === 3) {
2424
lastH2.children.push(node)
2525
}
@@ -29,7 +29,7 @@ module.exports = function yaml () {
2929
}
3030
}
3131

32-
function attchSlug (node, id) {
32+
function attchSlug(node, id) {
3333
if (!node.data) {
3434
node.data = {}
3535
}

lib/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const processor = unified()
3333
// add `v-pre` for code blocks
3434
.use(
3535
() =>
36-
function transformer (tree) {
36+
function transformer(tree) {
3737
const pres = select(tree, 'element[tagName=pre]')
3838
pres.forEach(pre => {
3939
const { children, properties } = pre
@@ -55,25 +55,29 @@ const processor = unified()
5555
const compileTemplate = require('./compileTemplate')
5656
const styleInjectFile = join(__dirname, 'styleInject.js')
5757

58-
module.exports = function process (path, option, callback) {
58+
module.exports = function process(path, option, callback) {
5959
if (typeof option === 'function') {
6060
callback = option
6161
}
6262

6363
const file = vfile.readSync(path)
6464
file.userOption = option
6565
// 与 vuepress-plugin-playground 整合
66-
file.contents = file.contents.toString().replace(/```(html|vue)\s+@(playground|demo)\s/g, m => {
67-
return m.replace(/@(playground|demo)/, '')
68-
})
69-
processor.process(file, function (err, file) {
66+
file.contents = file.contents
67+
.toString()
68+
.replace(/```(html|vue)\s+@(playground|demo)\s/g, m => {
69+
return m.replace(/@(playground|demo)/, '')
70+
})
71+
processor.process(file, function(err, file) {
7072
if (err) {
7173
return callback(err)
7274
}
7375

7476
if (file) {
7577
const { components, styles } = file.data
76-
const content = `<article class="markdown-body">${file.contents}</article>`
78+
const content = `<article class="markdown-body">${
79+
file.contents
80+
}</article>`
7781
const { render, staticRenderFns } = compileTemplate(content)
7882

7983
file.extname = '.js'

lib/inline/compile.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const vue = require('rollup-plugin-vue')
44
const commonjs = require('rollup-plugin-commonjs')
55
const memory = require('../rollup/pluginMemory')
66

7-
module.exports = function compile (sfc, name) {
7+
module.exports = function compile(sfc, name) {
88
let style = ''
99
const file = resolve(process.cwd(), './virtual.vue')
1010
const config = {
@@ -16,7 +16,7 @@ module.exports = function compile (sfc, name) {
1616
}),
1717
commonjs(),
1818
vue({
19-
css (content) {
19+
css(content) {
2020
style = content
2121
},
2222
vue: {
@@ -30,10 +30,12 @@ module.exports = function compile (sfc, name) {
3030
name,
3131
format: 'iife'
3232
}
33-
return rollup(config).then(bundle => bundle.generate(output)).then(ret => {
34-
return {
35-
style,
36-
script: ret.code
37-
}
38-
})
33+
return rollup(config)
34+
.then(bundle => bundle.generate(output))
35+
.then(ret => {
36+
return {
37+
style,
38+
script: ret.code
39+
}
40+
})
3941
}

lib/inline/normalize.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
/**
32
* 用于从 Vue SFC 代码块中提取出不同部分
43
*/
5-
module.exports = function normalizeVueDemo (code) {
4+
module.exports = function normalizeVueDemo(code) {
65
const reStyle = /<style(|\s*[^>]+)>([\s\S]+)<\/style>/
76
const reScript = /<script(|\s*[^>]+)>([\s\S]+)<\/script>/
87
const reTemplate = /<template(|\s*[^>]+)>([\s\S]+)<\/template>/
@@ -19,7 +18,10 @@ module.exports = function normalizeVueDemo (code) {
1918

2019
// if `<template>` absent
2120
if (template === '') {
22-
template = code.replace(reStyle, '').replace(reScript, '')
21+
template = code
22+
.replace(reStyle, '')
23+
.replace(reScript, '')
24+
.trim()
2325
}
2426

2527
if (script === '') {
@@ -40,7 +42,9 @@ ${script}
4042
</script>`
4143

4244
if (style !== '') {
43-
sfc = `<style ${styleAttrs ? `${styleAttrs} ` : ''}scoped>${style}</style>\n` + sfc
45+
sfc =
46+
`<style ${styleAttrs ? `${styleAttrs} ` : ''}scoped>${style}</style>\n` +
47+
sfc
4448
}
4549

4650
const demoOnly = templateAttrs.indexOf('demo-only') > -1

lib/parseFront.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const select = require('unist-util-select')
33
const toString = require('mdast-util-to-string')
44
const ofType = (node, type) => is({ type }, node)
55

6-
module.exports = function yaml () {
7-
return function transformer (tree, file) {
6+
module.exports = function yaml() {
7+
return function transformer(tree, file) {
88
const headings = select(tree, 'heading[depth=1]')
99
const h1 = headings.length && headings[0]
1010
const heading = h1 ? toString(h1) : 'Not Found'

lib/rollup/pluginMemory.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/**
22
* https://github.com/TrySound/rollup-plugin-memory/blob/master/src/index.js
33
*/
4-
function isPath (path) {
4+
function isPath(path) {
55
return typeof path === 'string'
66
}
77

8-
function isContents (contents) {
8+
function isContents(contents) {
99
return typeof contents === 'string' || Buffer.isBuffer(contents)
1010
}
1111

12-
module.exports = function memory (config = {}) {
12+
module.exports = function memory(config = {}) {
1313
let path = isPath(config.path) ? config.path : null
1414
let contents = isContents(config.contents) ? String(config.contents) : null
1515

1616
return {
1717
// do not use `options` as name for 1st argument
18-
options (rollupOptions) {
18+
options(rollupOptions) {
1919
const { input } = rollupOptions
2020
if (input && typeof input === 'object') {
2121
if (isPath(input.path)) {
@@ -28,7 +28,7 @@ module.exports = function memory (config = {}) {
2828
rollupOptions.input = path
2929
},
3030

31-
resolveId (id) {
31+
resolveId(id) {
3232
if (path === null || contents === null) {
3333
throw Error(
3434
"'path' should be a string and 'contents' should be a string of Buffer"
@@ -39,7 +39,7 @@ module.exports = function memory (config = {}) {
3939
}
4040
},
4141

42-
load (id) {
42+
load(id) {
4343
if (id === path) {
4444
return contents
4545
}

lib/styleInject.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://github.com/egoist/style-inject/blob/master/src/index.js
2-
module.exports = function styleInject (css, { insertAt } = {}) {
2+
module.exports = function styleInject(css, { insertAt } = {}) {
33
if (!css || typeof document === 'undefined') return
44

55
const head = document.head || document.getElementsByTagName('head')[0]
@@ -22,7 +22,7 @@ module.exports = function styleInject (css, { insertAt } = {}) {
2222
style.appendChild(document.createTextNode(css))
2323
}
2424

25-
return function () {
25+
return function() {
2626
head.removeChild(style)
2727
}
2828
}

lib/vueTransform.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const classnames = require('classnames')
77
const compileInlineDemo = require('./inline/compile')
88
const normalizeInlineDemo = require('./inline/normalize')
99

10-
module.exports = function vueTransform () {
10+
module.exports = function vueTransform() {
1111
return transformer
1212
}
1313

14-
function transformer (tree, file, next) {
14+
function transformer(tree, file, next) {
1515
let uid = 0
1616
const promises = []
1717
visit(tree, 'element', visitor)
@@ -29,7 +29,7 @@ function transformer (tree, file, next) {
2929
next(e, tree, file)
3030
})
3131

32-
function visitor (node) {
32+
function visitor(node) {
3333
if (!is({ tagName: 'pre' }, node)) {
3434
return
3535
}

package.json

+47-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "jest --watch"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/AngusFu/md2vue.git"
7+
"test": "jest --watch",
8+
"commit": "npx git-cz",
9+
"release": "npx standard-version",
10+
"lint": "eslint ./lib",
11+
"lint:fix": "eslint --fix --ext .js,.jsx ./lib",
12+
"format": "pretty-quick --pattern \"**/*.*(js|jsx)\""
1213
},
14+
"repository": "git+https://github.com/AngusFu/md2vue.git",
1315
"author": "wemlion <[email protected]>",
1416
"license": "MIT",
1517
"bugs": {
@@ -54,9 +56,48 @@
5456
"stylus": "^0.54.5"
5557
},
5658
"devDependencies": {
59+
"@commitlint/cli": "^7.5.2",
60+
"@commitlint/config-conventional": "^7.5.0",
5761
"@vue/test-utils": "^1.0.0-beta.24",
62+
"babel-eslint": "^10.0.1",
63+
"commitizen": "^3.0.7",
64+
"cz-conventional-changelog": "^2.1.0",
65+
"eslint": "^5.15.1",
66+
"eslint-config-prettier": "^4.1.0",
67+
"eslint-formatter-friendly": "^6.0.0",
68+
"eslint-plugin-prettier": "^3.0.1",
69+
"husky": "^1.3.1",
5870
"jest": "^23.5.0",
59-
"standard-version": "^5.0.0",
71+
"lint-staged": "^8.1.5",
72+
"prettier": "^1.16.4",
73+
"pretty-quick": "^1.10.0",
74+
"standard-version": "^5.0.1",
6075
"vue": "^2.5.17"
76+
},
77+
"config": {
78+
"commitizen": {
79+
"path": "./node_modules/cz-conventional-changelog"
80+
}
81+
},
82+
"husky": {
83+
"hooks": {
84+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
85+
"pre-commit": "pretty-quick --staged && lint-staged"
86+
}
87+
},
88+
"lint-staged": {
89+
"linters": {
90+
"*.{js,jsx}": [
91+
"eslint --format friendly"
92+
]
93+
},
94+
"ignore": [
95+
"**/dist/*.js"
96+
]
97+
},
98+
"commitlint": {
99+
"extends": [
100+
"@commitlint/config-conventional"
101+
]
61102
}
62103
}

0 commit comments

Comments
 (0)