html-to-text

repository·master·Indexed 23 days ago

https://github.com/html-to-text/node-html-to-text

An advanced Node.js library and CLI for converting HTML to plain text or Markdown. It features a customizable selector system for element formatting, support for tables, word wrapping, and Unicode. The package includes a CLI tool (@html-to/text-cli) that supports JSON configuration files, predefined presets (human and machine), and terminal pipes, as well as a specialized Markdown converter (@html-to/md).

Tokens
4.6K
Snippets
10
Records
27
Agent score
83%

What's inside html-to-text

  1. Overview of html-to-text features

    master

    The html-to-text library is an advanced converter that parses HTML and returns formatted text. Key features include:

    • Support for inline and block-level tags.
    • Table conversion including colspans and rowspans.
    • Link conversion that preserves both text and href attributes.
    • Word wrapping.
    • Unicode support.
    • Extensive customization options.
    • A separate CLI tool available via @html-to/text-cli.
  2. Customize element formatting with selectors

    master

    The selectors option is an array of objects that acts like a loose approximation of a stylesheet. Each object defines a selector (CSS-like) and a format (the formatter to use).

    Selector Rules:

    • Highest specificity selector is used for matches.
    • If specificity is equal, the last selector in the array is used.
    • All entries with the same selector value are merged at compile time (last defined properties win).
    • User-defined entries are appended after predefined ones.
    • Unlike CSS, values from different matched selectors are not merged at conversion time; only the single best match is used.

    Supported Selector Syntax:

    • * (universal), tag names (div), .class, #id, [attribute], [attribute=value] (including operators and case modifiers).
    • Combinators: + and >.
    • Pseudo-classes: :empty, :first-child, :last-child, :only-child, :any-link.
    const { convert } = require('html-to-text');
    
    const html = '<a href="/page.html">Page</a><a href="!#" class="button">Action</a>';
    const text = convert(html, {
      selectors: [
        { selector: 'a', options: { baseUrl: 'https://example.com' } },
        { selector: 'a.button', format: 'skip' }
      ]
    });
    console.log(text); // Page [https://example.com/page.html]
  3. Install @html-to/text-cli

    master

    Install the @html-to/text-cli package globally via npm to use the html-to-text command in your terminal.

    Important Note on Name Collisions:

    • Ensure that any old versions of the html-to-text package are uninstalled globally to avoid command conflicts.
    • Only use the namespaced package @html-to/text-cli to avoid confusion with an unrelated abandoned CLI package.
    npm i -g @html-to/text-cli
  4. Use the html-to-text CLI

    master

    The CLI converts HTML from stdin to plain text on stdout. You can pass converter options as command-line arguments.

    Bash/Linux/macOS usage:

    cat ./input.html | html-to-text [commands...] [keys and values...] > ./output.txt

    PowerShell usage: In PowerShell, use the .cmd wrapper (html-to-text.cmd) because the .ps1 wrapper may not work correctly with stdin.

    Get-Content .\input.html | html-to-text.cmd [commands...] [keys and values...] > .\output.txt
  5. ESLint configuration for node-html-to-text

    master
    The project uses ESLint with a flat configuration (eslint.config.mjs). The configuration enforces strict coding standards, including JSDoc requirements, import ordering, and specific file naming conventions. It applies different rules based on file patterns (e.g., CLI packages, examples, tests, and Rollup configs).
  6. CLI Argument Syntax and Transformation

    master

    The CLI uses a specific syntax for passing options:

    • Key Transformation: The CLI automatically converts kebab-case arguments to camelCase for the underlying html-to-text engine. For example, an argument like --word-wrap will be passed to the engine as wordWrap.
    • Unkeying: When outputting or inspecting, the CLI can convert camelCase keys back to kebab-case.
    • JSON Output: Use the --json flag to output the result in JSON format (via the internal business logic).
    • Merging Options: Use the --merge flag to compose CLI options.
  7. Configure general conversion options

    master

    The following general options are available in the options object:

    OptionDefaultDescription
    baseElementsDescribes which parts of the input document are converted and in what order. Includes selectors (array), orderBy ('selectors' or 'occurrence'), and returnDomByDefault (boolean).
    decodeEntitiestrueWhether to decode HTML entities.
    encodeCharacters{}A dictionary mapping characters to escape sequences.
    formatters{}Object containing custom formatting functions.
    limitsLimits output for large documents. Includes ellipsis (string), maxBaseElements (number), maxChildNodes (number), maxDepth (number), and maxInputLength (number).
    longWordSplitControls wrapping of long words. Includes wrapCharacters (array) and forceWrapOnLimit (boolean).
    preserveNewlinesfalseIf true, preserves \n from input HTML instead of collapsing them into spaces.
    selectors[]Array of objects describing how different HTML elements should be formatted.
    whitespaceCharacters' \t\r\n\f\u200b'String of characters recognized as HTML whitespace.
    wordwrap80Number of characters after which a line break follows. Set to null or false to disable.
  8. Use the @html-to/text-cli command line interface

    master

    The @html-to/text-cli is an advanced HTML to plain text converter. It operates via standard streams: it reads HTML input from stdin and writes the resulting plain text to stdout.

    All options available in the html-to-text package can be expressed as CLI arguments (except for function-based options).

    Basic Usage Pattern:

    cat input.html | html-to-text > output.txt
  9. Pass custom metadata to formatters

    master

    If you need to provide extra information to your custom formatters, you can pass a metadata object as the last argument to convert() or the function returned by compile(). This object is accessible within your formatter via builder.metadata.

    import { compile, convert } from 'html-to-text';
    
    // For batch use:
    const compiledConvert = compile(options);
    let text = compiledConvert(html, metadata);
    
    // For single use:
    let text = convert(html, options, metadata);
  10. Override formatting with custom formatters

    master

    You can define custom formatting logic by adding functions to the formatters object in your options. A custom formatter is a function with four arguments:

    1. elem: The HTML element being processed.
    2. walk: A recursive function to process children: walk(elem.children, builder).
    3. builder: A BlockTextBuilder object used to manipulate the output state.
    4. formatOptions: Options specified for that specific tag.

    To use a custom formatter, assign it to a selector in the selectors array.

    const { convert } = require('html-to-text');
    
    const html = '<foo>Hello World</foo>';
    const text = convert(html, {
      formatters: {
        'fooBlockFormatter': function (elem, walk, builder, formatOptions) {
          builder.openBlock({ leadingLineBreaks: formatOptions.leadingLineBreaks || 1 });
          walk(elem.children, builder);
          builder.addInline('!');
          builder.closeBlock({ trailingLineBreaks: formatOptions.trailingLineBreaks || 1 });
        }
      },
      selectors: [
        {
          selector: 'foo',
          format: 'fooBlockFormatter',
          options: { leadingLineBreaks: 1, trailingLineBreaks: 1 }
        }
      ]
    });
    console.log(text); // Hello World!