Skip to content

Commit aeaca27

Browse files
committed
feat: add format param
1 parent f848f18 commit aeaca27

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/cors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ export function jsonResponse(env: Env, data: unknown, status = 200): Response {
2626
});
2727
}
2828

29+
/** Build a plain-text response with CORS headers */
30+
export function textResponse(env: Env, text: string, status = 200): Response {
31+
return new Response(text, {
32+
status,
33+
headers: {
34+
'Content-Type': 'text/markdown; charset=utf-8',
35+
...getCorsHeaders(env),
36+
},
37+
});
38+
}
39+
2940
/** Shorthand for an error JSON response */
3041
export function errorResponse(env: Env, message: string, status = 400): Response {
3142
return jsonResponse(env, { success: false, error: message }, status);

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Response: { success, url, name, mimeType, tokens, markdown }
1212
*/
1313

14-
import { handlePreflight, jsonResponse, errorResponse } from './cors';
14+
import { handlePreflight, jsonResponse, errorResponse, textResponse } from './cors';
1515
import { robustFetch } from './fetch';
1616
import { extractTitle, preprocessHtml } from './html';
1717
import { collectImageUrls, rewriteImageUrls, uploadImages } from './r2';
@@ -42,13 +42,17 @@ export default {
4242

4343
// --- Parse target URL from request ---
4444
let targetUrl: string | null = null;
45+
let rawFormat = false;
4546

4647
if (request.method === 'GET') {
47-
targetUrl = new URL(request.url).searchParams.get('url');
48+
const params = new URL(request.url).searchParams;
49+
targetUrl = params.get('url');
50+
rawFormat = params.get('format') === 'raw';
4851
} else if (request.method === 'POST') {
4952
try {
50-
const body = (await request.json()) as { url?: string };
53+
const body = (await request.json()) as { url?: string; format?: string };
5154
targetUrl = body.url ?? null;
55+
rawFormat = body.format === 'raw';
5256
} catch {
5357
return errorResponse(env, 'Invalid JSON body. Expected: { "url": "https://..." }');
5458
}
@@ -130,6 +134,11 @@ export default {
130134
}
131135
}
132136

137+
// Return raw Markdown text or JSON envelope
138+
if (rawFormat) {
139+
return textResponse(env, markdown);
140+
}
141+
133142
return jsonResponse(env, {
134143
success: true,
135144
url: targetUrl,

0 commit comments

Comments
 (0)