Package Exports
- vfile
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vfile) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
vfile is a virtual file format part of the unified collective.
Announcing the unified collective! π Read more about it on Medium Β»
Sponsors
π₯ ZEIT |
π₯ Gatsby |
π₯ Compositor |
Holloway |
You? |
Intro
vfile is a virtual file format used by unified, a text processing umbrella (it powers retext for natural language, remark for markdown, and rehype for HTML). Each processors that parse, transform, and compile text, and need a virtual representation of files and a place to store messages about them. Plus, they work in the browser. vfile provides these requirements at a small size.
- Visit
unified.js.org
and try its guides for an overview - Read unifiedβs readme for a technical intro
- Follow us on Medium and Twitter to see what weβre up to
- Check out Contribute below to find out how to help out
vfile is different from the excellent vinyl in that it has a smaller API, a smaller size, and focuses on messages.
vfile can be used anywhere where files need a lightweight representation. For example, itβs used in:
documentation
β The documentation system for modern JavaScriptawoo
β Declarative small site generatorgeojsonhint
β Complete, fast, standards-based validation for geojson
Installation
npm:
npm install vfile
Table of Contents
Usage
var vfile = require('vfile')
var file = vfile({path: '~/example.txt', contents: 'Alpha *braavo* charlie.'})
file.path // => '~/example.txt'
file.dirname // => '~'
file.extname = '.md'
file.basename // => 'example.md'
file.basename = 'index.text'
file.history // => ['~/example.txt', '~/example.md', '~/index.text']
file.message('`braavo` is misspelt; did you mean `bravo`?', {
line: 1,
column: 8
})
console.log(file.messages)
Yields:
[ { [~/index.text:1:8: `braavo` is misspelt; did you mean `bravo`?]
message: '`braavo` is misspelt; did you mean `bravo`?',
name: '~/index.text:1:8',
file: '~/index.text',
reason: '`braavo` is misspelt; did you mean `bravo`?',
line: 1,
column: 8,
location: { start: [Object], end: [Object] },
ruleId: null,
source: null,
fatal: false } ]
Utilities
The following list of projects includes tools for working with virtual files. See unist for projects working with nodes.
convert-vinyl-to-vfile
β Convert from Vinylis-vfile-message
β Check if a value is aVMessage
objectto-vfile
β Create a virtual file from a file-path (and optionally read it)vfile-find-down
β Find files by searching the file system downwardsvfile-find-up
β Find files by searching the file system upwardsvfile-glob
β Find files by glob patternsvfile-is
β Check if a file passes a testvfile-location
β Convert between line/column- and range-based locationsvfile-message
β Create aVMessage
object (used invfile
itself)vfile-messages-to-vscode-diagnostics
β Convert to VS Code diagnosticsvfile-statistics
β Count messages per categoryvfile-sort
β Sort messages by line/columnvfile-to-eslint
β Convert vfiles to ESLint formatter compatible output
Reporters
The following list of projects show linting results for given virtual files.
Reporters must accept Array.<VFile>
as their first argument, and return
string
.
Reporters may accept other values too, in which case itβs suggested to stick
to vfile-reporter
s interface.
vfile-reporter
β Stylish reportervfile-reporter-json
β JSON reportervfile-reporter-folder-json
β JSON reporter with a folder structurevfile-reporter-pretty
β Pretty reporter
API
VFile([options])
Create a new virtual file.
If options
is string
or Buffer
, treats it as {contents: options}
.
If options
is a VFile
, returns it.
All other options are set on the newly created vfile
.
Path related properties are set in the following order (least specific
to most specific): history
, path
, basename
, stem
, extname
,
dirname
.
Itβs not possible to set either dirname
or extname
without setting
either history
, path
, basename
, or stem
as well.
Example
vfile()
vfile('console.log("alpha");')
vfile(Buffer.from('exit 1'))
vfile({path: path.join(__dirname, 'readme.md')})
vfile({stem: 'readme', extname: '.md', dirname: __dirname})
vfile({other: 'properties', are: 'copied', ov: {e: 'r'}})
vfile.contents
Buffer
, string
, null
β Raw value.
vfile.cwd
string
β Base of path
.
Defaults to process.cwd()
.
vfile.path
string?
β Path of vfile
.
Cannot be nullified.
vfile.basename
string?
β Current name (including extension) of vfile
.
Cannot contain path separators.
Cannot be nullified either (use file.path = file.dirname
instead).
vfile.stem
string?
β Name (without extension) of vfile
.
Cannot be nullified, and cannot contain path separators.
vfile.extname
string?
β Extension (with dot) of vfile
.
Cannot be set if thereβs no path
yet and cannot contain path separators.
vfile.dirname
string?
β Path to parent directory of vfile
.
Cannot be set if thereβs no path
yet.
vfile.history
Array.<string>
β List of file-paths the file moved between.
vfile.messages
Array.<VMessage>
β List of messages associated with the file.
vfile.data
Object
β Place to store custom information.
Itβs OK to store custom data directly on the vfile
, moving it to data
gives
a little more privacy.
VFile#toString([encoding])
Convert contents of vfile
to string.
If contents
is a buffer, encoding
is used to stringify buffers (default:
'utf8'
).
VFile#message(reason[, position][, origin])
Associates a message with the file, where fatal
is set to false
.
Constructs a new VMessage
and adds it to
vfile.messages
.
Returns
VFile#info(reason[, position][, origin])
Associates an informational message with the file, where fatal
is set to
null
.
Calls #message()
internally.
Returns
VFile#fail(reason[, position][, origin])
Associates a fatal message with the file, then immediately throws it.
Note: fatal errors mean a file is no longer processable.
Calls #message()
internally.
Throws
Contribute
vfile is built by people just like you!
Check out contributing.md
for ways to get started.
This project has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.
Want to chat with the community and contributors? Join us in spectrum!
Have an idea for a cool new utility or tool?
Thatβs great!
If you want feedback, help, or just to share it with the world you can do so by
creating an issue in the vfile/ideas
repository!
Acknowledgments
The initial release of this project was authored by @wooorm.
Thanks to @contra, @phated, and others for their work on Vinyl, which was a huge inspiration.
Thanks to @brendo, @shinnn, @KyleAMathews, @sindresorhus, and @denysdovhan for contributing commits since!
License
MIT Β© Titus Wormer