defuddle

repository·main·Indexed 26 days ago

https://github.com/kepano/defuddle

A library and CLI tool for extracting article content and metadata from web pages. It provides a Node.js API via the Defuddle class and parseSource function, a command-line interface for parsing URLs or HTML files into formats like Markdown and JSON, and a Cloudflare Worker implementation (defuddle.md) that acts as an HTTP API wrapper.

Tokens
2.1K
Snippets
3
Records
11
Agent score
94%

What's inside defuddle

  1. Use defuddle.md as an HTTP API

    main

    defuddle.md is a Cloudflare Worker that provides an HTTP API for cleaning web content. You can retrieve cleaned content from any URL by passing that URL as the path in your request.

    Note: When testing locally, use curl rather than a web browser.

    curl https://defuddle.md/https://example.com/article
  2. Run defuddle.md locally

    main

    To run the defuddle.md Cloudflare Worker on your local machine, follow these steps:

    1. Install the Cloudflare CLI, Wrangler:
      npm install -g wrangler
    2. Navigate to the website directory and start the development server:
      cd website
      npx wrangler dev
    3. Test the local server using curl:
      curl http://localhost:8787/https://example.com/article

    If changes to the source code are not reflected in your local environment, clear the Wrangler cache by deleting the .wrangler directory:

    rm -rf .wrangler
    npm install -g wrangler
    cd website
    npx wrangler dev
  3. Configure Defuddle options

    main

    When calling the Defuddle extraction function, you can pass a DefuddleOptions object to customize the extraction behavior. Key options include:

    • url: The URL of the page to be parsed.
    • markdown: Convert output to Markdown (defaults to false).
    • separateMarkdown: Include Markdown in the response (defaults to false).
    • contentSelector: A CSS selector to use as the main content element, bypassing auto-detection.
    • language: Preferred language for extraction (BCP 47 tag, e.g., 'en'). Used in Accept-Language headers and for selecting transcript tracks.
    • includeReplies: Controls reply inclusion. Can be 'extractors' (default, uses site-specific extractors), true (all replies/comments), or false (exclude all).
    • debug: Enable debug logging (defaults to false).
    • profile: Enable per-step profiling; timings are returned in result.profile (defaults to false).
    • fetch: Custom fetch function override for all HTTP requests (useful for proxying in restricted environments).

    Removal & Cleaning Options (Defaults are true unless noted):

    • removeExactSelectors: Remove elements matching exact selectors (e.g., ads).
    • removePartialSelectors: Remove elements matching partial selectors.
    • removeHiddenElements: Toggle removal of hidden elements.
    • removeLowScoring: Toggle content scoring/removal.
    • removeSmallImages: Toggle small image removal.
    • removeContentPatterns: Toggle content-based pattern removal (read time, boilerplate, etc.).
    • removeImages: Remove images (defaults to false).
    • standardize: Toggle HTML standardization (footnotes, headings, code blocks, etc.).

    Extraction Logic:

    • useAsync: Allow async extractors to fetch content from third-party APIs if local HTML extraction fails (defaults to true).
  4. Understand the DefuddleResponse object

    main

    The DefuddleResponse object contains the extracted content and associated metadata. It extends DefuddleMetadata and includes:

    • content: The extracted HTML content.
    • contentMarkdown: (Optional) The extracted Markdown content.
    • extractorType: (Optional) The type of extractor used.
    • metaTags: (Optional) An array of MetaTagItem objects.
    • debug: (Optional) DebugInfo containing the contentSelector used and a list of removals performed.
    • profile: (Optional) A Record<string, number> containing step timings if profile: true was set in options.
    • variables: (Optional) A dictionary of ExtractorVariables (key-value strings).
  5. Parse source programmatically with `parseSource`

    main

    If you are integrating defuddle into a Node.js application, you can use the parseSource function to process HTML from various inputs.

    Function Signature

    parseSource(source?: string, options: ParseOptions, input?: NodeJS.ReadStream): Promise<ParseResult>

    Parameters

    • source (optional): A string representing a URL, a file path, or - for stdin. If undefined, it defaults to reading from stdin.
    • options: A ParseOptions object (see below).
    • input (optional): A NodeJS.ReadStream. Defaults to process.stdin.

    ParseOptions

    KeyTypeDescription
    outputstringPath to write the output file
    markdownbooleanConvert content to markdown
    mdbooleanAlias for markdown
    jsonbooleanOutput as JSON with metadata
    debugbooleanEnable debug mode
    propertystringExtract a specific property (e.g., title)
    langstringPreferred language (BCP 47)
    userAgentstringCustom User-Agent header
    frontmatterbooleanPrepend YAML frontmatter

    Returns

    Returns a Promise resolving to a ParseResult object:

    interface ParseResult {
      output: string;
    }
  6. Reference DebugInfo and DebugRemoval

    main

    When debug: true is enabled, the response includes DebugInfo to help troubleshoot content extraction:

    export interface DebugRemoval {
    	step: string;
    	selector?: string;
    	reason?: string;
    	text: string;
    }
    
    export interface DebugInfo {
    	contentSelector: string;
    	removals: DebugRemoval[];
    }
  7. Reference DefuddleMetadata fields

    main

    The DefuddleMetadata interface defines the standard metadata fields returned in a successful extraction:

    FieldTypeDescription
    titlestringThe title of the content
    descriptionstringThe description of the content
    domainstringThe domain of the source
    faviconstringURL to the site favicon
    imagestringURL to the main image
    languagestringThe language of the content
    parseTimenumberTime taken to parse
    publishedstringPublication date/time
    authorstringAuthor of the content
    sitestringThe site name
    schemaOrgDataanySchema.org structured data
    wordCountnumberTotal word count
  8. Use the defuddle CLI to parse content

    main

    The defuddle CLI allows you to extract article content from a web URL, a local HTML file, or via stdin.

    Commands

    parse [source]

    Parses HTML content from the provided source.

    Arguments:

    • source: An HTML file path, a URL (starting with http:// or https://), or - to read from stdin.

    Options:

    • -o, --output <file>: Specify the output file path. If omitted, results are printed to stdout.
    • -m, --markdown or --md: Convert the extracted content to markdown format.
    • -j, --json: Output the result as a JSON object containing metadata (title, description, domain, etc.) and content.
    • -f, --frontmatter: Prepend YAML frontmatter (containing title, author, source, etc.) to the output.
    • -p, --property <name>: Extract only a specific property from the result (e.g., title, description, domain, wordCount).
    • --debug: Enable debug mode.
    • -l, --lang <code ext>: Set the preferred language using BCP 47 codes (e.g., en, fr, ja).
    • -u, --user-agent <string>: Provide a custom User-Agent header for HTTP requests to avoid 403/FORBIDDEN errors.

    Examples

    Parse a URL and output to a file as Markdown:

    defuddle parse https://example.com -m -o article.md

    Pipe HTML from stdin:

    cat page.html | defuddle parse -

    Extract only the title as JSON:

    defuddle parse https://example.com --property title
  9. Use Defuddle types

    main

    The library exports several key types for configuring the engine and handling responses. You can import these directly from the package entry point:

    • DefuddleOptions: Configuration object for initializing Defuddle.
    • DefuddleResponse: The shape of the data returned after processing.
    • DefuddleMetadata: Metadata associated with the defuddling process.
    • DebugInfo: Information used for debugging the engine's state.
    • DebugRemoval: Information regarding what content was removed during processing.
    import type { 
      DefuddleOptions, 
      DefuddleResponse, 
      DefuddleMetadata, 
      DebugInfo, 
      DebugRemoval 
    } from 'defuddle';