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
/**
* This val creates a simple web interface using Hono framework to convert HTML to Markdown.
* It uses HTMX for dynamic updates and Alpine.js for reactivity.
* The Turndown library is used for HTML to Markdown conversion.
* The interface includes two text areas (input and output) and buttons for conversion and copying.
*/
import { Hono } from "https://esm.sh/hono";
import { html } from "https://esm.sh/hono/html";
import TurndownService from "https://esm.sh/turndown";
const app = new Hono();
app.get("/", (c) => {
return c.html(html`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML to Markdown Converter</title>
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
<script src="https://unpkg.com/alpinejs@3.12.2/dist/cdn.min.js" defer></script>
</head>
<body>
<div x-data="{ markdown: '' }">
<h1>HTML to Markdown Converter</h1>
<div>
<h2>HTML Input</h2>
<textarea id="html-input" name="html-input" rows="10" cols="50"></textarea>
</div>
<button hx-post="/convert" hx-trigger="click" hx-target="#markdown-output" hx-include="#html-input">
Convert to Markdown
</button>
<div>
<h2>Markdown Output</h2>
<textarea id="markdown-output" x-model="markdown" rows="10" cols="50" readonly></textarea>
</div>
<button @click="navigator.clipboard.writeText(markdown)">Copy to Clipboard</button>
<p>
<a href="${import.meta.url.replace("esm.town", "val.town")}">View Source</a>
</p>
</div>
</body>
</html>
`);
});
app.post("/convert", async (c) => {
try {
const body = await c.req.text();
const params = new URLSearchParams(body);
let html = params.get("html-input");
if (typeof html !== "string") {
return c.text("Invalid input: HTML must be a string");
}
// Remove script tags and their contents
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
// Remove style tags and their contents
html = html.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "");
// Remove other non-content tags
html = html.replace(/<(meta|link|input|br|hr)[^>]*>/gi, "");
// Remove comments
html = html.replace(/<!--[\s\S]*?-->/g, "");
const turndownService = new TurndownService();
const markdown = turndownService.turndown(html);
// Trim whitespace and remove extra newlines
const cleanedMarkdown = markdown.trim().replace(/\n{3,}/g, "\n\n");
return c.text(cleanedMarkdown);
} catch (error) {
console.error("Error in /convert:", error);
return c.text(`An error occurred: ${error.message}`);
}
});
export default async function server(req: Request): Promise<Response> {
return app.fetch(req);
}