File size: 2,799 Bytes
26240b0
 
16adc1a
 
 
 
 
 
 
 
 
 
90a51a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94d9589
 
 
 
 
 
 
 
 
26240b0
 
 
 
 
90a51a8
26240b0
90a51a8
26240b0
94d9589
90a51a8
94d9589
 
 
26240b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a51a8
 
 
 
 
 
 
 
 
 
 
 
26240b0
 
 
 
 
16adc1a
26240b0
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const { convertHtmlToMarkdown } = htmlToSMD;

// Handle copy markdown button
document.getElementById("copy-markdown").addEventListener("click", async function() {
  const markdownOutput = document.getElementById("markdown-output");
  try {
    await navigator.clipboard.writeText(markdownOutput.textContent);
  } catch (err) {
    console.error('Failed to copy text: ', err);
  }
});

// Handle toggle between URL and HTML input
document.getElementById("input-html-toggle").addEventListener("change", function(e) {
  const urlInput = document.getElementById("url-input");
  const htmlInput = document.getElementById("html-input");
  
  if (e.target.checked) {
    urlInput.classList.add("hidden");
    htmlInput.classList.remove("hidden");
    urlInput.removeAttribute("required");
  } else {
    urlInput.classList.remove("hidden");
    htmlInput.classList.add("hidden");
    urlInput.setAttribute("required", "");
  }
});

function isValidUrl(string) {
  try {
    new URL(string);
    return true;
  } catch (_) {
    return false;
  }
}

document
  .getElementById("converter-form")
  .addEventListener("submit", async function (e) {
    e.preventDefault();

    const isHtmlInput = document.getElementById("input-html-toggle").checked;
    const urlInput = document.getElementById("url-input").value;
    const htmlInput = document.getElementById("html-input").value;
    const markdownOutput = document.getElementById("markdown-output");

    if (!isHtmlInput && !isValidUrl(urlInput)) {
      alert("Please enter a valid URL");
      return;
    }
    const extractMainContent = document.getElementById(
      "extract-main-content"
    ).checked;
    const refifyUrls = document.getElementById("refify-urls").checked;
    const enableTableColumnTracking = document.getElementById(
      "enable-table-column-tracking"
    ).checked;
    const websiteDomain = document.getElementById("website-domain").value;

    const options = {
      extractMainContent,
      refifyUrls,
      enableTableColumnTracking,
      websiteDomain: websiteDomain || undefined,
    };

    try {
      let htmlContent;
      if (isHtmlInput) {
        htmlContent = htmlInput;
      } else {
        // Fetch HTML content from the server
        const response = await fetch(
          `/fetch-html?url=${encodeURIComponent(urlInput)}`
        );
        if (!response.ok) {
          throw new Error("Failed to fetch HTML content");
        }
        htmlContent = await response.text();
      }

      // Convert HTML to Markdown
      const markdown = await convertHtmlToMarkdown(htmlContent, options);
      markdownOutput.textContent = markdown;
      document.getElementById("copy-markdown").disabled = false;
    } catch (error) {
      markdownOutput.textContent = "Error: " + error.message;
    }
  });