-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastro.config.mjs
More file actions
148 lines (132 loc) · 5.62 KB
/
astro.config.mjs
File metadata and controls
148 lines (132 loc) · 5.62 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
147
148
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.
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 (!feedUrl) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing url parameter' }));
return;
}
try {
const response = await fetch(feedUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; OriolesNews/1.0)',
Accept: format === 'text'
? 'text/html, text/plain, */*'
: format === 'audio'
? 'audio/*,*/*;q=0.8'
: 'application/rss+xml, application/xml, text/xml, */*',
...(format === 'audio' && req.headers.range ? { Range: req.headers.range } : {}),
},
});
if (format === 'audio') {
const buffer = Buffer.from(await response.arrayBuffer());
res.writeHead(response.status || 200, {
'Content-Type': response.headers.get('content-type') || 'audio/mpeg',
'Content-Length': response.headers.get('content-length') || String(buffer.length),
'Accept-Ranges': response.headers.get('accept-ranges') || 'bytes',
...(response.headers.get('content-range')
? { 'Content-Range': response.headers.get('content-range') }
: {}),
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=900',
});
res.end(buffer);
return;
}
const xml = await response.text();
if (format === 'text') {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
res.end(JSON.stringify({ text: xml }));
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',
'Access-Control-Allow-Origin': '*',
});
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: '/yardreport/',
output: 'static',
build: {
assets: 'assets',
},
vite: {
plugins: [rssProxyDevPlugin()],
build: {
rollupOptions: {
output: {
entryFileNames: 'assets/[name].js',
chunkFileNames: 'assets/[name].js',
assetFileNames: 'assets/[name][extname]',
},
},
},
},
});