Skip to content

Commit c3ba172

Browse files
authored
Set file.result when processing to non-text
unified is typically, but not always, used to *serialize* when it compiles. Compilers don’t always return such a serialized `string` or `Buffer` though. The previous behavior was to place the result of the stringify phase when processing on `file.contents`. This doesn’t make sense for non-text. This change places non-text results on `file.result`. Closes vfile/vfile#45. Closes GH-87.
1 parent b680d62 commit c3ba172

File tree

4 files changed

+114
-13
lines changed

4 files changed

+114
-13
lines changed

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict'
22

3-
var extend = require('extend')
43
var bail = require('bail')
5-
var vfile = require('vfile')
6-
var trough = require('trough')
4+
var buffer = require('is-buffer')
5+
var extend = require('extend')
76
var plain = require('is-plain-obj')
7+
var trough = require('trough')
8+
var vfile = require('vfile')
89

910
// Expose a frozen processor.
1011
module.exports = unified().freeze()
@@ -37,7 +38,16 @@ function pipelineRun(p, ctx, next) {
3738
}
3839

3940
function pipelineStringify(p, ctx) {
40-
ctx.file.contents = p.stringify(ctx.tree, ctx.file)
41+
var result = p.stringify(ctx.tree, ctx.file)
42+
var file = ctx.file
43+
44+
if (result === undefined || result === null) {
45+
// Empty.
46+
} else if (typeof result === 'string' || buffer(result)) {
47+
file.contents = result
48+
} else {
49+
file.result = result
50+
}
4151
}
4252

4353
// Function to create the first processor.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"dependencies": {
4646
"bail": "^1.0.0",
4747
"extend": "^3.0.0",
48+
"is-buffer": "^2.0.0",
4849
"is-plain-obj": "^2.0.0",
4950
"trough": "^1.0.0",
5051
"vfile": "^4.0.0"

readme.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ return a [`Node`][node].
461461

462462
### `processor.stringify(node[, file])`
463463

464-
Compile a [*syntax tree*][syntax-tree] to text.
464+
Compile a [*syntax tree*][syntax-tree].
465465

466466
###### Parameters
467467

@@ -471,7 +471,7 @@ Compile a [*syntax tree*][syntax-tree] to text.
471471

472472
###### Returns
473473

474-
`string` (see notes) — Textual representation of the [*syntax
474+
`string` or `Buffer` (see notes) — Textual representation of the [*syntax
475475
tree*][syntax-tree]
476476

477477
###### Note
@@ -481,10 +481,11 @@ tree*][syntax-tree]
481481
`stringify` performs the [*stringify phase*][description], not the *run phase*
482482
or other phases.
483483

484-
Be aware that [*compiler*][compiler]s typically, but not always, return
485-
`string`.
484+
unified typically compiles by serializing: most [*compiler*][compiler]s return
485+
`string` (or `Buffer`).
486486
Some compilers, such as the one configured with [`rehype-react`][rehype-react],
487487
return other values (in this case, a React tree).
488+
If you’re using a compiler doesn’t serialize, expect different result values.
488489
When using TypeScript, cast the type on your side.
489490

490491
###### Example
@@ -632,7 +633,7 @@ The returned promise is rejected with a fatal error, or resolved with the
632633
processed [*file*][file].
633634

634635
The parsed, transformed, and compiled value is exposed on
635-
[`file.contents`][vfile-contents].
636+
[`file.contents`][vfile-contents] or `file.result` (see notes).
636637

637638
###### Note
638639

@@ -647,6 +648,14 @@ return other values (in this case, a React tree).
647648
When using TypeScript, cast the type of [`file.contents`][vfile-contents] on
648649
your side.
649650

651+
unified typically compiles by serializing: most [*compiler*][compiler]s return
652+
`string` (or `Buffer`).
653+
Some compilers, such as the one configured with [`rehype-react`][rehype-react],
654+
return other values (in this case, a React tree).
655+
If you’re using a compiler that serializes, the result is available at
656+
`file.contents`.
657+
Otherwise, the result is available at `file.result`.
658+
650659
###### Example
651660

652661
The below example shows how `process` can be used to process a file, whether
@@ -750,19 +759,23 @@ An error is thrown if asynchronous [*plugin*][plugin]s are configured.
750759

751760
([`VFile`][vfile]) — Processed [*file*][file]
752761

762+
The parsed, transformed, and compiled value is exposed on
763+
[`file.contents`][vfile-contents] or `file.result` (see notes).
764+
753765
###### Note
754766

755767
`processSync` freezes the processor if not already [*frozen*][freeze].
756768

757769
`processSync` performs the [*parse*, *run*, and *stringify*
758770
phases][description].
759771

760-
Be aware that [*compiler*][compiler]s typically, but not always, return
761-
`string`.
772+
unified typically compiles by serializing: most [*compiler*][compiler]s return
773+
`string` (or `Buffer`).
762774
Some compilers, such as the one configured with [`rehype-react`][rehype-react],
763775
return other values (in this case, a React tree).
764-
When using TypeScript, cast the type of [`file.contents`][vfile-contents] on
765-
your side.
776+
If you’re using a compiler that serializes, the result is available at
777+
`file.contents`.
778+
Otherwise, the result is available at `file.result`.
766779

767780
###### Example
768781

test/process.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,80 @@ test('processSync(file)', function (t) {
204204
'should pass the result file'
205205
)
206206
})
207+
208+
test('compilers', function (t) {
209+
t.plan(4)
210+
211+
t.equal(
212+
unified()
213+
.use(function () {
214+
this.Parser = simple.Parser
215+
this.Compiler = string
216+
})
217+
.processSync('alpha').contents,
218+
'bravo',
219+
'should compile strings'
220+
)
221+
222+
t.deepEqual(
223+
unified()
224+
.use(function () {
225+
this.Parser = simple.Parser
226+
this.Compiler = buffer
227+
})
228+
.processSync('alpha').contents,
229+
Buffer.from('bravo'),
230+
'should compile buffers'
231+
)
232+
233+
t.deepEqual(
234+
unified()
235+
.use(function () {
236+
this.Parser = simple.Parser
237+
this.Compiler = nully
238+
})
239+
.processSync('alpha').contents,
240+
'alpha',
241+
'should compile null'
242+
)
243+
244+
t.deepEqual(
245+
unified()
246+
.use(function () {
247+
this.Parser = simple.Parser
248+
this.Compiler = nonText
249+
})
250+
.processSync('alpha').result,
251+
{
252+
_owner: null,
253+
type: 'p',
254+
ref: null,
255+
key: 'h-1',
256+
props: {children: ['bravo']}
257+
},
258+
'should compile non-text'
259+
)
260+
261+
function nully() {
262+
return null
263+
}
264+
265+
function string() {
266+
return 'bravo'
267+
}
268+
269+
function buffer() {
270+
return Buffer.from('bravo')
271+
}
272+
273+
function nonText() {
274+
// Somewhat like a React node.
275+
return {
276+
_owner: null,
277+
type: 'p',
278+
ref: null,
279+
key: 'h-1',
280+
props: {children: ['bravo']}
281+
}
282+
}
283+
})

0 commit comments

Comments
 (0)