@such-code/html-parser-utils
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

@such-code/html-parser-utils

Installation

npm i -D @such-code/html-parser-utils

Generic utils to simplify work with htmlparser2 library.

HtmlNode

Contains definitions for html elements.

DomRenderer

import { Node } from 'htmlparser2';

export declare type DomRendererOptions = {};

/**
 * Helps rendering HTML AST.
 */
export declare class DomRenderer {
    /**
     * Wraps the attribute in single or double quotes.
     * @param $value
     * @static
     */
    static quote($value: string): string;
    
    constructor(options?: DomRendererOptions);

    /**
     * Completely render one element including children.
     * @param $node
     */
    renderNode($node: Node): string;

    /**
     * Renders array of elements.
     * @param $elements
     */
    renderNodes($elements: Array<Node>): string;
}

Helper methods

import { ParserOptions } from 'htmlparser2/lib';
import { DataNode, DomHandlerOptions, Element, Node, NodeWithChildren } from 'domhandler/lib';
import { ProcessingInstruction } from 'domhandler/lib/node';

/**
 * Escapes special chars and converts string to RegExp matching that string.
 * @param $string
 * @returns RegExp
 */
export declare function stringToRegExp($string: string): RegExp;

/**
 * Type guard to check if $value is Node.
 * @param $value
 * @returns boolean
 */
export declare function isNode($value: any): $value is Node;

/**
 * Type guard to check if $value is DataNode.
 * @param $value
 * @returns boolean
 */
export declare function isDataNode($value: any): $value is DataNode;

/**
 * Type guard to check if $value is ProcessingInstruction.
 * @param $value
 * @returns boolean
 */
export declare function isProcessingInstruction($value: any): $value is ProcessingInstruction;

/**
 * Type guard to check if $value is NodeWithChildren.
 * @param $value
 * @returns boolean
 */
export declare function isNodeWithChildren($value: any): $value is NodeWithChildren;

/**
 * Type guard to check if $value is Element.
 * @param $value
 * @returns boolean
 */
export declare function isElement($value: any): $value is Element;

/**
 * Converts string to Array of Nodes using domhandler library.
 * @param $data
 * @param $parserOptions optional ParserOptions. Default values for `lowerCaseTags` and `lowerCaseAttributeNames` are
 * set to false.
 * @param $domHandlerOptions optional DomHandlerOptions. Override default values for DomHandler, could be used for html
 * optimization.
 * @returns Promise<Array<Node>>
 */
export declare function stringToDom($data: string, $parserOptions?: ParserOptions, $domHandlerOptions?: DomHandlerOptions): Promise<Array<Node>>;

Readme

Keywords

none

Package Sidebar

Install

npm i @such-code/html-parser-utils

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

41.1 kB

Total Files

19

Last publish

Collaborators

  • such-code