File size: 1,580 Bytes
26240b0
 
94d9589
 
 
 
 
 
 
 
 
26240b0
 
 
 
 
 
 
94d9589
 
 
 
 
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
const { convertHtmlToMarkdown } = htmlToSMD;

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

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

    const urlInput = document.getElementById("url-input").value;
    const markdownOutput = document.getElementById("markdown-output");

    if (!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 {
      // 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");
      }
      const htmlContent = await response.text();

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