Skip to content

Commit a27692e

Browse files
authored
Eval code after expressions resolve (#29)
* test feature - eval code after expressions resolve * add integration test
1 parent e6d693d commit a27692e

File tree

6 files changed

+164
-125
lines changed

6 files changed

+164
-125
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Any of these plugins can be customized by passing options described below.
9494
| **minify** | Minifies the html output by removing excess spaces and line breaks | `false` |
9595
| **appendPlugins** | Adds a single plugin or array of plugins after all the defaults | |
9696
| **prependPlugins** | Adds a single plugin or array of plugins before all the defaults | |
97+
| **template** | Set this to `true` if you are trying to output a client-side template function. | false |
98+
| **locals** | Optionally set your locals as soon as expressions are evaluated. | |
9799

98100
### Markdown Rendering Functions
99101

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let retext = require('reshape-retext')
99
let beautify = require('reshape-beautify')
1010
let minify = require('reshape-minify')
1111
let reshapeParser = require('reshape-parser')
12+
let evalCode = require('reshape-eval-code')
1213

1314
/**
1415
* Primary export, formats options and returns an object with intelligent
@@ -68,6 +69,9 @@ module.exports = function reshapeStandard (options = {}) {
6869
layouts(layoutsOpt),
6970
include(includeOpt),
7071
expressions(expressionsOpt),
72+
// eval code here if it's a static view so that locals can be
73+
// processed by content, retext, minifier, etc
74+
(options.template ? x => x : evalCode(options.locals)),
7175
content(contentOpt),
7276
retext(options.retext || smartypants)
7377
]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"markdown-it": "^8.2.2",
1212
"reshape-beautify": "^0.1.2",
1313
"reshape-content": "^0.2.0",
14+
"reshape-eval-code": "^0.3.2",
1415
"reshape-expressions": "^0.1.5",
1516
"reshape-include": "^1.0.0",
1617
"reshape-layouts": "^1.0.0",
@@ -21,7 +22,7 @@
2122
"sugarml": "^0.6.0"
2223
},
2324
"devDependencies": {
24-
"ava": "^0.19.1",
25+
"ava": "^0.20.0",
2526
"coveralls": "^2.13.1",
2627
"nyc": "^11.0.3",
2728
"reshape": "^0.4.0",

test/fixtures/index.sgr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
p(mdi) {{ "wow **amazing**" }}

test/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const test = require('ava')
22
const rewire = require('rewire')
33
const standardRewired = rewire('..')
44
const standard = require('..')
5+
const path = require('path')
6+
const fixtures = path.join(__dirname, 'fixtures')
7+
const reshape = require('reshape')
8+
const fs = require('fs')
59

610
test('content options passed correctly', (t) => {
711
const undo = standardRewired.__set__('content', (opts) => {
@@ -56,7 +60,7 @@ test('defaults come out right', (t) => {
5660
t.truthy(out.parser.name === 'SugarMLParser')
5761
t.truthy(Object.keys(out.locals).length === 0)
5862
t.truthy(out.filename === undefined)
59-
t.truthy(out.plugins.length === 6)
63+
t.truthy(out.plugins.length === 7)
6064
})
6165

6266
test('content defaults', (t) => {
@@ -104,6 +108,21 @@ test('minify option', (t) => {
104108
t.truthy(out.plugins[out.plugins.length - 1].name === 'minifyPlugin')
105109
})
106110

111+
test('locals option', (t) => {
112+
const undo = standardRewired.__set__('evalCode', (opts) => {
113+
t.truthy(opts === true)
114+
})
115+
standardRewired({ locals: true })
116+
undo()
117+
})
118+
119+
test('template option', (t) => {
120+
const out = standard()
121+
t.truthy(out.plugins[3].name === 'evalCodePlugin')
122+
const out2 = standard({ template: true })
123+
t.falsy(out2.plugins[3].name === 'evalCodePlugin')
124+
})
125+
107126
test('appendPlugins option', (t) => {
108127
const out = standard({ appendPlugins: ['test'] })
109128
const out2 = standard({ appendPlugins: 'test' })
@@ -132,3 +151,12 @@ test('markdownPlugins option', (t) => {
132151
standardRewired({ markdownPlugins: plugins })
133152
undo()
134153
})
154+
155+
test.only('integration', (t) => {
156+
const markup = fs.readFileSync(path.join(fixtures, 'index.sgr'), 'utf8')
157+
return reshape(standard())
158+
.process(markup)
159+
.then((res) => {
160+
t.is(res.output().trim(), '<p>wow <strong>amazing</strong></p>')
161+
})
162+
})

0 commit comments

Comments
 (0)