@aiquants/html-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 +21 -0
- package/README.md +130 -0
- package/dist/core-C45e4gDg.js +20745 -0
- package/dist/core-D1CksXT5.cjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +5 -0
- package/dist/main.cjs +2 -0
- package/dist/main.d.ts +6 -0
- package/dist/main.js +53 -0
- package/dist/src/browser.d.ts +11 -0
- package/dist/src/cli.d.ts +9 -0
- package/dist/src/converter.d.ts +12 -0
- package/dist/src/core.d.ts +10 -0
- package/dist/src/i18n.d.ts +12 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/plugins/index.d.ts +7 -0
- package/dist/src/plugins/rehype-absolute-links.d.ts +9 -0
- package/dist/src/plugins/rehype-sanitize-html.d.ts +7 -0
- package/dist/src/plugins/rehype-wikipedia-footnotes.d.ts +6 -0
- package/dist/src/types.d.ts +25 -0
- package/dist/tests/100-percent-coverage.test.d.ts +1 -0
- package/dist/tests/100-percent-final.test.d.ts +1 -0
- package/dist/tests/absolute-final-coverage.test.d.ts +1 -0
- package/dist/tests/browser.test.d.ts +1 -0
- package/dist/tests/cli.test.d.ts +1 -0
- package/dist/tests/converter.test.d.ts +1 -0
- package/dist/tests/core.test.d.ts +1 -0
- package/dist/tests/coverage-edge-cases.test.d.ts +1 -0
- package/dist/tests/direct-coverage.test.d.ts +1 -0
- package/dist/tests/final-100-percent.test.d.ts +1 -0
- package/dist/tests/final-coverage.test.d.ts +1 -0
- package/dist/tests/i18n.test.d.ts +1 -0
- package/dist/tests/index.test.d.ts +1 -0
- package/dist/tests/line-70-coverage.test.d.ts +1 -0
- package/dist/tests/main.test.d.ts +1 -0
- package/dist/tests/plugins.test.d.ts +1 -0
- package/dist/tests/rehype-absolute-links.test.d.ts +1 -0
- package/dist/tests/rehype-sanitize-html.test.d.ts +1 -0
- package/dist/tests/rehype-wikipedia-footnotes.test.d.ts +1 -0
- package/dist/tests/types.test.d.ts +1 -0
- package/dist/tests/ultimate-coverage.test.d.ts +1 -0
- package/dist/vite.config.d.ts +2 -0
- package/dist/vitest.config.d.ts +2 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 AIQuants
|
|
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,130 @@
|
|
|
1
|
+
# @aiquants/html-to-markdown
|
|
2
|
+
|
|
3
|
+
A tool to dynamically fetch a web page from a given URL and convert it to Markdown.
|
|
4
|
+
|
|
5
|
+
This tool fetches the fully rendered HTML after JavaScript execution and converts it to Markdown. It supports complex pages, including footnotes and table structures.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **Dynamic Content Fetching**: Accurately fetches content from dynamic sites like SPAs (Single Page Applications) using Playwright.
|
|
10
|
+
- **High-Fidelity Conversion**: Performs powerful AST (Abstract Syntax Tree)-based conversion using `rehype` and `remark`.
|
|
11
|
+
- **Preserves Table Content**: Retains HTML tags within table cells (`<td>`, `<th>`) as much as possible to maintain rich formatting.
|
|
12
|
+
- **Link Normalization**: Automatically converts relative links on the page to absolute links to prevent broken links.
|
|
13
|
+
- **Dual Interface**: Usable as both a Node.js library and a command-line tool.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @aiquants/html-to-markdown
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### As a Command-Line Tool
|
|
24
|
+
|
|
25
|
+
You can run the tool directly using `npx` without installation. The converted Markdown will be saved in the `.outputs/raw` directory.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx @aiquants/html-to-markdown <URL> [--locale <locale>]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Example:**
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# Convert a Wikipedia page with the Japanese locale
|
|
35
|
+
npx @aiquants/html-to-markdown https://ja.wikipedia.org/wiki/Node.js --locale ja-JP
|
|
36
|
+
|
|
37
|
+
# Convert an English page (locale defaults to en-US)
|
|
38
|
+
npx @aiquants/html-to-markdown https://en.wikipedia.org/wiki/Node.js
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### As a Library
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { htmlToMarkdown } from '@aiquants/html-to-markdown';
|
|
45
|
+
import fs from 'fs';
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
const url = 'https://en.wikipedia.org/wiki/Node.js';
|
|
49
|
+
const options = {
|
|
50
|
+
locale: 'en-US', // 'en-US' (default) or 'ja-JP'
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const markdown = await htmlToMarkdown(url, options);
|
|
55
|
+
fs.writeFileSync('output.md', markdown);
|
|
56
|
+
console.log('Markdown file has been saved as output.md');
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error('Error converting HTML to Markdown:', error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
main();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `htmlToMarkdown(url, options?)`
|
|
68
|
+
|
|
69
|
+
Converts the HTML content of a given URL to Markdown.
|
|
70
|
+
|
|
71
|
+
- `url` (`string`, **required**): The URL of the web page to convert.
|
|
72
|
+
- `options` (`object`, optional): Options for the conversion process.
|
|
73
|
+
|
|
74
|
+
#### `options` object
|
|
75
|
+
|
|
76
|
+
- `locale` (`string`, optional): Specifies the locale to use for the browser context and console messages.
|
|
77
|
+
- `'en-US'` (default)
|
|
78
|
+
- `'ja-JP'`
|
|
79
|
+
|
|
80
|
+
## How It Works
|
|
81
|
+
|
|
82
|
+
This tool follows these steps for conversion:
|
|
83
|
+
|
|
84
|
+
1. **Fetch HTML (Playwright)**:
|
|
85
|
+
- Launches a browser with Playwright using the specified locale.
|
|
86
|
+
- Navigates to the page and waits until the `networkidle` event, ensuring dynamic content is fully loaded before fetching the HTML.
|
|
87
|
+
|
|
88
|
+
2. **Pre-process HTML (Rehype)**:
|
|
89
|
+
- `rehype-parse`: Parses the HTML into a HAST (HTML Abstract Syntax Tree).
|
|
90
|
+
- `rehype-raw`: Preserves elements like `script` and `style`.
|
|
91
|
+
- `rehypeSanitizeHtml` (custom plugin): Removes empty comment nodes and unnecessary whitespace.
|
|
92
|
+
- `rehypeAbsoluteLinks` (custom plugin): Converts relative paths in `href` and `src` attributes to absolute paths.
|
|
93
|
+
- `rehypeWikipediaFootnotes` (custom plugin): Transforms Wikipedia footnotes into standard Markdown format.
|
|
94
|
+
- `rehypeSlug` & `rehypeAutolinkHeadings`: Adds IDs to headings and automatically generates anchor links.
|
|
95
|
+
|
|
96
|
+
3. **Convert to Markdown (Remark)**:
|
|
97
|
+
- `rehype-remark`: Converts the HAST to an MDAST (Markdown Abstract Syntax Tree), with custom handling for links (`<a>`) and table cells (`<td>`, `<th>`).
|
|
98
|
+
- `remark-gfm`: Adds support for GitHub Flavored Markdown (GFM), including tables and strikethrough.
|
|
99
|
+
- `remark-stringify`: Serializes the MDAST into a Markdown string.
|
|
100
|
+
|
|
101
|
+
## Dependencies and Licenses
|
|
102
|
+
|
|
103
|
+
This project is built upon the following open-source software. We are grateful to the developers of these libraries.
|
|
104
|
+
|
|
105
|
+
| Package | License |
|
|
106
|
+
| ------------------------ | ---------- |
|
|
107
|
+
| github-slugger | MIT |
|
|
108
|
+
| hast | MIT |
|
|
109
|
+
| hast-util-to-html | MIT |
|
|
110
|
+
| playwright | Apache-2.0 |
|
|
111
|
+
| rehype-parse | MIT |
|
|
112
|
+
| rehype-raw | MIT |
|
|
113
|
+
| rehype-remark | MIT |
|
|
114
|
+
| rehype-slug | MIT |
|
|
115
|
+
| remark-gfm | MIT |
|
|
116
|
+
| remark-stringify | MIT |
|
|
117
|
+
| unified | MIT |
|
|
118
|
+
| unist-util-visit | MIT |
|
|
119
|
+
| yargs-parser | ISC |
|
|
120
|
+
|
|
121
|
+
*This list is generated based on the `dependencies` in `package.json`. For the most accurate and up-to-date license information, please refer to the individual packages.*
|
|
122
|
+
|
|
123
|
+
## Author
|
|
124
|
+
|
|
125
|
+
- **GitHub**: [fehde-k](https://github.com/fehde-k)
|
|
126
|
+
- **X (formerly Twitter)**: [@fehdek](https://x.com/fehdek)
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|