node-html-markdown

repository·master·Indexed 42 days ago

https://github.com/crosstype/node-html-markdown

A high-performance HTML to Markdown cross-compiler compatible with Node.js and browser environments. Version 2.0.0 features a customizable translator system, support for batch processing via FileCollection, and extensive configuration options through NodeHtmlMarkdownOptions to control delimiters, block elements, and newline handling.

Tokens
5.6K
Snippets
17
Records
25
Agent score
54%

What's inside node-html-markdown

  1. Extend translation with Custom Translators

    master

    For advanced customization, you can provide custom translators to handle specific elements.

    • General Elements: Can be added during instance creation via the constructor or modified via the NodeHtmlMarkdown#translators property.
    • Code Blocks: Elements within <pre><code> blocks can be handled by NodeHtmlMarkdown#codeBlockTranslators.

    Refer to translator.ts and config.ts in the source for detailed TranslatorConfig and the list of defaultTranslators.

  2. Handle newlines and paragraph spacing

    master

    The library follows standard Markdown conventions for spacing:

    • Paragraphs: HTML block elements like <p>, <div>, or <h1> are surrounded by blank lines in the output.
    • Line Breaks: The <br> tag creates a single line break using the Markdown line break syntax (two trailing spaces).

    To control the amount of whitespace, use the maxConsecutiveNewlines option. A value of 1 creates compact output, while higher values (e.g., 10) preserve more spacing from the source HTML.

    // Default behavior - limits to 3 consecutive newlines
    const html = `<p>a</p>${'<br>'.repeat(10)}<p>b</p>`;
    const markdown = NodeHtmlMarkdown.translate(html);
    
    // Allow more consecutive newlines
    const markdown2 = NodeHtmlMarkdown.translate(html, {
      maxConsecutiveNewlines: 10
    });
  3. Configure NodeHtmlMarkdownOptions

    master

    The NodeHtmlMarkdownOptions interface allows you to customize the output format and parsing behavior.

    Key options include:

    • preferNativeParser: Use native window.DOMParser if available (default: false).
    • codeFence: The character used for code block fences (default: ```).
    • bulletMarker: The character used for bullets (default: *).
    • codeBlockStyle: Set to 'indented' or 'fenced' (default: 'fenced').
    • emDelimiter, strongDelimiter, strikeDelimiter: Custom delimiters for emphasis, strong, and strike (defaults: _, **, ~~).
    • ignore: An array of element names to be ignored (does not parse children).
    • blockElements: An array of elements to be treated as blocks (surrounded with blank lines).
    • maxConsecutiveNewlines: Limits consecutive newlines (default: 3).
    • keepDataImages: If true, keeps images with data: URI (up to 1MB each).
    • useLinkReferenceDefinitions: If true, moves URLs to the bottom using link reference definitions.
    • useInlineLinks: If true, wraps URLs in < > instead of []() syntax (default: true).
    export interface NodeHtmlMarkdownOptions {
      preferNativeParser: boolean,
      codeFence: string,
      bulletMarker: string,
      codeBlockStyle: 'indented' | 'fenced',
      emDelimiter: string,
      strongDelimiter: string,
      strikeDelimiter: string,
      ignore?: string[],
      blockElements?: string[],
      maxConsecutiveNewlines: number,
      lineStartEscape: [ pattern: RegExp, replacement: string ],
      globalEscape: [ pattern: RegExp, replacement: string ],
      textReplace?: [ pattern: RegExp, replacement: string ][],
      keepDataImages?: boolean,
      useLinkReferenceDefinitions?: boolean,
      useInlineLinks?: boolean
    }
  4. Customize element handling with Translators

    master

    The converter uses a translator system to map HTML elements to Markdown. You can extend or override the defaultTranslators by providing a TranslatorConfigObject.

    Translators can define:

    • prefix: Text to prepend to the element's content.
    • postfix: Text to append to the element's content.
    • content: Static content to replace the element with.
    • surroundingNewlines: Number of newlines to add around the element.
    • noEscape: Whether to skip escaping the content.
    • preserveWhitespace: Whether to preserve whitespace.
    • recurse: Whether to process child nodes.
    • childTranslators: A nested TranslatorConfigObject for elements inside the current one.
    • postprocess: A function to transform the resulting string after translation.
  5. Use NodeHtmlMarkdown for single-use translation

    master

    If you only need to perform a single conversion, use the static NodeHtmlMarkdown.translate method. You can pass a single HTML string or a FileCollection object containing multiple files.

    import { NodeHtmlMarkdown } from 'node-html-markdown'
    
    // Single file
    NodeHtmlMarkdown.translate(
      /* html */ `<b>hello</b>`,
      /* options (optional) */ {},
      /* customTranslators (optional) */ undefined,
      /* customCodeBlockTranslators (optional) */ undefined
    );
    
    // Multiple files
    NodeHtmlMarkdown.translate(
      /* FileCollection */ { 
        'file1.html': `<b>hello</b>`, 
        'file2.html': `<b>goodbye</b>` 
      },
      /* options (optional) */ {},
      /* customTranslators (optional) */ undefined,
      /* customCodeBlockTranslators (optional) */ undefined
    );
  6. Use NodeHtmlMarkdown instances for repeated translation

    master

    For better performance when performing multiple conversions, create an instance of NodeHtmlMarkdown. Reusing an instance is faster than calling the static method repeatedly.

    import { NodeHtmlMarkdown } from 'node-html-markdown'
    
    const nhm = new NodeHtmlMarkdown(
      /* options (optional) */ {},
      /* customTransformers (optional) */ undefined,
      /* customCodeBlockTranslators (optional) */ undefined
    );
    
    // Single file
    hm.translate(/* html */ `<b>hello</b>`);
    
    // Multiple Files
    hm.translate(
      /* FileCollection */ { 
        'file1.html': `<b>hello</b>`, 
        'file2.html': `<b>goodbye</b>` 
      }
    );
  7. Configure NodeHtmlMarkdownOptions

    master

    The NodeHtmlMarkdownOptions interface allows you to customize how HTML is converted to Markdown. You can control delimiters for emphasis, list indentation, code block styles, and how specific HTML elements are handled (e.g., ignoring certain tags or treating others as block elements). It also provides advanced control over newline handling, escaping patterns, and link formatting.

    const options: NodeHtmlMarkdownOptions = {
      codeFence: '```',
      bulletMarker: '-',
      indent: '    ',
      codeBlockStyle: 'fenced',
      maxConsecutiveNewlines: 2,
      useLinkReferenceDefinitions: true
    };
  8. Convert HTML to Markdown with NodeHtmlMarkdown

    master

    The primary entrypoint for converting HTML to Markdown is the NodeHtmlMarkdown class. It is used to process HTML strings or files into Markdown format based on provided options.

    import { NodeHtmlMarkdown } from 'node-html-markdown';
    
    const converter = new NodeHtmlMarkdown();
    const markdown = converter.translate('<h1>Hello World</h1>');
  9. Manage tag translators with TranslatorCollection

    master

    The TranslatorCollection class is used to store and manage mappings between HTML tag names and their corresponding TranslatorConfig or TranslatorConfigFactory.

    Note that tag names are treated as case-insensitive (stored as uppercase).

    Methods:

    • set(keys: string, config: TranslatorConfig | TranslatorConfigFactory): Adds or updates translators. keys can be a comma-separated list of tags (e.g., 'DIV,SPAN').
    • get(key: string): Retrieves the config for a specific tag.
    • remove(keys: string): Removes translators for the specified comma-separated tags.
    • entries(): Returns an array of [tagName, config] pairs.
    import { TranslatorCollection } from './translator';
    
    const collection = new TranslatorCollection();
    
    // Set multiple tags at once
    collection.set('DIV,SPAN,P', { prefix: ' ' });
    
    // Get a specific config
    const divConfig = collection.get('div');
    
    // Remove specific tags
    collection.remove('SPAN');
  10. Initialize NodeHtmlMarkdown for HTML to Markdown conversion

    master

    The NodeHtmlMarkdown class is the primary entry point for converting HTML to Markdown. You can instantiate it with optional configuration to customize how specific HTML elements are translated.

    Constructor Parameters

    • options?: Options: Configuration for general behavior (e.g., ignore elements, blockElements, textReplace).
    • customTranslators?: TranslatorConfigObject: A mapping of HTML elements to their translation configurations to override or extend default behavior.
    • customCodeBlockTranslators?: TranslatorConfigObject: A mapping of HTML elements to their translation configurations specifically for code blocks.

    When you provide custom translators, they are merged with the default ones. The class also maintains specialized TranslatorCollection instances for aTagTranslators, tableTranslators, tableRowTranslators, and tableCellTranslators.

    import { NodeHtmlMarkdown } from 'node-html-markdown';
    
    const converter = new NodeHtmlMarkdown({
      ignore: ['script', 'style'],
      blockElements: ['div']
    }, {
      // customTranslators
      'b': { surroundingNewlines: 1 }
    });
    
    const markdown = converter.translate('<div>Hello <b>World</b></div>');