Skip to content

Commit 1f19de3

Browse files
authored
update for next version of reshape/spike (#18)
* update for next version of reshape/spike * remove extra options * 0.6.0-0 * add parserOptions, redo tests to be more thorough and reasonable * 0.6.0-1 * update reshape-exp * 0.6.0-2 * add append and prepend plugins options, more docs * internal docs and naming * 0.6.0-3 * update deps
1 parent eb3e958 commit 1f19de3

File tree

5 files changed

+1665
-1465
lines changed

5 files changed

+1665
-1465
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ Any of these plugins can be customized by passing options described below.
8181
| ---- | ----------- | ------- |
8282
| **root** | Root path used to resolve layouts and includes | |
8383
| **filename** | Name of the file being compiled, used for error traces and as the include/layout root if not otherwise provided | |
84-
| **addDependencyTo** | Object with `addDependency` method that will get file paths for tracked deps from includes/layouts | |
85-
| **webpack** | Shortcut for webpack users to set the `root` and `addDependencyTo` options more easily. Pass webpack loader context. | |
8684
| **delimiters** | Delimiters used for html-escaped expressions | `['{{', '}}']` |
8785
| **unescapeDelimiters** | Delimiters used for unescaped expressions | `['{{{', '}}}']` |
8886
| **markdown** | Options passed in to [markdown-it](https://github.com/markdown-it/markdown-it) constructor | `{ typographer: true, linkify: true }` |
@@ -91,7 +89,10 @@ Any of these plugins can be customized by passing options described below.
9189
| **retext** | Plugins to be passed to the [reshape-retext](https://github.com/reshape/retext) plugin | `[smartypants]` ([ref](https://github.com/wooorm/retext-smartypants)) |
9290
| **locals** | Added directly to the output object, used when compiling a reshape template to html | `{}` |
9391
| **alias** | Alias option to be passed to the [include plugin](https://github.com/reshape/include#options) | |
92+
| **parserRules** | Parser rules to be passed to the [include plugin](https://github.com/reshape/include#options) | |
9493
| **minify** | Minifies the html output by removing excess spaces and line breaks | `false` |
94+
| **appendPlugins** | Adds a single plugin or array of plugins after all the defaults | |
95+
| **prependPlugins** | Adds a single plugin or array of plugins before all the defaults | |
9596

9697
### Markdown Rendering Functions
9798

lib/index.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@ let layouts = require('reshape-layouts')
88
let retext = require('reshape-retext')
99
let beautify = require('reshape-beautify')
1010
let minify = require('reshape-minify')
11+
let reshapeParser = require('reshape-parser')
1112

13+
/**
14+
* Primary export, formats options and returns an object with intelligent
15+
* defaults.
16+
* @param {Object} [options={}] - options object
17+
* @param {Array} options.delimiters - passed to expressions plugin
18+
* @param {Array} options.unescapeDelimiters - passed to expressions plugin
19+
* @param {String} options.root - passed to layouts & include plugins
20+
* @param {Object} options.alias - passed to include plugin
21+
* @param {Object} options.parserRules - passed to include plugin
22+
* @param {Object|Function} [options.retext=smartypants] - passed to retext
23+
* plugin
24+
* @param {Object} [options.content={}] - passed to content plugin
25+
* @param {Object} options.markdown - passed to markdown-it constructor
26+
* @param {Object} options.locals - if passed, renders as html string
27+
* @param {Boolean} options.minify - add minify plugin if true
28+
* @param {String} options.filename - copied directly to output
29+
* @param {Function} [options.parser=sugarml] - undefined if false
30+
* @param {Array|String} options.appendPlugins - appended to plugins array
31+
* @param {Array|String} options.prependPlugins - prepended to plugins array
32+
* @return {Object} valid reshape options object
33+
*/
1234
module.exports = function reshapeStandard (options = {}) {
13-
// set options using webpack shortcut if present
14-
if (options.webpack) {
15-
options.filename = options.webpack.resourcePath
16-
options.addDependencyTo = options.webpack
17-
}
18-
1935
// add options for expressions, layouts, and includes if present
20-
const expressionsOpt = selectiveMerge(options, ['delimiters', 'unescapeDelimiters'])
21-
const layoutsOpt = selectiveMerge(options, ['root', 'addDependencyTo'])
22-
const includeOpt = selectiveMerge(options, ['root', 'addDependencyTo', 'alias'])
36+
const expressionsOpt = selectKeys(options, ['delimiters', 'unescapeDelimiters'])
37+
const layoutsOpt = selectKeys(options, ['root'])
38+
const includeOpt = selectKeys(options, ['root', 'alias', 'parserRules'])
2339

2440
// if the user has not provided a custom parser or `false`, use sugarml
2541
let parserOpt = options.parser || sugarml
@@ -38,6 +54,12 @@ module.exports = function reshapeStandard (options = {}) {
3854
contentOpt.mdi = md.renderInline.bind(md)
3955
}
4056

57+
// default parserRules if using sugarml correctly import html and svg files
58+
if (!includeOpt.parserRules && parserOpt === sugarml) {
59+
includeOpt.parserRules = [{ test: /\.html|svg$/, parser: reshapeParser }]
60+
}
61+
62+
// add all the default plugins with their options
4163
const plugins = [
4264
layouts(layoutsOpt),
4365
include(includeOpt),
@@ -46,6 +68,16 @@ module.exports = function reshapeStandard (options = {}) {
4668
retext(options.retext || smartypants)
4769
]
4870

71+
// append and prepend plugins if needed
72+
if (options.appendPlugins) {
73+
plugins.push(...Array.prototype.concat(options.appendPlugins))
74+
}
75+
76+
if (options.prependPlugins) {
77+
plugins.unshift(...Array.prototype.concat(options.prependPlugins))
78+
}
79+
80+
// append the minify or beautify formatting plugin (always last)
4981
if (options.minify) {
5082
plugins.push(minify())
5183
} else {
@@ -60,7 +92,15 @@ module.exports = function reshapeStandard (options = {}) {
6092
}
6193
}
6294

63-
function selectiveMerge (opts, optNames) {
95+
/**
96+
* Given an options object and an array of key names, return an object filtered
97+
* to contain only the keys in the optNames array, if they exist on the options
98+
* object.
99+
* @param {Object} opts - full options object
100+
* @param {Array} optNames - keys to filter
101+
* @return {Object} object filtered for the specific keys
102+
*/
103+
function selectKeys (opts, optNames) {
64104
return optNames.reduce((m, opt) => {
65105
if (typeof opts[opt] !== 'undefined') { m[opt] = opts[opt] }; return m
66106
}, {})

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "reshape-standard",
33
"description": "a standard plugin pack for reshape",
4-
"version": "0.5.0",
4+
"version": "0.6.0-3",
55
"author": "Jeff Escalante",
66
"ava": {
77
"verbose": "true"
@@ -11,16 +11,17 @@
1111
"markdown-it": "^8.2.2",
1212
"reshape-beautify": "^0.1.2",
1313
"reshape-content": "^0.2.0",
14-
"reshape-expressions": "^0.1.3",
15-
"reshape-include": "^0.3.0",
16-
"reshape-layouts": "^0.2.1",
14+
"reshape-expressions": "^0.1.5",
15+
"reshape-include": "^1.0.0",
16+
"reshape-layouts": "^1.0.0",
1717
"reshape-minify": "^0.1.1",
18+
"reshape-parser": "^0.2.1",
1819
"reshape-retext": "^0.3.0",
1920
"retext-smartypants": "^2.0.0",
2021
"sugarml": "^0.5.0"
2122
},
2223
"devDependencies": {
23-
"ava": "^0.18.1",
24+
"ava": "^0.19.0",
2425
"coveralls": "2.x",
2526
"nyc": "^10.0.0",
2627
"reshape": "^0.4.0",

test/index.js

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,117 @@ const rewire = require('rewire')
33
const standardRewired = rewire('..')
44
const standard = require('..')
55

6-
test('options passed correctly', (t) => {
7-
standardRewired.__set__('content', (opts) => {
6+
test('content options passed correctly', (t) => {
7+
const undo = standardRewired.__set__('content', (opts) => {
88
t.truthy(opts.md === 'test')
99
})
10-
standardRewired.__set__('include', (opts) => {
10+
standardRewired({ content: { md: 'test' } })
11+
undo()
12+
})
13+
14+
test('include options passed correctly', (t) => {
15+
const undo = standardRewired.__set__('include', (opts) => {
1116
t.truthy(opts.root === 'test')
12-
t.truthy(opts.addDependencyTo.addDependency === 'test')
17+
t.truthy(opts.alias === 'test')
18+
t.truthy(opts.parserRules === 'test')
1319
})
14-
standardRewired.__set__('layouts', (opts) => {
20+
standardRewired({ root: 'test', alias: 'test', parserRules: 'test' })
21+
undo()
22+
})
23+
24+
test('layouts options passed correctly', (t) => {
25+
const undo = standardRewired.__set__('layouts', (opts) => {
1526
t.truthy(opts.root === 'test')
16-
t.truthy(opts.addDependencyTo.addDependency === 'test')
1727
})
18-
standardRewired.__set__('expressions', (opts) => {
28+
standardRewired({ root: 'test' })
29+
undo()
30+
})
31+
32+
test('expressions options passed correctly', (t) => {
33+
const undo = standardRewired.__set__('expressions', (opts) => {
1934
t.truthy(opts.delimiters === 'test')
2035
t.truthy(opts.unescapeDelimiters === 'test')
2136
})
22-
standardRewired.__set__('retext', (opts) => {
37+
standardRewired({ delimiters: 'test', unescapeDelimiters: 'test' })
38+
undo()
39+
})
40+
41+
test('retext options passed correctly', (t) => {
42+
const undo = standardRewired.__set__('retext', (opts) => {
2343
t.truthy(opts.length === 3)
2444
})
25-
standardRewired.__set__('MarkdownIt', class Mock {
26-
constructor (opts) {
27-
t.truthy(opts === 'test')
28-
}
45+
standardRewired({ retext: [1, 2, 3] })
46+
undo()
47+
})
48+
49+
test('filename passed correctly', (t) => {
50+
const out = standard({ filename: 'test' })
51+
t.truthy(out.filename === 'test')
52+
})
53+
54+
test('defaults come out right', (t) => {
55+
const out = standard()
56+
t.truthy(out.parser.name === 'SugarMLParser')
57+
t.truthy(Object.keys(out.locals).length === 0)
58+
t.truthy(out.filename === undefined)
59+
t.truthy(out.plugins.length === 6)
60+
})
61+
62+
test('content defaults', (t) => {
63+
const undo = standardRewired.__set__('content', (opts) => {
64+
t.truthy(typeof opts.md === 'function')
65+
t.truthy(typeof opts.mdi === 'function')
2966
})
67+
standardRewired()
68+
undo()
69+
})
3070

31-
const out1 = standardRewired({
32-
root: 'test',
33-
webpack: { resourcePath: 'test', addDependency: 'test' },
34-
delimiters: 'test',
35-
locals: 'true',
36-
unescapeDelimiters: 'test',
37-
content: { md: 'test' },
38-
retext: [1, 2, 3],
39-
markdown: 'test'
71+
test('parserRules defaults', (t) => {
72+
const undo = standardRewired.__set__('include', (opts) => {
73+
t.truthy(opts.parserRules[0].test.exec)
4074
})
75+
standardRewired()
76+
undo()
77+
})
4178

42-
const out2 = standard({
43-
parser: false,
44-
addDependencyTo: { addDependency: (x) => x },
45-
locals: 'true',
46-
minify: true
79+
test('retext default', (t) => {
80+
const undo = standardRewired.__set__('retext', (opts) => {
81+
t.truthy(typeof opts === 'function')
4782
})
83+
standardRewired()
84+
undo()
85+
})
86+
87+
test('parser false', (t) => {
88+
const out = standard({ parser: false })
89+
t.truthy(out.parser === undefined)
90+
})
91+
92+
test('alternate parser', (t) => {
93+
const out = standard({ parser: 'test' })
94+
t.truthy(out.parser === 'test')
95+
})
96+
97+
test('passed locals', (t) => {
98+
const out = standard({ locals: 'test' })
99+
t.truthy(out.locals === 'test')
100+
})
101+
102+
test('minify option', (t) => {
103+
const out = standard({ minify: true })
104+
t.truthy(out.plugins[out.plugins.length - 1].name === 'minifyPlugin')
105+
})
106+
107+
test('appendPlugins option', (t) => {
108+
const out = standard({ appendPlugins: ['test'] })
109+
const out2 = standard({ appendPlugins: 'test' })
110+
t.truthy(out.plugins[out.plugins.length - 2] === 'test')
111+
t.truthy(out2.plugins[out.plugins.length - 2] === 'test')
112+
})
48113

49-
t.truthy(out1.parser)
50-
t.truthy(out1.locals)
51-
t.truthy(out1.filename === 'test')
52-
t.truthy(out1.plugins.length === 6)
53-
t.falsy(out2.parser)
54-
t.truthy(out2.plugins[out2.plugins.length - 1].name === 'minifyPlugin')
114+
test('prependPlugins option', (t) => {
115+
const out = standard({ prependPlugins: ['test'] })
116+
const out2 = standard({ prependPlugins: 'test' })
117+
t.truthy(out.plugins[0] === 'test')
118+
t.truthy(out2.plugins[0] === 'test')
55119
})

0 commit comments

Comments
 (0)