import pretty from "npm:pretty";
const title = "HTML Formatter";
const html = (url: string) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
textarea {
width: 100%;
height: 300px;
margin-top: 10px;
}
button {
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>${title}</h1>
<textarea id="inputText"></textarea>
<button id="transformButton">Transform</button>
<script>
async function transform() {
const textarea = document.getElementById('inputText');
const text = textarea.value;
try {
const response = await fetch('${url}', {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: text
});
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const data = await response.text();
textarea.value = data;
} catch (error) {
console.error('Error during fetch:', error);
alert('There was an error processing your request.');
}
}
window.addEventListener("keydown", (e) => {
if (e.metaKey && e.key === "Enter") {
e.preventDefault();
transform()
}
});
document.getElementById('transformButton').addEventListener('click', transform);
</script>
</body>
</html>
`;
export default async function(req: Request): Promise<Response> {
if (req.method === "POST") {
const input = await req.text();
return new Response(pretty(input), {
headers: { "Content-Type": "text/plain" },
});
}
return new Response(html(req.url), {
headers: { "Content-Type": "text/html" },
});
}