@aiquants/html-to-markdown 0.5.0 → 0.5.2

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.
@@ -20418,6 +20418,335 @@ const rehypeAbsoluteLinks = (options) => {
20418
20418
  });
20419
20419
  };
20420
20420
  };
20421
+ const rehypeDecodeEntities = () => {
20422
+ return (tree) => {
20423
+ visit(tree, "text", (node2) => {
20424
+ node2.value = decodeHtmlEntities(node2.value);
20425
+ });
20426
+ visit(tree, "element", (node2) => {
20427
+ if (node2.properties) {
20428
+ for (const [key2, value] of Object.entries(node2.properties)) {
20429
+ if (typeof value === "string") {
20430
+ node2.properties[key2] = decodeHtmlEntities(value);
20431
+ }
20432
+ }
20433
+ }
20434
+ });
20435
+ };
20436
+ };
20437
+ const preprocessHtmlEntities = (html2) => {
20438
+ return decodeHtmlEntities(html2);
20439
+ };
20440
+ function decodeHtmlEntities(text2) {
20441
+ if (!text2 || typeof text2 !== "string") {
20442
+ return text2;
20443
+ }
20444
+ text2 = text2.replace(/&#(\d+);/g, (match, dec) => {
20445
+ try {
20446
+ const codePoint = parseInt(dec, 10);
20447
+ if (codePoint >= 0 && codePoint <= 1114111) {
20448
+ return String.fromCodePoint(codePoint);
20449
+ }
20450
+ return match;
20451
+ } catch {
20452
+ return match;
20453
+ }
20454
+ });
20455
+ text2 = text2.replace(/&#[xX]([0-9a-fA-F]+);/g, (match, hex) => {
20456
+ try {
20457
+ const codePoint = parseInt(hex, 16);
20458
+ if (codePoint >= 0 && codePoint <= 1114111) {
20459
+ return String.fromCodePoint(codePoint);
20460
+ }
20461
+ return match;
20462
+ } catch {
20463
+ return match;
20464
+ }
20465
+ });
20466
+ const namedEntities = getComprehensiveNamedEntities();
20467
+ for (const [entity, character] of Object.entries(namedEntities)) {
20468
+ const regex2 = new RegExp(entity.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g");
20469
+ text2 = text2.replace(regex2, character);
20470
+ }
20471
+ return text2;
20472
+ }
20473
+ function getComprehensiveNamedEntities() {
20474
+ return {
20475
+ // 基本的なHTMLエンティティ(大文字小文字両対応)
20476
+ "&amp;": "&",
20477
+ "&AMP;": "&",
20478
+ "&lt;": "<",
20479
+ "&LT;": "<",
20480
+ "&gt;": ">",
20481
+ "&GT;": ">",
20482
+ "&quot;": '"',
20483
+ "&QUOT;": '"',
20484
+ "&apos;": "'",
20485
+ "&APOS;": "'",
20486
+ "&nbsp;": " ",
20487
+ "&NBSP;": " ",
20488
+ // 数学記号
20489
+ "&times;": "×",
20490
+ "&divide;": "÷",
20491
+ "&minus;": "−",
20492
+ "&plusmn;": "±",
20493
+ "&sup1;": "¹",
20494
+ "&sup2;": "²",
20495
+ "&sup3;": "³",
20496
+ "&frac14;": "¼",
20497
+ "&frac12;": "½",
20498
+ "&frac34;": "¾",
20499
+ "&infin;": "∞",
20500
+ "&sum;": "∑",
20501
+ "&prod;": "∏",
20502
+ "&radic;": "√",
20503
+ "&prop;": "∝",
20504
+ "&part;": "∂",
20505
+ "&int;": "∫",
20506
+ "&ang;": "∠",
20507
+ "&perp;": "⊥",
20508
+ "&parallel;": "∥",
20509
+ // ギリシャ文字
20510
+ "&Alpha;": "Α",
20511
+ "&alpha;": "α",
20512
+ "&Beta;": "Β",
20513
+ "&beta;": "β",
20514
+ "&Gamma;": "Γ",
20515
+ "&gamma;": "γ",
20516
+ "&Delta;": "Δ",
20517
+ "&delta;": "δ",
20518
+ "&Epsilon;": "Ε",
20519
+ "&epsilon;": "ε",
20520
+ "&Zeta;": "Ζ",
20521
+ "&zeta;": "ζ",
20522
+ "&Eta;": "Η",
20523
+ "&eta;": "η",
20524
+ "&Theta;": "Θ",
20525
+ "&theta;": "θ",
20526
+ "&Iota;": "Ι",
20527
+ "&iota;": "ι",
20528
+ "&Kappa;": "Κ",
20529
+ "&kappa;": "κ",
20530
+ "&Lambda;": "Λ",
20531
+ "&lambda;": "λ",
20532
+ "&Mu;": "Μ",
20533
+ "&mu;": "μ",
20534
+ "&Nu;": "Ν",
20535
+ "&nu;": "ν",
20536
+ "&Xi;": "Ξ",
20537
+ "&xi;": "ξ",
20538
+ "&Omicron;": "Ο",
20539
+ "&omicron;": "ο",
20540
+ "&Pi;": "Π",
20541
+ "&pi;": "π",
20542
+ "&Rho;": "Ρ",
20543
+ "&rho;": "ρ",
20544
+ "&Sigma;": "Σ",
20545
+ "&sigma;": "σ",
20546
+ "&Tau;": "Τ",
20547
+ "&tau;": "τ",
20548
+ "&Upsilon;": "Υ",
20549
+ "&upsilon;": "υ",
20550
+ "&Phi;": "Φ",
20551
+ "&phi;": "φ",
20552
+ "&Chi;": "Χ",
20553
+ "&chi;": "χ",
20554
+ "&Psi;": "Ψ",
20555
+ "&psi;": "ψ",
20556
+ "&Omega;": "Ω",
20557
+ "&omega;": "ω",
20558
+ // 通貨記号
20559
+ "&euro;": "€",
20560
+ "&yen;": "¥",
20561
+ "&pound;": "£",
20562
+ "&cent;": "¢",
20563
+ "&curren;": "¤",
20564
+ // 著作権・商標(大文字小文字両対応)
20565
+ "&copy;": "©",
20566
+ "&COPY;": "©",
20567
+ "&reg;": "®",
20568
+ "&REG;": "®",
20569
+ "&trade;": "™",
20570
+ "&TRADE;": "™",
20571
+ // 句読点・記号
20572
+ "&hellip;": "…",
20573
+ "&mdash;": "—",
20574
+ "&ndash;": "–",
20575
+ "&lsquo;": "‘",
20576
+ "&rsquo;": "’",
20577
+ "&ldquo;": "“",
20578
+ "&rdquo;": "”",
20579
+ "&bull;": "•",
20580
+ "&middot;": "·",
20581
+ "&sect;": "§",
20582
+ "&para;": "¶",
20583
+ "&dagger;": "†",
20584
+ "&Dagger;": "‡",
20585
+ "&permil;": "‰",
20586
+ "&lsaquo;": "‹",
20587
+ "&rsaquo;": "›",
20588
+ "&laquo;": "«",
20589
+ "&raquo;": "»",
20590
+ // アクセント文字(ラテン文字)
20591
+ "&Agrave;": "À",
20592
+ "&agrave;": "à",
20593
+ "&Aacute;": "Á",
20594
+ "&aacute;": "á",
20595
+ "&Acirc;": "Â",
20596
+ "&acirc;": "â",
20597
+ "&Atilde;": "Ã",
20598
+ "&atilde;": "ã",
20599
+ "&Auml;": "Ä",
20600
+ "&auml;": "ä",
20601
+ "&Aring;": "Å",
20602
+ "&aring;": "å",
20603
+ "&AElig;": "Æ",
20604
+ "&aelig;": "æ",
20605
+ "&Ccedil;": "Ç",
20606
+ "&ccedil;": "ç",
20607
+ "&Egrave;": "È",
20608
+ "&egrave;": "è",
20609
+ "&Eacute;": "É",
20610
+ "&eacute;": "é",
20611
+ "&Ecirc;": "Ê",
20612
+ "&ecirc;": "ê",
20613
+ "&Euml;": "Ë",
20614
+ "&euml;": "ë",
20615
+ "&Igrave;": "Ì",
20616
+ "&igrave;": "ì",
20617
+ "&Iacute;": "Í",
20618
+ "&iacute;": "í",
20619
+ "&Icirc;": "Î",
20620
+ "&icirc;": "î",
20621
+ "&Iuml;": "Ï",
20622
+ "&iuml;": "ï",
20623
+ "&ETH;": "Ð",
20624
+ "&eth;": "ð",
20625
+ "&Ntilde;": "Ñ",
20626
+ "&ntilde;": "ñ",
20627
+ "&Ograve;": "Ò",
20628
+ "&ograve;": "ò",
20629
+ "&Oacute;": "Ó",
20630
+ "&oacute;": "ó",
20631
+ "&Ocirc;": "Ô",
20632
+ "&ocirc;": "ô",
20633
+ "&Otilde;": "Õ",
20634
+ "&otilde;": "õ",
20635
+ "&Ouml;": "Ö",
20636
+ "&ouml;": "ö",
20637
+ "&Oslash;": "Ø",
20638
+ "&oslash;": "ø",
20639
+ "&Ugrave;": "Ù",
20640
+ "&ugrave;": "ù",
20641
+ "&Uacute;": "Ú",
20642
+ "&uacute;": "ú",
20643
+ "&Ucirc;": "Û",
20644
+ "&ucirc;": "û",
20645
+ "&Uuml;": "Ü",
20646
+ "&uuml;": "ü",
20647
+ "&Yacute;": "Ý",
20648
+ "&yacute;": "ý",
20649
+ "&THORN;": "Þ",
20650
+ "&thorn;": "þ",
20651
+ "&szlig;": "ß",
20652
+ "&yuml;": "ÿ",
20653
+ // 矢印
20654
+ "&larr;": "←",
20655
+ "&uarr;": "↑",
20656
+ "&rarr;": "→",
20657
+ "&darr;": "↓",
20658
+ "&harr;": "↔",
20659
+ "&crarr;": "↵",
20660
+ "&lArr;": "⇐",
20661
+ "&uArr;": "⇑",
20662
+ "&rArr;": "⇒",
20663
+ "&dArr;": "⇓",
20664
+ "&hArr;": "⇔",
20665
+ // 数学記号(追加)
20666
+ "&forall;": "∀",
20667
+ "&exist;": "∃",
20668
+ "&empty;": "∅",
20669
+ "&nabla;": "∇",
20670
+ "&isin;": "∈",
20671
+ "&notin;": "∉",
20672
+ "&ni;": "∋",
20673
+ "&cap;": "∩",
20674
+ "&cup;": "∪",
20675
+ "&sub;": "⊂",
20676
+ "&sup;": "⊃",
20677
+ "&nsub;": "⊄",
20678
+ "&sube;": "⊆",
20679
+ "&supe;": "⊇",
20680
+ "&oplus;": "⊕",
20681
+ "&otimes;": "⊗",
20682
+ "&equiv;": "≡",
20683
+ "&ne;": "≠",
20684
+ "&le;": "≤",
20685
+ "&ge;": "≥",
20686
+ "&sim;": "∼",
20687
+ "&cong;": "≅",
20688
+ "&asymp;": "≈",
20689
+ // スペード・ハート・ダイヤ・クラブ
20690
+ "&spades;": "♠",
20691
+ "&hearts;": "♥",
20692
+ "&diams;": "♦",
20693
+ "&clubs;": "♣",
20694
+ // 追加の特殊文字
20695
+ "&loz;": "◊",
20696
+ "&image;": "ℑ",
20697
+ "&real;": "ℜ",
20698
+ "&weierp;": "℘",
20699
+ "&alefsym;": "ℵ",
20700
+ // 日本語・中国語・韓国語特有のエンティティ
20701
+ "&#xff08;": "(",
20702
+ // 全角左括弧
20703
+ "&#xff09;": ")",
20704
+ // 全角右括弧
20705
+ "&#x30b7;": "シ",
20706
+ // カタカナのシ
20707
+ "&#x30B7;": "シ",
20708
+ // カタカナのシ(大文字)
20709
+ "&#x3042;": "あ",
20710
+ // ひらがなのあ
20711
+ "&#x3044;": "い",
20712
+ // ひらがなのい
20713
+ "&#x3046;": "う",
20714
+ // ひらがなのう
20715
+ "&#x3048;": "え",
20716
+ // ひらがなのえ
20717
+ "&#x304a;": "お",
20718
+ // ひらがなのお
20719
+ // 中国語の文字
20720
+ "&#x4e2d;": "中",
20721
+ // 中
20722
+ "&#x6587;": "文",
20723
+ // 文
20724
+ "&#x56fd;": "国",
20725
+ // 国
20726
+ // 韓国語の文字
20727
+ "&#xd55c;": "한",
20728
+ // 한
20729
+ "&#xad6d;": "국",
20730
+ // 국
20731
+ "&#xc5b4;": "어",
20732
+ // 어
20733
+ // その他のUnicode文字
20734
+ "&ensp;": " ",
20735
+ // En space
20736
+ "&emsp;": " ",
20737
+ // Em space
20738
+ "&thinsp;": " ",
20739
+ // Thin space
20740
+ "&zwnj;": "‌",
20741
+ // Zero width non-joiner
20742
+ "&zwj;": "‍",
20743
+ // Zero width joiner
20744
+ "&lrm;": "‎",
20745
+ // Left-to-right mark
20746
+ "&rlm;": "‏"
20747
+ // Right-to-left mark
20748
+ };
20749
+ }
20421
20750
  const blockLevelTags = /* @__PURE__ */ new Set([
20422
20751
  "address",
20423
20752
  "article",
@@ -20687,7 +21016,8 @@ const handleTableCell = (_state, node2, _parent) => {
20687
21016
  const convertHtmlToMarkdown = async (html2, baseUrl, locale) => {
20688
21017
  console.info(getMessage(locale, "convert_start"));
20689
21018
  const slugger2 = new BananaSlug();
20690
- const file = await unified().use(rehypeParse, { fragment: true }).use(rehypeRaw).use(rehypeSanitizeHtml).use(rehypeWikipediaFootnotes).use(rehypeNamedAnchors).use(rehypeSlug).use(rehypeParagraphWrapper).use(rehypeAbsoluteLinks, { baseUrl }).use(rehypeRemark, {
21019
+ const preprocessedHtml = preprocessHtmlEntities(html2);
21020
+ const file = await unified().use(rehypeParse, { fragment: true }).use(rehypeRaw).use(rehypeDecodeEntities).use(rehypeSanitizeHtml).use(rehypeWikipediaFootnotes).use(rehypeNamedAnchors).use(rehypeSlug).use(rehypeParagraphWrapper).use(rehypeAbsoluteLinks, { baseUrl }).use(rehypeRemark, {
20691
21021
  handlers: {
20692
21022
  span(state, node2) {
20693
21023
  const id = node2.properties?.id;
@@ -20784,11 +21114,12 @@ const convertHtmlToMarkdown = async (html2, baseUrl, locale) => {
20784
21114
  return value.replace(/\n/g, "").replace(/\|/g, "\\|");
20785
21115
  }
20786
21116
  }
20787
- }).process(html2);
21117
+ }).process(preprocessedHtml);
20788
21118
  const unescapedMarkdown = String(file).replace(/\\\[\^(.+?)\\?\](?!\s*:)/g, "[^$1]").replace(/\]\(\\#/g, "](#");
20789
21119
  const correctedMarkdown = unescapedMarkdown.replace(/(\s*)- - /g, "$1- ").replace(/(\s*)\* \* /g, "$1* ");
21120
+ const finalMarkdown = preprocessHtmlEntities(correctedMarkdown);
20790
21121
  console.info(getMessage(locale, "convert_success"));
20791
- return correctedMarkdown;
21122
+ return finalMarkdown;
20792
21123
  };
20793
21124
  const htmlToMarkdown = async (urlOrHtml, options = {}) => {
20794
21125
  const { locale = "en-US", htmlContent } = options;
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-Dq4LfiYy.cjs"),t=require("./mcp-server-CMBUHOmc.cjs"),c=require("./mcp-server-streamable-IQlimU2H.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-Dn3EHzcf.cjs"),c=require("./mcp-server-streamable-DUB_ixZe.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
@@ -5,9 +5,9 @@ Object.assign(global, {
5
5
  window,
6
6
  self: window
7
7
  });
8
- import { g, h } from "./core-DJV8t_Qj.js";
9
- import { c, r } from "./mcp-server-DD8CqdNw.js";
10
- import { c as c2, r as r2 } from "./mcp-server-streamable-DODolSbj.js";
8
+ import { g, h } from "./core-DhxxOcPm.js";
9
+ import { c, r } from "./mcp-server-DOe8W9IZ.js";
10
+ import { c as c2, r as r2 } from "./mcp-server-streamable-pcpU_lDl.js";
11
11
  export {
12
12
  c as createMcpServer,
13
13
  c2 as createStreamableMcpServer,
package/dist/main.cjs CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty;const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e});const t=require("./core-Dq4LfiYy.cjs");var r="undefined"!=typeof document?document.currentScript:null;(async()=>{(("undefined"==typeof document?require("url").pathToFileURL(__filename).href:r&&"SCRIPT"===r.tagName.toUpperCase()&&r.src||new URL("main.cjs",document.baseURI).href).endsWith(process.argv[1])||process.argv[1]&&process.argv[1].includes("vite-node"))&&(async()=>{const e=(await import("yargs-parser")).default,r=await import("node:fs"),o=await import("node:path"),c=e(process.argv.slice(2),{string:["locale","output","html-content"],alias:{output:["o"],"html-content":["h"]},default:{locale:"en-US"}}),n=c._[0],s=c.locale,a=c.output,i=c["html-content"];if(n||i||process.exit(1),n&&!i)try{new URL(n)}catch(p){process.exit(1)}try{const{markdown:e,html:c}=await t.htmlToMarkdown(n||"direct-html-input",{locale:s,htmlContent:i});let p;if(a){p=o.resolve(process.cwd(),a);const e=o.dirname(p);r.existsSync(e)||r.mkdirSync(e,{recursive:!0})}else{let e;e=i?"html_content":n.replace(/https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"_");const t=`${e}_${Date.now()}.md`,c=o.join(process.cwd(),".outputs","raw");r.existsSync(c)||r.mkdirSync(c,{recursive:!0}),p=o.join(c,t)}r.writeFileSync(p,e);const l=o.parse(p),d=o.join(l.dir,`${l.name}.html`);r.writeFileSync(d,c)}catch(p){process.exit(1)}})()})();
2
+ Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty;const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e});const t=require("./core--XIBQvpW.cjs");var r="undefined"!=typeof document?document.currentScript:null;(async()=>{(("undefined"==typeof document?require("url").pathToFileURL(__filename).href:r&&"SCRIPT"===r.tagName.toUpperCase()&&r.src||new URL("main.cjs",document.baseURI).href).endsWith(process.argv[1])||process.argv[1]&&process.argv[1].includes("vite-node"))&&(async()=>{const e=(await import("yargs-parser")).default,r=await import("node:fs"),o=await import("node:path"),c=e(process.argv.slice(2),{string:["locale","output","html-content"],alias:{output:["o"],"html-content":["h"]},default:{locale:"en-US"}}),n=c._[0],s=c.locale,a=c.output,i=c["html-content"];if(n||i||process.exit(1),n&&!i)try{new URL(n)}catch(p){process.exit(1)}try{const{markdown:e,html:c}=await t.htmlToMarkdown(n||"direct-html-input",{locale:s,htmlContent:i});let p;if(a){p=o.resolve(process.cwd(),a);const e=o.dirname(p);r.existsSync(e)||r.mkdirSync(e,{recursive:!0})}else{let e;e=i?"html_content":n.replace(/https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"_");const t=`${e}_${Date.now()}.md`,c=o.join(process.cwd(),".outputs","raw");r.existsSync(c)||r.mkdirSync(c,{recursive:!0}),p=o.join(c,t)}r.writeFileSync(p,e);const l=o.parse(p),d=o.join(l.dir,`${l.name}.html`);r.writeFileSync(d,c)}catch(p){process.exit(1)}})()})();
package/dist/main.js CHANGED
@@ -6,7 +6,7 @@ Object.assign(global, {
6
6
  window,
7
7
  self: window
8
8
  });
9
- import { g as getMessage, h as htmlToMarkdown } from "./core-DJV8t_Qj.js";
9
+ import { g as getMessage, h as htmlToMarkdown } from "./core-DhxxOcPm.js";
10
10
  const runCli = async () => {
11
11
  const yargsParser = (await import("yargs-parser")).default;
12
12
  const fs = await import("node:fs");
@@ -5,9 +5,9 @@ Object.assign(global, {
5
5
  window,
6
6
  self: window
7
7
  });
8
- import { J as JSONRPCMessageSchema, g as getPackageVersion, S as Server, L as ListToolsRequestSchema, M as MCP_TOOL_SCHEMAS, C as CallToolRequestSchema, p as parseHtmlToMarkdownArgs, c as createSuccessResult, a as createErrorResult, b as parseSaveContentArgs, s as saveContentToFile, d as parseUrlToMarkdownFileArgs } from "./version-CTdrYd1G.js";
8
+ import { J as JSONRPCMessageSchema, g as getPackageVersion, S as Server, L as ListToolsRequestSchema, M as MCP_TOOL_SCHEMAS, C as CallToolRequestSchema, p as parseHtmlToMarkdownArgs, c as createSuccessResult, a as createErrorResult, b as parseSaveContentArgs, s as saveContentToFile, d as parseUrlToMarkdownFileArgs } from "./version-BNwQMbri.js";
9
9
  import process from "node:process";
10
- import { h as htmlToMarkdown } from "./core-DJV8t_Qj.js";
10
+ import { h as htmlToMarkdown } from "./core-DhxxOcPm.js";
11
11
  class ReadBuffer {
12
12
  append(chunk) {
13
13
  this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
@@ -1 +1 @@
1
- const e=new(require("happy-dom").Window);Object.assign(global,{document:e.document,window:e,self:e});const t=require("./version-BkgGdAYt.cjs"),r=require("node:process"),a=require("./core-Dq4LfiYy.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-BsWiDlbg.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()};