Skip to content

Commit

Permalink
🚔
Browse files Browse the repository at this point in the history
  • Loading branch information
surprisetalk committed Aug 31, 2024
1 parent 384aeee commit c110afb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
29 changes: 28 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ for (const blog of blogs) {
blog.title = (blog.title || "").trim();
blog.desc = (blog.desc || "").trim();
if (!blog.url) continue;
if (blog.title && blog.title === blog.desc) console.log(blog.title);
if (
3 >
0 +
Expand Down Expand Up @@ -90,6 +89,7 @@ const about = `
<p><a href="/">blogs.hn</a> is a directory of tech sites, primarily sourced from <a href="https://news.ycombinator.com">HackerNews</a>.</p>
<p>To submit/update a blog, <a href="https://github.com/surprisetalk/blogs.hn/blob/main/blogs.json">edit blogs.json in a pull-request</a>.</p>
<p>If you like sites with RSS feeds, consider checking out <a href="https://ooh.directory">ooh.directory</a>. If you don't have an RSS reader, I highly recommend <a href="https://reederapp.com">Reeder 5</a>.</p>
<p>You can import all the RSS feeds via <a href="/blogs.hn.opml" download>this OPML file</a>.</p>
<p>And if you aren't already, <em>you should</em> <a href="https://www.benkuhn.net/writing/">write things on the internet</a>. All you need is <a href="https://github.com/surprisetalk/worstpress">worstpress</a> and some markdown files! You can <a href="mailto:[email protected]">email me</a> if you need help getting started.</p>
<p>Every blog is a window into a skull. Don't be afraid to ask questions and kindle friendships! Remember to be kind, courteous, and succinct.</p>
<p>Thanks for stopping by.</p>
Expand All @@ -102,3 +102,30 @@ fs.writeFileSync(
"./dist/about.html",
template.replace("{{body}}", `<main id="about">${about}</main>`)
);

const escxml = unsafe => {
return unsafe.replace(/[<>&'"]/g, (c) => {
switch (c) {
case '<': return '&lt;';
case '>': return '&gt;';
case '&': return '&amp;';
case '\'': return '&apos;';
case '"': return '&quot;';
default: return c;
}
});
};
fs.writeFileSync(
"./dist/blogs.hn.opml",
`<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>blogs.hn</title>
</head>
<body>
${blogs.filter(blog => blog.title && blog.feed && blog.url).map(blog => `<outline type="rss" text="${escxml(blog.title.trim())}" title="${escxml(blog.title.trim())}" htmlUrl="${blog.url.trim()}" xmlUrl="${blog.feed.trim()}" />`.replace(/\s+/gi, " ")).join('\n')}
</body>
</opml>`,
"utf-8"
);

50 changes: 50 additions & 0 deletions feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fetch = require("node-fetch");
const xml2js = require("xml2js");

process.stdin.setEncoding('utf8');

let input = '';
process.stdin.on('data', chunk => input += chunk);

process.stdin.on('end', async () => {
try {
const feeds = JSON.parse(input)
.map(x => x.feed)
.filter(x => x)
.sort(() => Math.random() - 0.5)
.slice(0, 1);

const results = await Promise.all(feeds.map(async (url) => {
try {
console.log(url);
const response = await fetch(url);
const text = await response.text();
const parser = new xml2js.Parser();
const xml = await parser.parseStringPromise(text);

const items = xml.rss ? xml.rss.channel[0].item : xml.feed.entry;
return items.map(item => {
let obj = {};
for (let key in item) {
obj[key] = item[key][0];
}
return obj;
});
} catch (error) {
return [];
}
}));

console.log(results?.[0]);

const output = results
.flatMap(x => x)
.filter(x => x.id || x.guid)
.map(x => `${x.id || x.guid}\t${x.link}`)
.join('\n');

console.log(output);
} catch (error) {
console.error("Error parsing input or processing feeds:", error);
}
});
6 changes: 4 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ body {
}
header {
padding: 1rem 2rem;
display: grid;
display: flex;
gap: 0.5rem;
grid-template-columns: auto 1fr auto auto auto;
align-items: baseline;
}
header > a:first-child {
margin-right: auto;
}
main {
margin: 2rem 2rem 12rem;
}
Expand Down
10 changes: 3 additions & 7 deletions template.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
<body>
<header>
<a href="/"><strong>blogs.hn</strong></a>
<div></div>
<div>
<a id="shuffle" style="display: none" href="#" onclick="shuffle()">
shuffle
</a>
</div>
<a href="https://github.com/surprisetalk/blogs.hn">github</a>
<a href="/about">about</a>
<a href="https://github.com/surprisetalk/blogs.hn">src</a>
<a href="/blogs.hn.opml" download>opml</a>
<a id="shuffle" style="display: none" href="#" onclick="shuffle()">shuf</a>
</header>
{{body}}
</body>
Expand Down

0 comments on commit c110afb

Please sign in to comment.