3 [![Build Status][build-badge]][build-status]
4 [![Coverage Status][coverage-badge]][coverage-status]
6 **VFile** is a virtual file format used by [**unified**][unified],
7 a text processing umbrella (it powers [**retext**][retext] for
8 natural language, [**remark**][remark] for markdown, and
9 [**rehype**][rehype] for HTML). Each processors that parse, transform,
10 and compile text, and need a virtual representation of files and a
11 place to store [messages][] about them. Plus, they work in the browser.
12 **VFile** provides these requirements at a small size, in IE 9 and up.
14 > **VFile** is different from the excellent [**vinyl**][vinyl]
15 > in that it has a smaller API, a smaller size, and focuses on
18 VFile can be used anywhere where files need a lightweight representation.
19 For example, it’s used in:
21 * [`documentation`](https://github.com/documentationjs/documentation)
22 — The documentation system for modern JavaScript
23 * [`weh`](https://github.com/wehjs/weh)
24 — Declarative small site generator
25 * [`geojsonhint`](https://github.com/mapbox/geojsonhint)
26 — Complete, fast, standards-based validation for geojson
39 * [Utilities](#utilities)
40 * [Reporters](#reporters)
42 * [VFile(\[options\])](#vfileoptions)
43 * [vfile.contents](#vfilecontents)
44 * [vfile.cwd](#vfilecwd)
45 * [vfile.path](#vfilepath)
46 * [vfile.basename](#vfilebasename)
47 * [vfile.stem](#vfilestem)
48 * [vfile.extname](#vfileextname)
49 * [vfile.dirname](#vfiledirname)
50 * [vfile.history](#vfilehistory)
51 * [vfile.messages](#vfilemessages)
52 * [vfile.data](#vfiledata)
53 * [VFile#toString(\[encoding\])](#vfiletostringencoding)
54 * [VFile#message(reason\[, position\]\[, origin\])](#vfilemessagereason-position-origin)
55 * [VFile#info(reason\[, position\]\[, origin\])](#vfileinforeason-position-origin)
56 * [VFile#fail(reason\[, position\]\[, origin\])](#vfilefailreason-position-origin)
62 var vfile = require('vfile');
64 var file = vfile({path: '~/example.txt', contents: 'Alpha *braavo* charlie.'});
66 file.path; //=> '~/example.txt'
67 file.dirname; //=> '~'
71 file.basename; //=> 'example.md'
73 file.basename = 'index.text';
75 file.history; //=> ['~/example.txt', '~/example.md', '~/index.text']
77 file.message('`braavo` is misspelt; did you mean `bravo`?', {line: 1, column: 8});
79 console.log(file.messages);
85 [ { [~/index.text:1:8: `braavo` is misspelt; did you mean `bravo`?]
86 message: '`braavo` is misspelt; did you mean `bravo`?',
87 name: '~/index.text:1:8',
89 reason: '`braavo` is misspelt; did you mean `bravo`?',
92 location: { start: [Object], end: [Object] },
100 The following list of projects includes tools for working with virtual
101 files. See [**Unist**][unist] for projects working with nodes.
103 * [`convert-vinyl-to-vfile`](https://github.com/dustinspecker/convert-vinyl-to-vfile)
104 — Convert from [Vinyl][]
105 * [`is-vfile-message`](https://github.com/shinnn/is-vfile-message)
106 — Check if a value is a `VMessage` object
107 * [`to-vfile`](https://github.com/vfile/to-vfile)
108 — Create a virtual file from a file-path (and optionally read it)
109 * [`vfile-find-down`](https://github.com/vfile/vfile-find-down)
110 — Find files by searching the file system downwards
111 * [`vfile-find-up`](https://github.com/vfile/vfile-find-up)
112 — Find files by searching the file system upwards
113 * [`vfile-location`](https://github.com/vfile/vfile-location)
114 — Convert between line/column- and range-based locations
115 * [`vfile-statistics`](https://github.com/vfile/vfile-statistics)
116 — Count messages per category
117 * [`vfile-messages-to-vscode-diagnostics`](https://github.com/shinnn/vfile-messages-to-vscode-diagnostics)
118 — Convert to VS Code diagnostics
119 * [`vfile-sort`](https://github.com/vfile/vfile-sort)
120 — Sort messages by line/column
121 * [`vfile-to-eslint`](https://github.com/vfile/vfile-to-eslint)
122 — Convert VFiles to ESLint formatter compatible output
126 The following list of projects show linting results for given virtual files.
127 Reporters _must_ accept `Array.<VFile>` as their first argument, and return
128 `string`. Reporters _may_ accept other values too, in which case it’s suggested
129 to stick to `vfile-reporter`s interface.
131 * [`vfile-reporter`][reporter]
133 * [`vfile-reporter-json`](https://github.com/vfile/vfile-reporter-json)
135 * [`vfile-reporter-pretty`](https://github.com/vfile/vfile-reporter-pretty)
140 ### `VFile([options])`
142 Create a new virtual file. If `options` is `string` or `Buffer`, treats
143 it as `{contents: options}`. If `options` is a `VFile`, returns it.
144 All other options are set on the newly created `vfile`.
146 Path related properties are set in the following order (least specific
147 to most specific): `history`, `path`, `basename`, `stem`, `extname`,
150 It’s not possible to set either `dirname` or `extname` without setting
151 either `history`, `path`, `basename`, or `stem` as well.
157 vfile('console.log("alpha");');
158 vfile(Buffer.from('exit 1'));
159 vfile({path: path.join(__dirname, 'readme.md')});
160 vfile({stem: 'readme', extname: '.md', dirname: __dirname});
161 vfile({other: 'properties', are: 'copied', ov: {e: 'r'}});
166 `Buffer`, `string`, `null` — Raw value.
170 `string` — Base of `path`. Defaults to `process.cwd()`.
174 `string?` — Path of `vfile`. Cannot be nullified.
178 `string?` — Current name (including extension) of `vfile`. Cannot
179 contain path separators. Cannot be nullified either (use
180 `file.path = file.dirname` instead).
184 `string?` — Name (without extension) of `vfile`. Cannot be nullified,
185 and cannot contain path separators.
189 `string?` — Extension (with dot) of `vfile`. Cannot be set if
190 there’s no `path` yet and cannot contain path separators.
194 `string?` — Path to parent directory of `vfile`. Cannot be set if
195 there’s no `path` yet.
199 `Array.<string>` — List of file-paths the file moved between.
203 [`Array.<VMessage>`][message] — List of messages associated with the file.
207 `Object` — Place to store custom information. It’s OK to store custom
208 data directly on the `vfile`, moving it to `data` gives a _little_ more
211 ### `VFile#toString([encoding])`
213 Convert contents of `vfile` to string. If `contents` is a buffer,
214 `encoding` is used to stringify buffers (default: `'utf8'`).
216 ### `VFile#message(reason[, position][, origin])`
218 Associates a message with the file, where `fatal` is set to `false`.
219 Constructs a new [`VMessage`][vmessage] and adds it to
220 [`vfile.messages`][messages].
224 [`VMessage`][vmessage].
226 ### `VFile#info(reason[, position][, origin])`
228 Associates an informational message with the file, where `fatal` is set to
229 `null`. Calls [`#message()`][message] internally.
233 [`VMessage`][vmessage].
235 ### `VFile#fail(reason[, position][, origin])`
237 Associates a fatal message with the file, then immediately throws it.
238 Note: fatal errors mean a file is no longer processable.
239 Calls [`#message()`][message] internally.
243 [`VMessage`][vmessage].
247 [MIT][license] © [Titus Wormer][author]
251 [build-badge]: https://img.shields.io/travis/vfile/vfile.svg
253 [build-status]: https://travis-ci.org/vfile/vfile
255 [coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile.svg
257 [coverage-status]: https://codecov.io/github/vfile/vfile
259 [npm]: https://docs.npmjs.com/cli/install
263 [author]: http://wooorm.com
265 [vfile]: https://cdn.rawgit.com/vfile/vfile/f65510e/logo.svg
267 [unified]: https://github.com/unifiedjs/unified
269 [retext]: https://github.com/wooorm/retext
271 [remark]: https://github.com/wooorm/remark
273 [rehype]: https://github.com/wooorm/rehype
275 [vinyl]: https://github.com/gulpjs/vinyl
277 [unist]: https://github.com/syntax-tree/unist#list-of-utilities
279 [reporter]: https://github.com/vfile/vfile-reporter
281 [vmessage]: https://github.com/vfile/vfile-message
283 [messages]: #vfilemessages
285 [message]: #vfilemessagereason-position-origin