web-to-markdown 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nidhi Singh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # web-to-markdown
2
+
3
+ Convert any web page to clean Markdown. Built for developers and LLM pipelines.
4
+
5
+ Handles documentation sites (Mintlify, GitBook, Docusaurus, Next.js docs), blogs, news articles, and more. Automatically extracts the main content, strips navigation chrome, and produces readable Markdown with GFM support.
6
+
7
+ ## Quick Start
8
+
9
+ ```bash
10
+ # No install needed
11
+ npx web-to-markdown https://example.com
12
+
13
+ # Save to a file
14
+ npx web-to-markdown https://example.com -o page.md
15
+ ```
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ # Global CLI
21
+ npm install -g web-to-markdown
22
+
23
+ # Project dependency
24
+ npm install web-to-markdown
25
+ ```
26
+
27
+ Requires Node.js 20 or later.
28
+
29
+ ## CLI Usage
30
+
31
+ ```bash
32
+ web-to-markdown <url> [options]
33
+ ```
34
+
35
+ ### Options
36
+
37
+ | Flag | Description |
38
+ |------|-------------|
39
+ | `-o, --output <file>` | Write to file instead of stdout |
40
+ | `-b, --browser` | Force headless browser rendering (for SPAs) |
41
+ | `-r, --raw` | Convert full HTML without content extraction |
42
+ | `-f, --frontmatter` | Include YAML frontmatter with metadata |
43
+ | `--no-images` | Strip images from output |
44
+ | `--timeout <ms>` | Timeout for page loading (default: 30000) |
45
+
46
+ ### Examples
47
+
48
+ ```bash
49
+ # Convert a blog post
50
+ web-to-markdown https://example.com/blog/post
51
+
52
+ # Include title, author, date as YAML frontmatter
53
+ web-to-markdown https://example.com/blog/post -f
54
+
55
+ # Strip all images
56
+ web-to-markdown https://example.com --no-images
57
+
58
+ # Convert full page HTML without content extraction
59
+ web-to-markdown https://example.com -r
60
+
61
+ # Pipe to another tool
62
+ web-to-markdown https://example.com | head -20
63
+ ```
64
+
65
+ ### JavaScript-Rendered Pages (SPAs)
66
+
67
+ Some pages (React, Vue, Angular apps) render content with JavaScript. Use `--browser` to launch a headless Chromium instance:
68
+
69
+ ```bash
70
+ # One-time setup
71
+ npm install playwright
72
+ npx playwright install chromium
73
+
74
+ # Use the --browser flag
75
+ web-to-markdown https://myapp.com --browser
76
+ ```
77
+
78
+ Playwright is an optional peer dependency and is only needed for JS-rendered pages.
79
+
80
+ ## Programmatic API
81
+
82
+ ```typescript
83
+ import { convert } from "web-to-markdown";
84
+
85
+ const { markdown, metadata, warnings } = await convert("https://example.com", {
86
+ frontmatter: true,
87
+ noImages: false,
88
+ timeout: 30_000,
89
+ });
90
+
91
+ console.log(metadata.title);
92
+ console.log(markdown);
93
+ ```
94
+
95
+ ## How It Works
96
+
97
+ The converter tries multiple extraction strategies in order, using the first one that succeeds:
98
+
99
+ ### Strategy 1: Raw Markdown Endpoint
100
+
101
+ Many documentation sites serve raw markdown when `.md` is appended to the URL path. The converter tries this first (e.g., `/docs/guide` becomes `/docs/guide.md`). Works with Mintlify, Stripe Docs, GitHub Docs, React docs, Next.js docs, and others.
102
+
103
+ ### Strategy 2: MDX Extraction (Next.js / Mintlify)
104
+
105
+ For sites built with Next.js and Mintlify, the converter extracts MDX content directly from React Server Component payloads embedded in the HTML. It parses YAML frontmatter, strips JSX components (Accordion, Tab, Callout, Card, etc.) into clean Markdown equivalents, and resolves relative URLs.
106
+
107
+ ### Strategy 3: Readability + Turndown
108
+
109
+ The general-purpose fallback:
110
+
111
+ 1. **Extract** -- Mozilla Readability removes boilerplate (nav, footer, ads), isolating the main content
112
+ 2. **Convert** -- Turndown converts the cleaned HTML to Markdown with GFM support (tables, task lists, strikethrough, fenced code blocks)
113
+
114
+ If Readability drops significant content (detected via `<pre>` block comparison), the converter falls back to extracting from `<article>` or `<main>` elements directly.
115
+
116
+ ### Site-Specific Handling
117
+
118
+ **GitBook** -- Detects GitBook pages via `data-gb-*` attributes and removes navigation chrome before extraction: site header, sidebar TOC, breadcrumbs, SVG icons in headings, dark-mode duplicate images, and "Was this helpful?" boilerplate.
119
+
120
+ **Code blocks** -- Language detection from `class="language-X"`, `data-lang` attributes, and Sphinx/Pygments span classes. Adaptive fence length for code containing backticks.
121
+
122
+ ## Security
123
+
124
+ - **SSRF protection** -- Blocks requests to private IPs, localhost, link-local, and cloud metadata endpoints. Handles obfuscated IPs (hex, octal, decimal encoding, IPv6-mapped IPv4, NAT64). DNS is resolved once and pinned to prevent rebinding attacks.
125
+ - **Dangerous protocol filtering** -- Strips `javascript:`, `data:`, `vbscript:`, and other dangerous URL schemes from links and images, with control character and zero-width character normalization.
126
+ - **Content limits** -- 10MB response size limit, 100K DOM element cap, 500-level nesting depth guard.
127
+ - **Redirect validation** -- Each redirect hop is re-validated for SSRF (max 5 redirects).
128
+ - **Sandboxed MDX evaluation** -- Compiled MDX is executed with a mock React runtime; dangerous patterns (`require`, `import`, `eval`, `fetch`, `process`) are blocked.
129
+ - **Typed errors** -- All failures throw specific error classes (`ValidationError`, `SSRFError`, `NetworkError`, `ContentError`) with ES2022 error cause chaining.
130
+
131
+ ## License
132
+
133
+ MIT