/** * Create a virtual file from a description. * If `options` is a string or a buffer, it’s used as the * path. In all other cases, the options are passed through * to `vfile()`. * * @param {string|buffer|Object} description - File description. * @param {Object|string} [options] - `fs.readFile` options. */ function toVFile(options) { if (string(options) || buffer(options)) { options = {path: String(options)}; } return vfile(options); }
/* Data management. * Getter / setter for processor-specific informtion. */ function data(key, value) { if (string(key)) { /* Set `key`. */ if (arguments.length === 2) { assertUnfrozen('data', frozen); namespace[key] = value; return processor; } /* Get `key`. */ return (own.call(namespace, key) && namespace[key]) || null; } /* Set space. */ if (key) { assertUnfrozen('data', frozen); namespace = key; return processor; } /* Get space. */ return namespace; }
/** * Data management. * * Getter / setter for processor-specific informtion. * * @param {string} key - Key to get or set. * @param {*} value - Value to set. * @return {*} - Either the operator on processor in * setter mode; or the value stored as `key` in * getter mode. */ function data(key, value) { assertConcrete('data'); if (string(key)) { /* Set `key`. */ if (arguments.length === 2) { namespace[key] = value; return processor; } /* Get `key`. */ return (has(namespace, key) && namespace[key]) || null; } /* Get space. */ if (!key) { return namespace; } /* Set space. */ namespace = key; return processor; }
function h(tagName, properties, children) { var tag, props, childNodes, key if (!children) { if (isChildren(properties)) { children = properties properties = undefined } } tag = parseTag(tagName, properties) if (!isString(tag)) { props = tag.properties tag = tag.tagName } else { props = properties } if (isArray(children)) { var len = children.length for (var i = 0; i < len; i++) { var child = children[i] if (isString(child)) { children[i] = new VText(child) } } childNodes = children } else if (isString(children)) { childNodes = [new VText(children)] } else if (isChild(children)) { childNodes = [children] } if (props && "key" in props) { key = props.key delete props.key } return new VNode(tag, props, childNodes, key) }
function each(file) { if (string(file)) { if (magic(file)) { expected++ glob(file, {cwd: cwd}, one) } else { /* `relative` to make the paths canonical. */ file = relative(cwd, resolve(cwd, file)) || '.' paths.push(file) } } else { file.cwd = cwd file.path = relative(cwd, file.path) file.history = [file.path] paths.push(file) } }
/** * Stringify a Unist node representation of a file * (in string or VFile representation) into a string * using the `Compiler` on the processor. * * @param {Node} node - Unist node. * @param {(string|VFile)?} [file] - File representation. * @param {Object?} [options] - Configuration. * @return {string} - String representation. */ function stringify(node, file, options) { assertConcrete('stringify'); assertCompiler('stringify'); assertNode(node); if ( !options && !string(file) && !buffer(file) && !(typeof file === 'object' && 'messages' in file) ) { options = file; file = null; } return new processor.Compiler(vfile(file), options, processor).compile(node); }
function applyPatch(vpatch, domNode, renderOptions) { var vNode = vpatch.vNode var patch = vpatch.patch if (patch == null) { return removeNode(domNode, vNode) } else if (vNode == null) { return insertNode(domNode, patch, renderOptions) } else if (isString(patch)) { return stringPatch(domNode, vNode, patch, renderOptions) } else if (isWidget(patch)) { return widgetPatch(domNode, vNode, patch, renderOptions) } else if (isVNode(patch)) { return vNodePatch(domNode, vNode, patch, renderOptions) } else { applyProperties(domNode, patch, vNode.propeties) return domNode } }
/* Construct a new file. */ function VFile(options) { var prop; var index; var length; if (!options) { options = {}; } else if (string(options) || buffer(options)) { options = {contents: options}; } else if ('message' in options && 'messages' in options) { return options; } if (!(this instanceof VFile)) { return new VFile(options); } this.data = {}; this.messages = []; this.history = []; this.cwd = process.cwd(); /* Set path related properties in the correct order. */ index = -1; length = order.length; while (++index < length) { prop = order[index]; if (has(options, prop)) { this[prop] = options[prop]; } } /* Set non-path related properties. */ for (prop in options) { if (order.indexOf(prop) === -1) { this[prop] = options[prop]; } } }
function filePath(file) { return string(file) ? file : file.path }
function base(file) { return string(file) ? basename(file) : file.basename }
function each(file) { var ext = string(file) ? extname(file) : file.extname var part /* Normalise globs. */ if (string(file)) { file = file.split('/').join(path.sep) } part = base(file) if (nested && (hidden(part) || part === 'node_modules')) { return } expected++ statAndIgnore(file, options, handle) function handle(error, result) { var ignored = result && result.ignored var dir = result && result.stats && result.stats.isDirectory() if (ignored && (nested || silent)) { return one(null, []) } if (!ignored && dir) { return readdir(resolve(cwd, filePath(file)), directory) } if ( nested && !dir && extensions.length !== 0 && extensions.indexOf(ext) === -1 ) { return one(null, []) } file = vfile(file) file.cwd = cwd if (ignored) { try { file.fail('Cannot process specified file: it’s ignored') } catch (error2) {} } if (error && error.code === 'ENOENT') { try { file.fail( error.syscall === 'stat' ? 'No such file or directory' : error ) } catch (error2) {} } one(null, [file]) } function directory(error, basenames) { var file /* istanbul ignore if - Should not happen often: the directory * is `stat`ed first, which was ok, but reading it is not. */ if (error) { file = vfile(filePath(file)) file.cwd = cwd try { file.fail('Cannot read directory') } catch (error2) {} one(null, [file]) } else { search(basenames.map(concat), xtend(options, {nested: true}), one) } } /* Error is never given. Always given `results`. */ function one(_, results) { /* istanbul ignore else - always given. */ if (results) { files = files.concat(results) } actual++ if (actual === expected) { next(null, files) } } function concat(value) { return join(filePath(file), value) } }
get: function () { return string(this.path) ? path.dirname(this.path) : undefined; },
get: function () { return string(this.path) ? path.basename(this.path, this.extname) : undefined; },
function isChildren(x) { return isArray(x) || isString(x) || isChild(x) }
/* Assert `node` is a Unist node. */ function assertNode(node) { if (!node || !string(node.type)) { throw new Error('Expected node, got `' + node + '`'); } }
/** * Check if `node` is a Unist node. * * @param {*} node - Value. * @return {boolean} - Whether `node` is a Unist node. */ function isNode(node) { return node && string(node.type) && node.type.length !== 0; }