npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@page-replica/pure-html-for-rag

v1.0.0

Published

Aggressively clean HTML for RAG, LLM ingestion, and semantic extraction

Readme

@page-replica/pure-html-for-rag

npm version License: MIT npm downloads

Aggressively clean HTML for RAG pipelines and LLM ingestion 🧹✨

Transform bloated web pages into pure, semantic HTML perfect for embeddings, vector databases, and AI processing.

Demo • Report Bug • Request Feature


🚀 Used in Production: This library powers page-replica.com's structured scraping engine. See it in action with our live demo to extract and structure real web pages into clean JSON, Markdown, or HTML.


Why?

Modern web pages are cluttered with tracking scripts, analytics, styling, ads, and interactive elements that waste tokens and dilute semantic meaning when processing content for AI systems. This library strips away the noise to give you clean, meaningful HTML that:

  • ✅ Reduces token count by 60-90% (fewer API costs)
  • ✅ Improves embedding quality (less noise = better semantic search)
  • ✅ Speeds up processing (smaller payloads = faster inference)
  • ✅ Preserves structure (headings, paragraphs, links stay intact)
  • ✅ Zero dependencies (pure JavaScript, no bloat)

Installation

npm install @page-replica/pure-html-for-rag

Quick Start

const { cleanHtml } = require("@page-replica/pure-html-for-rag");

const rawHtml = `
	<html>
		<head>
			<title>Example</title>
			<script src="tracker.js"></script>
		</head>
		<body>
			<h1>Example page</h1>
			<p style="color:red" onclick="alert('hi')">Hello world!</p>
			<img src="hero.jpg" alt="" />
		</body>
	</html>
`;

const cleaned = cleanHtml(rawHtml);
// => "<html><head><title>Example</title></head><body><h1>Example page</h1><p>Hello world!</p></body></html>"

Result: 189 characters → 105 characters (44% reduction)

Use Cases

🔍 RAG (Retrieval Augmented Generation)

Clean HTML before chunking and embedding web content for vector databases like Pinecone, Weaviate, or ChromaDB.

const { cleanHtml } = require("@page-replica/pure-html-for-rag");
const fetch = require("node-fetch");

async function indexWebPage(url) {
  const response = await fetch(url);
  const html = await response.text();
  const cleaned = cleanHtml(html);
  
  // Now chunk and embed the cleaned HTML
  const chunks = chunkText(cleaned, 512);
  await vectorDB.upsert(chunks);
}

🤖 LLM Context Preparation

Reduce token costs when feeding web content to GPT-4, Claude, or other LLMs.

const { cleanHtml } = require("@page-replica/pure-html-for-rag");
const puppeteer = require("puppeteer");

async function scrapeForLLM(url) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  const html = await page.content();
  await browser.close();
  
  const cleaned = cleanHtml(html);
  
  // Use cleaned HTML in your prompt
  const response = await openai.chat.completions.create({
    model: "gpt-4",
    messages: [
      { role: "user", content: `Summarize this page: ${cleaned}` }
    ]
  });
}

📊 Web Scraping for Analysis

Extract clean, structured content from websites for data analysis or content migration.

const { cleanHtml } = require("@page-replica/pure-html-for-rag");
const axios = require("axios");

async function extractArticle(url) {
  const { data } = await axios.get(url);
  const cleaned = cleanHtml(data, {
    removeComments: true,
    collapseWhitespace: true,
  });
  
  // Extract text or parse structure
  return parseArticleContent(cleaned);
}

API Reference

cleanHtml(html, options?)

| Option | Type | Default | Description | | --- | --- | --- | --- | | collapseWhitespace | boolean | true | Converts repeated whitespace to single spaces for a smaller payload. | | removeEmptyElements | boolean | true | Iteratively drops elements that become empty after cleaning. | | removeComments | boolean | true | Removes HTML comments. | | allowedAttributeTags | string[] | ["a"] | Tags that should keep their attributes (e.g. keep href on links). |

Returns a minimised HTML string containing only textual content and headings.

createCleaner(defaultOptions?)

Create a reusable cleaner with baked-in defaults:

const { createCleaner } = require("@page-replica/pure-html-for-rag");

const clean = createCleaner({ allowedAttributeTags: [] });
const output = clean(rawHtml, { collapseWhitespace: false });

Browser Usage

You can use this library in the browser by including the bundled version:

<!DOCTYPE html>
<html>
<head>
  <title>HTML Cleaner Demo</title>
</head>
<body>
  <script src="https://unpkg.com/@page-replica/pure-html-for-rag@latest/demo/pure-html-for-rag.bundle.js"></script>
  <script>
    const { cleanHtml } = window.pureHtmlForRag;
    
    const dirtyHtml = document.documentElement.outerHTML;
    const cleaned = cleanHtml(dirtyHtml);
    
    console.log('Cleaned HTML:', cleaned);
  </script>
</body>
</html>

Or use with a module bundler (Webpack, Rollup, Vite):

import { cleanHtml } from '@page-replica/pure-html-for-rag';

const cleaned = cleanHtml(document.body.innerHTML);

Advanced Examples

Puppeteer Integration

const puppeteer = require("puppeteer");
const { cleanHtml } = require("@page-replica/pure-html-for-rag");

async function scrapeAndClean(url) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  // Block unnecessary resources
  await page.setRequestInterception(true);
  page.on('request', (req) => {
    const resourceType = req.resourceType();
    if (['image', 'stylesheet', 'font', 'media'].includes(resourceType)) {
      req.abort();
    } else {
      req.continue();
    }
  });
  
  await page.goto(url, { waitUntil: 'domcontentloaded' });
  const html = await page.content();
  await browser.close();
  
  return cleanHtml(html);
}

Batch Processing

const { createCleaner } = require("@page-replica/pure-html-for-rag");

const cleaner = createCleaner({
  collapseWhitespace: true,
  removeEmptyElements: true,
  allowedAttributeTags: ['a', 'img'], // Keep links and images with alt text
});

async function processMultiplePages(urls) {
  const results = await Promise.all(
    urls.map(async (url) => {
      const response = await fetch(url);
      const html = await response.text();
      return {
        url,
        cleaned: cleaner(html),
        originalSize: html.length,
        cleanedSize: cleaner(html).length,
      };
    })
  );
  
  return results;
}

Custom Cleaning Rules

const { cleanHtml } = require("@page-replica/pure-html-for-rag");

// Minimal cleaning - keep everything except scripts
const minimal = cleanHtml(html, {
  removeComments: false,
  removeEmptyElements: false,
  collapseWhitespace: false,
  allowedAttributeTags: ['a', 'img', 'table', 'td', 'th'],
});

// Aggressive cleaning - strip everything
const aggressive = cleanHtml(html, {
  removeComments: true,
  removeEmptyElements: true,
  collapseWhitespace: true,
  allowedAttributeTags: [], // No attributes preserved
});

Comparison: Before & After

Input (2,847 characters)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Blog Post - 10 Tips</title>
  <link rel="stylesheet" href="style.css">
  <script async src="https://www.google-analytics.com/ga.js"></script>
  <script src="https://cdn.segment.com/analytics.js"></script>
  <style>
    body { font-family: Arial; color: #333; }
    .container { max-width: 1200px; margin: 0 auto; }
  </style>
</head>
<body>
  <nav class="navbar navbar-expand-lg">
    <a href="/" class="logo">
      <img src="logo.png" alt="Logo" width="150">
    </a>
    <button onclick="toggleMenu()" class="menu-btn">Menu</button>
  </nav>
  
  <article>
    <h1>10 Productivity Tips</h1>
    <p>Learn how to boost your productivity with these proven strategies.</p>
    
    <h2>Tip 1: Start Early</h2>
    <p>The early bird catches the worm.</p>
    
    <h2>Tip 2: Take Breaks</h2>
    <p>Regular breaks prevent burnout.</p>
  </article>
  
  <form class="newsletter">
    <input type="email" placeholder="Your email">
    <button type="submit">Subscribe</button>
  </form>
  
  <script>
    function toggleMenu() { /* ... */ }
    gtag('event', 'page_view');
  </script>
</body>
</html>

Output (412 characters, 85.5% reduction)

<html><head><title>Blog Post - 10 Tips</title></head><body><nav><a href="/"> </a></nav><article><h1>10 Productivity Tips</h1><p>Learn how to boost your productivity with these proven strategies.</p><h2>Tip 1: Start Early</h2><p>The early bird catches the worm.</p><h2>Tip 2: Take Breaks</h2><p>Regular breaks prevent burnout.</p></article></body></html>

What Gets Removed?

  • <script>, <style>, <noscript>, <iframe>, <svg>, <video>, <audio>, <canvas>, <form>, <button>, <select>, <textarea> and similar interactive blocks.
  • <img>, <source>, <track>, <input>, <meta>, <base>, <link> (including preload/stylesheet/analytics variants).
  • Inline style= attributes, event handlers like onclick=, and extra class= clutter.
  • HTML comments and empty containers created by the removal step.

The end result is a compact, stable string ready to feed into embeddings or LLM prompts without wasting budget on layout cruft.

Performance

  • Fast: Processes typical web pages in under 5ms
  • Lightweight: Zero dependencies, ~5KB minified
  • Memory efficient: Streams through content without building large DOM trees
  • Regex-based: Uses optimized regular expressions for maximum speed

Benchmark on a typical blog post (100KB HTML):

Original size: 102,847 bytes
Cleaned size:   12,441 bytes (87.9% reduction)
Processing time: 3.2ms

Page Replica Structured Scraping - Live Demo

Page Replica Structured Scraping Demo

Good news everyone 🙂

If you just want to see how the structured cleanup works without setting anything up locally, you can try the live demo.

It lets you experiment with structuring real pages into JSON, Markdown, or low-noise HTML, which is useful if you need to quickly inspect or structure page content.

https://page-replica.com/structured/live-demo


Made with ❤️ for the AI community

Website • GitHub • npm