Repository navigation
Expand file tree
/
Copy pathastro.config.mjs
More file actions
146 lines (129 loc) · 5.44 KB
/
Copy pathastro.config.mjs
File metadata and controls
146 lines (129 loc) · 5.44 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { defineConfig } from 'astro/config';
// Dev-only plugin: proxies rss-proxy.php requests so the static PHP file
// doesn't need a running PHP server during local development. No CORS
// header, so other sites open in the browser can't read through it.
function rssProxyDevPlugin() {
return {
name: 'rss-proxy-dev',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (!req.url?.includes('rss-proxy.php')) return next();
const { searchParams } = new URL(req.url, 'http://localhost');
const feedUrl = searchParams.get('url');
const format = searchParams.get('format');
if (!/^https?:\/\//i.test(feedUrl ?? '')) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing or invalid url parameter' }));
return;
}
try {
const response = await fetch(feedUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Fanbase/1.0)',
Accept: format === 'text' || format === 'og'
? 'text/html, text/plain, */*'
: 'application/rss+xml, application/xml, text/xml, */*',
},
});
const xml = await response.text();
if (format === 'text') {
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ text: xml }));
return;
}
if (format === 'og') {
const image = xml.match(/<meta[^>]+property=["']og:image["'][^>]+content=["']([^"']+)["']/)?.[1]
?? xml.match(/<meta[^>]+content=["']([^"']+)["'][^>]+property=["']og:image["']/)?.[1]
?? null;
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ image }));
return;
}
// Extract text content of a single XML tag, handling CDATA
function extractTag(src, tag) {
const re = new RegExp(
`<${tag}[^>]*>\\s*(?:<!\\[CDATA\\[([\\s\\S]*?)\\]\\]>|([\\s\\S]*?))<\\/${tag}>`,
'i'
);
return (src.match(re)?.[1] ?? src.match(re)?.[2] ?? '').trim();
}
const items = [];
for (const [, itemXml] of xml.matchAll(/<item>([\s\S]*?)<\/item>/g)) {
if (items.length >= 20) break;
const title = extractTag(itemXml, 'title');
const pubDate = extractTag(itemXml, 'pubDate');
// Link: plain text or Atom href attribute
let link = extractTag(itemXml, 'link');
if (!link) {
link = itemXml.match(/<link[^>]+href="([^"]+)"/)?.[1] ?? '';
}
// Description — strip HTML tags, truncate
const rawDesc = extractTag(itemXml, 'description');
const plainDesc = rawDesc.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim();
const description =
plainDesc.length > 297 ? plainDesc.slice(0, 297) + '…' : plainDesc;
const content = extractTag(itemXml, 'content:encoded');
// Thumbnail: media:content/thumbnail → enclosure → first <img> in content
let thumbnail = null;
const mediaUrl = itemXml.match(/<media:(?:content|thumbnail)[^>]+url="([^"]+)"/)?.[1];
if (mediaUrl) thumbnail = mediaUrl;
if (!thumbnail) {
const enc = itemXml.match(/<enclosure[^>]+type="image\/[^"]*"[^>]+url="([^"]+)"/);
if (enc) thumbnail = enc[1];
}
if (!thumbnail && content) {
thumbnail = content.match(/<img[^>]+src=["']([^"']+)["']/)?.[1] ?? null;
}
const audioMatch = itemXml.match(/<enclosure[^>]+type="(audio\/[^"]*)"[^>]+url="([^"]+)"/i)
?? itemXml.match(/<enclosure[^>]+url="([^"]+)"[^>]+type="(audio\/[^"]*)"/i);
const audioUrl = audioMatch
? (audioMatch[2]?.startsWith('http') ? audioMatch[2] : audioMatch[1]) ?? null
: null;
const audioType = audioMatch
? (audioMatch[1]?.startsWith('audio/') ? audioMatch[1] : audioMatch[2]) ?? null
: null;
items.push({ title, link, pubDate, description, content, thumbnail, audioUrl, audioType });
}
const feedTitle = extractTag(xml.match(/<channel>([\s\S]*)/)?.[1] ?? '', 'title');
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ feedTitle, items }));
} catch {
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to fetch feed' }));
}
});
},
};
}
export default defineConfig({
site: 'https://www.briancsmith.org',
base: '/fanbase/',
output: 'static',
// The news page moved from /news/ to /dugout/. .htaccess answers /news/
// with a 301 on the live server; this builds a /news/ page that forwards
// too, for the dev server or a host that ignores .htaccess.
redirects: {
'/news': '/fanbase/dugout/',
},
build: {
assets: 'assets',
},
vite: {
plugins: [rssProxyDevPlugin()],
build: {
rolldownOptions: {
output: {
entryFileNames: 'assets/[name].js',
chunkFileNames: 'assets/[name].js',
assetFileNames: 'assets/[name][extname]',
},
},
},
},
});