unified document APIs.
npm install unified-doc
A unified API to easily work with documents of supported content types.
;// easily initialize a `doc` instance with access to document APIs.// any supported content type benefits from the same APIs.const doc =;// export source file;// export file as html;// export file as text (only textContent is extracted);// easily search on a doc;// retrieve just the textContent of the document;// retrieve the `hast` (syntax tree) representation of the document;// compile the document to use the results for rendering; // vfile instance
doc refers to an instance of
doc instance that provides a set of useful methods to working with documents. Any supported content type benefits from the API methods. As of the time of this writing,
unified-doc supports the following content types:
- parsing most code file content (e.g.
.py) into code blocks.
Future content types such as
// initialize as markdown contentconst doc1 =;// initialize as html contentconst doc2 =;// initialize as code contentconst doc3 =;
doc instance can be configured by providing other properties. This will be elaborated more in the
Returns the results of the compiled content based on the
compiler attached to the
doc. The results are stored as a
VFile, and can be used by various renderers. By default, a HTML string compiler is used, and stringifed HTML is returned by this method.
FileData for the specified extension. This is a useful way to convert and output different file formats. Supported extensions include
.xml. If no extension is provided, the source file should be returned. Future extensions can be implemented, providing a powerful way to convert file formats for any supported content type
const doc =;// export source file;// export file as html;// export file as markdown;// export file as text (only textContent is extracted);// export file as html-compatible xml;
hast representation of the content. This content representation is used internally by the
doc, but it can also be used by any
;const hast = doc;const mdast = ;
SearchResultSnippet based on the provided
query string and search
options. Uses the
searchAlogrithm attached to the
doc for when executing a search against the
textContent of a
const doc =;;
textContent of a
doc. This content is the concatenated value of all text nodes under a
doc, and is used by many internal APIs (marking, searching).
const doc =;;
doc instance with the following
The following is an example of a doc with custom configurations.
const doc =;
/*** An object used by a mark algorithm to mark text nodes based on text offset positions*//*** Mapping of mimeTypes to unified parsers.* New parsers can be introduced and existing parsers can be overwritten.* Parsers are provided in the `PluggableList` interface.* Parsers may consist of multiple steps.*/// see https://github.com/syntax-tree/hast-util-sanitize;/*** Unified search options affecting any attached search algorithm.*/
The following enums indicate
mimeTypes for supported parsers and
extensionTypes for supported file conversion targets.