-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (63 loc) · 2.16 KB
/
index.js
File metadata and controls
74 lines (63 loc) · 2.16 KB
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
// index.js (Express server)
const express = require("express");
const path = require("path");
const fetch = require("node-fetch"); // install if needed
const { JSDOM } = require("jsdom"); // install if needed
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files
app.use(express.static(path.join(__dirname, "src")));
// Fetch article route
app.get("/api/fetchArticle", async (req, res) => {
try {
const url = req.query.url;
const resp = await fetch(url);
const html = await resp.text();
// Extract article text via DOM
const dom = new JSDOM(html);
const doc = dom.window.document;
// Try to find article content using common selectors
let articleText = "";
// Try common article selectors
const selectors = [
"article",
'[role="main"]',
".article-content",
".post-content",
".entry-content",
".content",
"main",
"#content"
];
for (const selector of selectors) {
const element = doc.querySelector(selector);
if (element) {
articleText = element.textContent || element.innerText;
if (articleText.length > 200) break;
}
}
// Fallback to body if nothing found
if (!articleText || articleText.length < 200) {
articleText = doc.body.textContent || doc.body.innerText;
}
// Clean up the text
articleText = articleText
.replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]/g, "") // Remove control characters
.replace(/\r\n/g, "\n") // Normalize line endings
.replace(/\r/g, "\n") // Normalize line endings
.replace(/\t/g, " ") // Replace tabs with spaces
.replace(/ +/g, " ") // Replace multiple spaces with single space
.replace(/\n{3,}/g, "\n\n") // Replace 3+ newlines with 2
.trim();
res.json({ text: articleText });
} catch (err) {
console.error("Error fetching article:", err);
res.status(500).json({ error: "Unable to fetch article" });
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "src", "index.html"));
});
app.listen(PORT, () => {
console.log(`🚀 IQCraft running at http://localhost:${PORT}`);
});