๐ Markdown Harvest
A Rust crate designed to extract, clean, and convert web content from URLs found in text messages into clean Markdown format. Originally created as an auxiliary component for Retrieval-Augmented Generation (RAG) solutions to process URLs submitted by users.
๐ Table of Contents
- Overview
- Features
- Quick Start
- Installation
- Usage Examples
- API Documentation
- Content Processing Pipeline
- Supported Platforms
- Contributing
- License
- Changelog
Overview
Markdown Harvest was initially developed as part of a Retrieval-Augmented Generation (RAG) system where users submit text containing URLs, and the system needs to extract meaningful content from those URLs for further analysis or processing. This crate handles the extraction, cleaning, and structuring of web content automatically.
๐ฏ Why Markdown Harvest?
- ๐ Built for AI/RAG Systems: Specifically designed for content preprocessing in AI workflows
- ๐งน Smart Content Extraction: Removes ads, navigation, and irrelevant elements automatically
- ๐ Markdown Output: Clean, structured Markdown perfect for LLM processing
- ๐ Batch Processing: Handle multiple URLs efficiently in a single operation
- ๐ก๏ธ Robust Error Handling: Gracefully handles network issues and invalid URLs
Use Case Process Flow
graph LR
A[User Input] --> B{Identifies URLs}
B -->|Yes| C[Retrieves HTTP Content]
C --> D[Processes & Extracts Data]
D --> E[Augments Context]
E --> F[Generates Response with Model]
B -->|No| F
F -->|Contextualized response| A
โจ Features
- ๐ URL Detection: Automatically identifies HTTP/HTTPS URLs in text using regex patterns
- ๐ฏ Smart Content Extraction: Extracts only relevant content from HTML
<body>elements - ๐ HTML to Markdown Conversion: Converts HTML content to clean, readable Markdown while preserving structure and removing unwanted elements
- ๐งน Content Cleaning: Removes JavaScript, CSS, advertisements, and navigation elements
- ๐ค Multi-Platform User Agents: Rotates between different browser user agents to avoid detection
- โก Async/Blocking Support: Choose between async and blocking HTTP requests
- ๐ก๏ธ Error Handling: Graceful handling of network errors and invalid URLs
- ๐ Clean Text Output: Normalizes whitespace and removes common non-content patterns
๐ Quick Start
use MarkdownHarvester;
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
๐ Usage Examples
๐ Basic Usage
use MarkdownHarvester;
๐ป Interactive CLI Mode
The crate provides an interactive CLI mode for testing:
Then enter text containing URLs when prompted.
๐ง Library Integration
er
use ;
// Use different user agents
let user_agent = random_windows;
println!;
// Process multiple URLs from text
let input = "Articles: https://site1.com and https://site2.com";
let results = get_hyperlinks_content;
๐ API Documentation
Core Functions
// Main function to extract content from URLs in text
get_hyperlinks_content // User agent utilities
random_windows
Supported Platforms & User Agents
The crate includes user agents for:
- Windows: Chrome, Firefox, Edge
- macOS: Chrome, Safari, Firefox
- Linux: Chrome, Firefox
- Android: Chrome, Firefox
- iOS: Safari, Chrome
๐๏ธ Dependencies
reqwest- HTTP client with blocking supportscraper- HTML parsing and CSS selector enginehtml2md- Intelligent HTML to Markdown conversionregex- URL detection and content filteringrand- Random user agent selectiontokio- Async runtime support
๐ค AI Integration Context
This crate was specifically designed to serve as a content extraction component in Retrieval-Augmented Generation (RAG) workflows where:
- ๐ฅ Users submit messages containing URLs alongside other text
- ๐ง AI systems need structured content from those URLs for analysis
- ๐ Clean, readable Markdown is required preserving essential content and structure while removing HTML markup, scripts, ads, and links
- ๐ Multiple URLs need processing in batch operations
- ๐ก๏ธ Reliability is crucial with proper error handling and fallbacks
The extracted content can then be fed into language models, search systems, or other AI components for further processing.
๐ฏ Perfect for RAG Systems
- Vector Database Integration: Clean Markdown is ideal for embedding generation
- Token Optimization: Removes unnecessary content to reduce token usage
- Batch Processing: Handle multiple URLs from user queries efficiently
- Content Quality: Preserves semantic structure while removing noise
โ๏ธ Markdown Transformation Details
The crate performs intelligent HTML to Markdown conversion that preserves essential formatting while removing clutter:
โ Preserved Elements
- Headers:
<h1>โ# Header,<h2>โ## Header - Emphasis:
<strong>โ**bold**,<em>โ*italic* - Lists:
<ul><li>โ- item,<ol><li>โ1. item - Blockquotes:
<blockquote>โ> quote text - Scientific names:
<i>Bertholletia excelsa</i>โ*Bertholletia excelsa*
โ Removed Elements
- Links:
[text](url)โtext(keeps text, removes URL) - Images:
<img>tags completely removed - Media:
<iframe>,<video>,<audio>elements stripped - Navigation:
<nav>,<header>,<footer>,<aside>sections - Metadata: Author bylines, publication dates, tag lists
- Advertisements: Elements with ad-related classes or IDs
๐งน Text Cleanup
- Normalizes excessive whitespace and line breaks
- Removes photo captions and image attribution text
- Filters out navigation phrases ("click here", "read more")
- Eliminates code blocks and technical markup
- Preserves paragraph structure and readability
๐ Content Processing Pipeline
graph TD
A[๐ Input Text] --> B{URL Detection}
B -->|URLs Found| C[๐ HTTP Request]
B -->|No URLs| D[โก Return Empty]
C --> E[๐ HTML Parsing]
E --> F[โ๏ธ Content Extraction]
F --> G[๐งน Clean & Filter]
G --> H[๐ Markdown Conversion]
H --> I[๐ง Final Cleanup]
I --> J[โ
Output]
- ๐ Input: Raw text from user containing URLs
- ๐ฏ Detection: Regex-based URL extraction with punctuation cleanup
- ๐ Fetching: HTTP requests with randomized user agents
- ๐ HTML Parsing: Document parsing with scraper crate
- โ๏ธ Body Extraction: Extracts only content from HTML
<body>element - ๐ซ Media Removal: Strips images, iframes, videos, and other non-textual elements
- ๐งน Structure Cleaning: Removes scripts, styles, navigation, headers, footers, and ads
- ๐ฏ Content Selection: Focuses on relevant elements (articles, main content, headings, paragraphs)
- ๐ Markdown Conversion: Transforms cleaned HTML to structured Markdown using html2md
- ๐ Link Processing: Converts
[text](url)links to plain text, removes standalone URLs - โจ Format Preservation: Maintains headers, bold, italic, lists, and blockquotes
- ๐ง Final Cleanup: Removes metadata, navigation text, and excessive whitespace
- โ Output: Clean, readable Markdown content paired with source URLs
โ ๏ธ Error Handling
The crate handles various error conditions gracefully:
- ๐ Network timeouts and connection errors
- ๐ Invalid or malformed URLs
- ๐ Empty or missing content
- ๐ซ Server errors (404, 500, etc.)
- ๐ก๏ธ Blocked requests or rate limiting
๐ค Contributing
Contributions are welcome! Here's how to get started:
- ๐ด Fork the repository
- ๐ง Create a feature branch (
git checkout -b feature/amazing-feature) - ๐พ Commit your changes (
git commit -m 'Add amazing feature') - ๐ค Push to the branch (
git push origin feature/amazing-feature) - ๐ Open a Pull Request
Development Setup
# Clone the repository
# Run tests
# Run the interactive CLI
# Format code
# Check for issues
๐ License
Licensed under the MIT License. See LICENSE for details.
๐ Changelog
v0.1.0
- โจ Initial release
- ๐ URL detection and content extraction
- ๐ค Multi-platform user agent support
- ๐งน Content cleaning and normalization
- ๐ป Interactive CLI mode