@aiquants/html-to-markdown 0.6.0 → 0.7.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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e}),Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./core--XIBQvpW.cjs"),t=require("./mcp-server-jjdOcY1v.cjs"),c=require("./mcp-server-streamable-DU6yq8ZG.cjs");exports.getMessage=r.getMessage,exports.htmlToMarkdown=r.htmlToMarkdown,exports.createMcpServer=t.createMcpServer,exports.runMcpServer=t.runMcpServer,exports.createStreamableMcpServer=c.createStreamableMcpServer,exports.runStreamableMcpServer=c.runStreamableMcpServer;
1
+ const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e}),Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./core--XIBQvpW.cjs"),t=require("./mcp-server-eznfa4gh.cjs"),c=require("./mcp-server-streamable-DJ-R_Z_E.cjs");exports.getMessage=r.getMessage,exports.htmlToMarkdown=r.htmlToMarkdown,exports.createMcpServer=t.createMcpServer,exports.runMcpServer=t.runMcpServer,exports.createStreamableMcpServer=c.createStreamableMcpServer,exports.runStreamableMcpServer=c.runStreamableMcpServer;
package/dist/index.js CHANGED
@@ -1,18 +1 @@
1
- import { Window } from "happy-dom";
2
- const window = new Window();
3
- Object.assign(global, {
4
- document: window.document,
5
- window,
6
- self: window
7
- });
8
- import { g, h } from "./core-txi0EJ9v.js";
9
- import { c, r } from "./mcp-server-B8T7ljWL.js";
10
- import { c as c2, r as r2 } from "./mcp-server-streamable-Bv5fFO_D.js";
11
- export {
12
- c as createMcpServer,
13
- c2 as createStreamableMcpServer,
14
- g as getMessage,
15
- h as htmlToMarkdown,
16
- r as runMcpServer,
17
- r2 as runStreamableMcpServer
18
- };
1
+ import{Window as e}from"happy-dom";const r=new e;Object.assign(global,{document:r.document,window:r,self:r});import{g as a,h as s}from"./core-ChRwTJHy.js";import{c as o,r as m}from"./mcp-server-CvLGX0Fe.js";import{c as t,r as c}from"./mcp-server-streamable-CjY9xfH0.js";export{o as createMcpServer,t as createStreamableMcpServer,a as getMessage,s as htmlToMarkdown,m as runMcpServer,c as runStreamableMcpServer};
package/dist/main.js CHANGED
@@ -1,106 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import { Window } from "happy-dom";
3
- const window = new Window();
4
- Object.assign(global, {
5
- document: window.document,
6
- window,
7
- self: window
8
- });
9
- import { g as getMessage, h as htmlToMarkdown } from "./core-txi0EJ9v.js";
10
- const runCli = async () => {
11
- const yargsParser = (await import("yargs-parser")).default;
12
- const fs = await import("node:fs");
13
- const path = await import("node:path");
14
- const argv = yargsParser(process.argv.slice(2), {
15
- string: ["locale", "output", "html-content"],
16
- alias: {
17
- output: ["o"],
18
- "html-content": ["h"]
19
- },
20
- default: {
21
- locale: "en-US"
22
- // デフォルトのロケール
23
- }
24
- });
25
- const targetUrl = argv._[0];
26
- const locale = argv.locale;
27
- const outputFile = argv.output;
28
- const htmlContent = argv["html-content"];
29
- if (!(targetUrl || htmlContent)) {
30
- console.error(getMessage(locale, "error_url_or_html_required"));
31
- console.info(getMessage(locale, "usage"));
32
- console.info(getMessage(locale, "example"));
33
- process.exit(1);
34
- }
35
- if (targetUrl && !htmlContent) {
36
- try {
37
- new URL(targetUrl);
38
- } catch (error) {
39
- console.error(getMessage(locale, "error_app"), error);
40
- process.exit(1);
41
- }
42
- }
43
- try {
44
- console.info(`
45
- ${getMessage(locale, "cli_start")}`);
46
- if (targetUrl) {
47
- console.info(getMessage(locale, "cli_url", { url: targetUrl }));
48
- }
49
- if (htmlContent) {
50
- console.info(getMessage(locale, "cli_html_content", { length: htmlContent.length.toString() }));
51
- }
52
- console.info(getMessage(locale, "cli_locale", { locale }));
53
- if (outputFile) {
54
- console.info(getMessage(locale, "cli_output", { path: outputFile }));
55
- }
56
- const { markdown, html } = await htmlToMarkdown(targetUrl || "direct-html-input", {
57
- locale,
58
- htmlContent
59
- });
60
- let filePath;
61
- if (outputFile) {
62
- filePath = path.resolve(process.cwd(), outputFile);
63
- const outputDir = path.dirname(filePath);
64
- if (!fs.existsSync(outputDir)) {
65
- fs.mkdirSync(outputDir, { recursive: true });
66
- }
67
- } else {
68
- let safeFileName;
69
- if (htmlContent) {
70
- safeFileName = "html_content";
71
- } else {
72
- safeFileName = targetUrl.replace(/https?:\/\//, "").replace(/[^a-zA-Z0-9]/g, "_");
73
- }
74
- const timestamp = Date.now();
75
- const fileName = `${safeFileName}_${timestamp}.md`;
76
- const outputDir = path.join(process.cwd(), ".outputs", "raw");
77
- if (!fs.existsSync(outputDir)) {
78
- fs.mkdirSync(outputDir, { recursive: true });
79
- }
80
- filePath = path.join(outputDir, fileName);
81
- }
82
- fs.writeFileSync(filePath, markdown);
83
- const parsedPath = path.parse(filePath);
84
- const htmlFilePath = path.join(parsedPath.dir, `${parsedPath.name}.html`);
85
- fs.writeFileSync(htmlFilePath, html);
86
- console.info(`
87
- ${getMessage(locale, "success_save")}`);
88
- console.info(getMessage(locale, "success_path", { path: filePath }));
89
- console.info(getMessage(locale, "success_path", { path: htmlFilePath }));
90
- } catch (error) {
91
- console.error(`
92
- ${getMessage(locale, "error_app")}`, error);
93
- process.exit(1);
94
- }
95
- };
96
- const main = async () => {
97
- const isDirectRun = (
98
- // `node dist/main.js` や `npx` 経由での実行を判定
99
- import.meta.url.endsWith(process.argv[1]) || // `vite-node main.ts` 経由での実行を判定
100
- process.argv[1] && process.argv[1].includes("vite-node")
101
- );
102
- if (isDirectRun) {
103
- runCli();
104
- }
105
- };
106
- main();
2
+ import{Window as t}from"happy-dom";const e=new t;Object.assign(global,{document:e.document,window:e,self:e});import{g as o,h as s}from"./core-ChRwTJHy.js";(async()=>{(import.meta.url.endsWith(process.argv[1])||process.argv[1]&&process.argv[1].includes("vite-node"))&&(async()=>{const t=(await import("yargs-parser")).default,e=await import("node:fs"),o=await import("node:path"),c=t(process.argv.slice(2),{string:["locale","output","html-content"],alias:{output:["o"],"html-content":["h"]},default:{locale:"en-US"}}),r=c._[0],n=c.locale,a=c.output,i=c["html-content"];if(r||i||process.exit(1),r&&!i)try{new URL(r)}catch(l){process.exit(1)}try{const{markdown:t,html:c}=await s(r||"direct-html-input",{locale:n,htmlContent:i});let l;if(a){l=o.resolve(process.cwd(),a);const t=o.dirname(l);e.existsSync(t)||e.mkdirSync(t,{recursive:!0})}else{let t;t=i?"html_content":r.replace(/https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"_");const s=`${t}_${Date.now()}.md`,c=o.join(process.cwd(),".outputs","raw");e.existsSync(c)||e.mkdirSync(c,{recursive:!0}),l=o.join(c,s)}e.writeFileSync(l,t);const p=o.parse(l),m=o.join(p.dir,`${p.name}.html`);e.writeFileSync(m,c)}catch(l){process.exit(1)}})()})();
@@ -0,0 +1 @@
1
+ import{Window as t}from"happy-dom";const e=new t;Object.assign(global,{document:e.document,window:e,self:e});import{J as r,g as a,S as n,L as s,M as o,C as i,p as c,c as l,a as d,b as h,s as u,d as f}from"./version-DEqf1gI6.js";import m from"node:process";import{h as p}from"./core-ChRwTJHy.js";class _{append(t){this._buffer=this._buffer?Buffer.concat([this._buffer,t]):t}readMessage(){if(!this._buffer)return null;const t=this._buffer.indexOf("\n");if(-1===t)return null;const e=this._buffer.toString("utf8",0,t).replace(/\r$/,"");return this._buffer=this._buffer.subarray(t+1),function(t){return r.parse(JSON.parse(t))}(e)}clear(){this._buffer=void 0}}class w{constructor(t=m.stdin,e=m.stdout){this._stdin=t,this._stdout=e,this._readBuffer=new _,this._started=!1,this._ondata=t=>{this._readBuffer.append(t),this.processReadBuffer()},this._onerror=t=>{var e;null===(e=this.onerror)||void 0===e||e.call(this,t)}}async start(){if(this._started)throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");this._started=!0,this._stdin.on("data",this._ondata),this._stdin.on("error",this._onerror)}processReadBuffer(){for(var t,e;;)try{const e=this._readBuffer.readMessage();if(null===e)break;null===(t=this.onmessage)||void 0===t||t.call(this,e)}catch(r){null===(e=this.onerror)||void 0===e||e.call(this,r)}}async close(){var t;this._stdin.off("data",this._ondata),this._stdin.off("error",this._onerror);0===this._stdin.listenerCount("data")&&this._stdin.pause(),this._readBuffer.clear(),null===(t=this.onclose)||void 0===t||t.call(this)}send(t){return new Promise(e=>{const r=function(t){return JSON.stringify(t)+"\n"}(t);this._stdout.write(r)?e():this._stdout.once("drain",e)})}}class v{constructor(){this.version=a(),this.server=new n({name:"@aiquants/html-to-markdown",version:this.version,description:"Convert web pages and HTML to Markdown with JavaScript dynamic content support and table structure preservation. Includes separate file saving tool."},{capabilities:{tools:{}}}),this.setupToolHandlers()}setupToolHandlers(){this.server.setRequestHandler(s,async()=>({tools:[{name:"html_to_markdown",description:"Convert web pages or HTML strings to Markdown format. Handles JavaScript-rendered dynamic content and preserves table structures.",inputSchema:o.htmlToMarkdown},{name:"save_content_to_file",description:"Save text content to a file with specified path or directory. Supports automatic filename generation and format detection.",inputSchema:o.saveContentToFile},{name:"url_to_markdown_file",description:"Convert web pages or HTML strings directly to Markdown files. Combines HTML-to-Markdown conversion and file saving in one step. Handles JavaScript-rendered dynamic content.",inputSchema:o.urlToMarkdownFile}]})),this.server.setRequestHandler(i,async t=>{switch(t.params.name){case"html_to_markdown":return await this.handleHtmlToMarkdown(t.params.arguments);case"save_content_to_file":return await this.handleSaveContentToFile(t.params.arguments);case"url_to_markdown_file":return await this.handleUrlToMarkdownFile(t.params.arguments);default:throw new Error(`Unknown tool: ${t.params.name}`)}})}async handleHtmlToMarkdown(t){try{const e=c(t),{url:r,html_content:a,locale:n="en-US"}=e;if(r)try{new URL(r)}catch{throw new Error(`Invalid URL: ${r}`)}const s=(await p(r||"direct-html-input",{locale:n,htmlContent:a})).markdown;return l(s)}catch(e){return d(e)}}async handleSaveContentToFile(t){try{const e=h(t),{content:r,save_path:a,save_directory:n,filename:s}=e,o=await u(r,a,n,s);return l(`File saved successfully to: ${o}`)}catch(e){return d(e)}}async handleUrlToMarkdownFile(t){try{const e=f(t),{url:r,html_content:a,locale:n="en-US",save_path:s,save_directory:o,filename:i}=e;if(r)try{new URL(r)}catch{throw new Error(`Invalid URL: ${r}`)}const c=(await p(r||"direct-html-input",{locale:n,htmlContent:a})).markdown;let d=i;!d&&r&&(d=this.generateFilenameFromUrl(r));const h=await u(c,s,o,d);return l(`URL successfully converted to Markdown and saved to: ${h}`)}catch(e){return d(e)}}generateFilenameFromUrl(t){try{let e=new URL(t).pathname.split("/").pop()||"page";return e=e.replace(/\.[^.]*$/,""),e=e.replace(/[<>:"/\\|?*]/g,"_"),e&&!e.match(/^\.+$/)||(e="page"),e}catch{return"page"}}async start(){const t=new w;await this.server.connect(t)}}const y=()=>new v,g=async()=>{const t=y();await t.start()};export{y as c,g as r};
@@ -1 +1 @@
1
- const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e});const t=require("./version-Jbxn3hLf.cjs"),r=require("node:process"),a=require("./core--XIBQvpW.cjs");class n{append(e){this._buffer=this._buffer?Buffer.concat([this._buffer,e]):e}readMessage(){if(!this._buffer)return null;const e=this._buffer.indexOf("\n");if(-1===e)return null;const r=this._buffer.toString("utf8",0,e).replace(/\r$/,"");return this._buffer=this._buffer.subarray(e+1),function(e){return t.JSONRPCMessageSchema.parse(JSON.parse(e))}(r)}clear(){this._buffer=void 0}}class s{constructor(e=r.stdin,t=r.stdout){this._stdin=e,this._stdout=t,this._readBuffer=new n,this._started=!1,this._ondata=e=>{this._readBuffer.append(e),this.processReadBuffer()},this._onerror=e=>{var t;null===(t=this.onerror)||void 0===t||t.call(this,e)}}async start(){if(this._started)throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");this._started=!0,this._stdin.on("data",this._ondata),this._stdin.on("error",this._onerror)}processReadBuffer(){for(var e,t;;)try{const t=this._readBuffer.readMessage();if(null===t)break;null===(e=this.onmessage)||void 0===e||e.call(this,t)}catch(r){null===(t=this.onerror)||void 0===t||t.call(this,r)}}async close(){var e;this._stdin.off("data",this._ondata),this._stdin.off("error",this._onerror);0===this._stdin.listenerCount("data")&&this._stdin.pause(),this._readBuffer.clear(),null===(e=this.onclose)||void 0===e||e.call(this)}send(e){return new Promise(t=>{const r=function(e){return JSON.stringify(e)+"\n"}(e);this._stdout.write(r)?t():this._stdout.once("drain",t)})}}class o{constructor(){this.version=t.getPackageVersion(),this.server=new t.Server({name:"@aiquants/html-to-markdown",version:this.version,description:"Convert web pages and HTML to Markdown with JavaScript dynamic content support and table structure preservation. Includes separate file saving tool."},{capabilities:{tools:{}}}),this.setupToolHandlers()}setupToolHandlers(){this.server.setRequestHandler(t.ListToolsRequestSchema,async()=>({tools:[{name:"html_to_markdown",description:"Convert web pages or HTML strings to Markdown format. Handles JavaScript-rendered dynamic content and preserves table structures.",inputSchema:t.MCP_TOOL_SCHEMAS.htmlToMarkdown},{name:"save_content_to_file",description:"Save text content to a file with specified path or directory. Supports automatic filename generation and format detection.",inputSchema:t.MCP_TOOL_SCHEMAS.saveContentToFile},{name:"url_to_markdown_file",description:"Convert web pages or HTML strings directly to Markdown files. Combines HTML-to-Markdown conversion and file saving in one step. Handles JavaScript-rendered dynamic content.",inputSchema:t.MCP_TOOL_SCHEMAS.urlToMarkdownFile}]})),this.server.setRequestHandler(t.CallToolRequestSchema,async e=>{switch(e.params.name){case"html_to_markdown":return await this.handleHtmlToMarkdown(e.params.arguments);case"save_content_to_file":return await this.handleSaveContentToFile(e.params.arguments);case"url_to_markdown_file":return await this.handleUrlToMarkdownFile(e.params.arguments);default:throw new Error(`Unknown tool: ${e.params.name}`)}})}async handleHtmlToMarkdown(e){try{const r=t.parseHtmlToMarkdownArgs(e),{url:n,html_content:s,locale:o="en-US"}=r;if(n)try{new URL(n)}catch{throw new Error(`Invalid URL: ${n}`)}const i=(await a.htmlToMarkdown(n||"direct-html-input",{locale:o,htmlContent:s})).markdown;return t.createSuccessResult(i)}catch(r){return t.createErrorResult(r)}}async handleSaveContentToFile(e){try{const r=t.parseSaveContentArgs(e),{content:a,save_path:n,save_directory:s,filename:o}=r,i=`File saved successfully to: ${await t.saveContentToFile(a,n,s,o)}`;return t.createSuccessResult(i)}catch(r){return t.createErrorResult(r)}}async handleUrlToMarkdownFile(e){try{const r=t.parseUrlToMarkdownFileArgs(e),{url:n,html_content:s,locale:o="en-US",save_path:i,save_directory:c,filename:l}=r;if(n)try{new URL(n)}catch{throw new Error(`Invalid URL: ${n}`)}const d=(await a.htmlToMarkdown(n||"direct-html-input",{locale:o,htmlContent:s})).markdown;let u=l;!u&&n&&(u=this.generateFilenameFromUrl(n));const h=`URL successfully converted to Markdown and saved to: ${await t.saveContentToFile(d,i,c,u)}`;return t.createSuccessResult(h)}catch(r){return t.createErrorResult(r)}}generateFilenameFromUrl(e){try{let t=new URL(e).pathname.split("/").pop()||"page";return t=t.replace(/\.[^.]*$/,""),t=t.replace(/[<>:"/\\|?*]/g,"_"),t&&!t.match(/^\.+$/)||(t="page"),t}catch{return"page"}}async start(){const e=new s;await this.server.connect(e)}}const i=()=>new o;exports.createMcpServer=i,exports.runMcpServer=async()=>{const e=i();await e.start()};
1
+ const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e});const t=require("./version-D8gIcbbb.cjs"),r=require("node:process"),a=require("./core--XIBQvpW.cjs");class n{append(e){this._buffer=this._buffer?Buffer.concat([this._buffer,e]):e}readMessage(){if(!this._buffer)return null;const e=this._buffer.indexOf("\n");if(-1===e)return null;const r=this._buffer.toString("utf8",0,e).replace(/\r$/,"");return this._buffer=this._buffer.subarray(e+1),function(e){return t.JSONRPCMessageSchema.parse(JSON.parse(e))}(r)}clear(){this._buffer=void 0}}class s{constructor(e=r.stdin,t=r.stdout){this._stdin=e,this._stdout=t,this._readBuffer=new n,this._started=!1,this._ondata=e=>{this._readBuffer.append(e),this.processReadBuffer()},this._onerror=e=>{var t;null===(t=this.onerror)||void 0===t||t.call(this,e)}}async start(){if(this._started)throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");this._started=!0,this._stdin.on("data",this._ondata),this._stdin.on("error",this._onerror)}processReadBuffer(){for(var e,t;;)try{const t=this._readBuffer.readMessage();if(null===t)break;null===(e=this.onmessage)||void 0===e||e.call(this,t)}catch(r){null===(t=this.onerror)||void 0===t||t.call(this,r)}}async close(){var e;this._stdin.off("data",this._ondata),this._stdin.off("error",this._onerror);0===this._stdin.listenerCount("data")&&this._stdin.pause(),this._readBuffer.clear(),null===(e=this.onclose)||void 0===e||e.call(this)}send(e){return new Promise(t=>{const r=function(e){return JSON.stringify(e)+"\n"}(e);this._stdout.write(r)?t():this._stdout.once("drain",t)})}}class o{constructor(){this.version=t.getPackageVersion(),this.server=new t.Server({name:"@aiquants/html-to-markdown",version:this.version,description:"Convert web pages and HTML to Markdown with JavaScript dynamic content support and table structure preservation. Includes separate file saving tool."},{capabilities:{tools:{}}}),this.setupToolHandlers()}setupToolHandlers(){this.server.setRequestHandler(t.ListToolsRequestSchema,async()=>({tools:[{name:"html_to_markdown",description:"Convert web pages or HTML strings to Markdown format. Handles JavaScript-rendered dynamic content and preserves table structures.",inputSchema:t.MCP_TOOL_SCHEMAS.htmlToMarkdown},{name:"save_content_to_file",description:"Save text content to a file with specified path or directory. Supports automatic filename generation and format detection.",inputSchema:t.MCP_TOOL_SCHEMAS.saveContentToFile},{name:"url_to_markdown_file",description:"Convert web pages or HTML strings directly to Markdown files. Combines HTML-to-Markdown conversion and file saving in one step. Handles JavaScript-rendered dynamic content.",inputSchema:t.MCP_TOOL_SCHEMAS.urlToMarkdownFile}]})),this.server.setRequestHandler(t.CallToolRequestSchema,async e=>{switch(e.params.name){case"html_to_markdown":return await this.handleHtmlToMarkdown(e.params.arguments);case"save_content_to_file":return await this.handleSaveContentToFile(e.params.arguments);case"url_to_markdown_file":return await this.handleUrlToMarkdownFile(e.params.arguments);default:throw new Error(`Unknown tool: ${e.params.name}`)}})}async handleHtmlToMarkdown(e){try{const r=t.parseHtmlToMarkdownArgs(e),{url:n,html_content:s,locale:o="en-US"}=r;if(n)try{new URL(n)}catch{throw new Error(`Invalid URL: ${n}`)}const i=(await a.htmlToMarkdown(n||"direct-html-input",{locale:o,htmlContent:s})).markdown;return t.createSuccessResult(i)}catch(r){return t.createErrorResult(r)}}async handleSaveContentToFile(e){try{const r=t.parseSaveContentArgs(e),{content:a,save_path:n,save_directory:s,filename:o}=r,i=`File saved successfully to: ${await t.saveContentToFile(a,n,s,o)}`;return t.createSuccessResult(i)}catch(r){return t.createErrorResult(r)}}async handleUrlToMarkdownFile(e){try{const r=t.parseUrlToMarkdownFileArgs(e),{url:n,html_content:s,locale:o="en-US",save_path:i,save_directory:c,filename:l}=r;if(n)try{new URL(n)}catch{throw new Error(`Invalid URL: ${n}`)}const d=(await a.htmlToMarkdown(n||"direct-html-input",{locale:o,htmlContent:s})).markdown;let u=l;!u&&n&&(u=this.generateFilenameFromUrl(n));const h=`URL successfully converted to Markdown and saved to: ${await t.saveContentToFile(d,i,c,u)}`;return t.createSuccessResult(h)}catch(r){return t.createErrorResult(r)}}generateFilenameFromUrl(e){try{let t=new URL(e).pathname.split("/").pop()||"page";return t=t.replace(/\.[^.]*$/,""),t=t.replace(/[<>:"/\\|?*]/g,"_"),t&&!t.match(/^\.+$/)||(t="page"),t}catch{return"page"}}async start(){const e=new s;await this.server.connect(e)}}const i=()=>new o;exports.createMcpServer=i,exports.runMcpServer=async()=>{const e=i();await e.start()};