@aiquants/html-to-markdown 0.4.0 → 0.5.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/README.md CHANGED
@@ -30,10 +30,17 @@ You can run the tool directly using `npx` without installation. By default, the
30
30
  npx @aiquants/html-to-markdown <URL> [--locale <locale>] [--output <path>]
31
31
  ```
32
32
 
33
+ Or convert HTML content directly:
34
+
35
+ ```bash
36
+ npx @aiquants/html-to-markdown --html-content <HTML_TEXT> [--locale <locale>] [--output <path>]
37
+ ```
38
+
33
39
  **Options:**
34
40
 
35
41
  - `--locale <locale>`: Set the locale for the browser context and console messages (`en-US` or `ja-JP`). Defaults to `en-US`.
36
42
  - `--output <path>`, `-o <path>`: Specify the output file path. If not specified, the file will be saved in the `.outputs/raw` directory with an auto-generated filename.
43
+ - `--html-content <HTML_TEXT>`, `-h <HTML_TEXT>`: Convert HTML content directly instead of fetching from a URL.
37
44
 
38
45
  **Examples:**
39
46
 
@@ -49,6 +56,12 @@ npx @aiquants/html-to-markdown https://en.wikipedia.org/wiki/Node.js --output ./
49
56
 
50
57
  # Use short option for output
51
58
  npx @aiquants/html-to-markdown https://ja.wikipedia.org/wiki/Node.js --locale ja-JP -o ./nodejs-ja.md
59
+
60
+ # Convert HTML content directly
61
+ npx @aiquants/html-to-markdown --html-content '<html><body><h1>Sample Title</h1><p>This is a sample paragraph.</p></body></html>' --output ./sample.md
62
+
63
+ # Convert HTML content with Japanese locale
64
+ npx @aiquants/html-to-markdown --html-content '<html><body><h1>サンプルタイトル</h1><p>これはサンプルの段落です。</p></body></html>' --locale ja-JP -o ./sample-ja.md
52
65
  ```
53
66
 
54
67
  ### As a Library
@@ -58,6 +71,7 @@ import { htmlToMarkdown } from '@aiquants/html-to-markdown';
58
71
  import fs from 'fs';
59
72
 
60
73
  async function main() {
74
+ // Example 1: Convert from URL
61
75
  const url = 'https://en.wikipedia.org/wiki/Node.js';
62
76
  const options = {
63
77
  locale: 'en-US', // 'en-US' (default) or 'ja-JP'
@@ -70,6 +84,31 @@ async function main() {
70
84
  } catch (error) {
71
85
  console.error('Error converting HTML to Markdown:', error);
72
86
  }
87
+
88
+ // Example 2: Convert HTML content directly
89
+ const htmlContent = `
90
+ <html>
91
+ <body>
92
+ <h1>Sample Title</h1>
93
+ <p>This is a sample paragraph.</p>
94
+ <ul>
95
+ <li>Item 1</li>
96
+ <li>Item 2</li>
97
+ </ul>
98
+ </body>
99
+ </html>
100
+ `;
101
+
102
+ try {
103
+ const { markdown } = await htmlToMarkdown('', {
104
+ locale: 'en-US',
105
+ htmlContent: htmlContent
106
+ });
107
+ fs.writeFileSync('output-from-html.md', markdown);
108
+ console.log('Markdown file has been saved as output-from-html.md');
109
+ } catch (error) {
110
+ console.error('Error converting HTML to Markdown:', error);
111
+ }
73
112
  }
74
113
 
75
114
  main();
@@ -262,11 +301,11 @@ await streamableMcpServer.start(8000); // Port 8000
262
301
 
263
302
  ## API
264
303
 
265
- ### `htmlToMarkdown(url, options?)`
304
+ ### `htmlToMarkdown(urlOrHtml, options?)`
266
305
 
267
- Converts the HTML content of a given URL to Markdown.
306
+ Converts the HTML content of a given URL or HTML string to Markdown.
268
307
 
269
- - `url` (`string`, **required**): The URL of the web page to convert.
308
+ - `urlOrHtml` (`string`, **required**): The URL of the web page to convert, or when using `htmlContent` option, this can be any string (commonly used as identifier).
270
309
  - `options` (`object`, optional): Options for the conversion process.
271
310
 
272
311
  #### `options` object
@@ -274,6 +313,7 @@ Converts the HTML content of a given URL to Markdown.
274
313
  - `locale` (`string`, optional): Specifies the locale to use for the browser context and console messages.
275
314
  - `'en-US'` (default)
276
315
  - `'ja-JP'`
316
+ - `htmlContent` (`string`, optional): HTML content as a string instead of fetching from URL. When provided, the function will convert this HTML content directly instead of fetching content from the `urlOrHtml` parameter.
277
317
 
278
318
  ## How It Works
279
319
 
@@ -9,10 +9,12 @@ import { chromium } from "playwright";
9
9
  const messages = {
10
10
  "ja-JP": {
11
11
  error_url_required: "エラー: 変換対象のURLを引数で指定してください。",
12
- usage: "使用法: node dist/main.js <URL> [--output <ファイルパス>] [--locale <ロケール>]",
13
- example: "例: node dist/main.js https://ja.wikipedia.org/wiki/Node.js -o output.md --locale ja-JP",
12
+ error_url_or_html_required: "エラー: URLまたはHTMLコンテンツのいずれかを指定してください。",
13
+ usage: "使用法: node dist/main.js <URL> [--output <ファイルパス>] [--locale <ロケール>] [--html-content <HTMLテキスト>]",
14
+ example: "例: node dist/main.js https://ja.wikipedia.org/wiki/Node.js -o output.md --locale ja-JP\n または: node dist/main.js --html-content '<html><body><h1>タイトル</h1></body></html>' -o output.md",
14
15
  cli_start: "🚀 コマンドラインから処理を開始します...",
15
16
  cli_url: " - URL: {url}",
17
+ cli_html_content: " - HTML Content: {length} 文字",
16
18
  cli_locale: " - Locale: {locale}",
17
19
  cli_output: " - Output: {path}",
18
20
  success_save: "✅ Markdownファイルが正常に保存されました。",
@@ -29,10 +31,12 @@ const messages = {
29
31
  },
30
32
  "en-US": {
31
33
  error_url_required: "Error: Please specify the URL to convert as an argument.",
32
- usage: "Usage: node dist/main.js <URL> [--output <file path>] [--locale <locale>]",
33
- example: "Example: node dist/main.js https://en.wikipedia.org/wiki/Node.js -o output.md --locale en-US",
34
+ error_url_or_html_required: "Error: Please specify either a URL or HTML content.",
35
+ usage: "Usage: node dist/main.js <URL> [--output <file path>] [--locale <locale>] [--html-content <HTML text>]",
36
+ example: "Example: node dist/main.js https://en.wikipedia.org/wiki/Node.js -o output.md --locale en-US\n or: node dist/main.js --html-content '<html><body><h1>Title</h1></body></html>' -o output.md",
34
37
  cli_start: "🚀 Starting process from command line...",
35
38
  cli_url: " - URL: {url}",
39
+ cli_html_content: " - HTML Content: {length} characters",
36
40
  cli_locale: " - Locale: {locale}",
37
41
  cli_output: " - Output: {path}",
38
42
  success_save: "✅ Markdown file saved successfully.",