victor's picture
victor HF staff
Implemented URL validation and alert for invalid URLs before sending the request.
94d9589
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;
}
});