unified-doc
unified document APIs.
Install
npm install unified-doc
Use
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
API
Doc(options)
doc.compile()
doc.file([extension])
doc.parse()
doc.search(query[, options])
doc.textContent()
options
enums
A doc
refers to an instance of unified-doc
.
Doc(options)
Initialize a 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:
.html
.md
- parsing most code file content (e.g.
.js
,.py
) into code blocks.
Future content types such as .csv
, .xml
, .docx
, .pdf
will be supported.
// initialize as markdown contentconst doc1 = ;// initialize as html contentconst doc2 = ;// initialize as code contentconst doc3 = ;
The doc
instance can be configured by providing other properties. This will be elaborated more in the options
section.
doc.compile()
Interface
;
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.
doc.file([extension])
Interface
;
Returns FileData
for the specified extension. This is a useful way to convert and output different file formats. Supported extensions include '.html'
, '.txt'
, .md
, .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
Example
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;
Related interfaces
doc.parse()
Interface
;
Returns the hast
representation of the content. This content representation is used internally by the doc
, but it can also be used by any hast
utility.
Example
; const hast = doc;const mdast = ;
doc.search(query[, options])
Interface
;
Returns 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 doc
.
Example
const doc = ; ;
Related interfaces
doc.textContent()
Interface
;
Returns the 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).
Example
const doc = ; ;
options
Configure the doc
instance with the following options
:
Interface
Example
The following is an example of a doc with custom configurations.
const doc = ;
Related interfaces
/** * 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. */
enums
The following enums indicate mimeTypes
for supported parsers and extensionTypes
for supported file conversion targets.
; ;