forked from superfly/dns-help
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (27 loc) · 1.04 KB
/
index.js
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
import { proxy } from "@fly/edge";
import * as marked from 'marked'
import * as yamlFront from 'yaml-front-matter'
const repo = proxy("https://raw.githubusercontent.com/superfly/dns-help/master/providers/");
fly.http.respondWith((req) => {
const url = new URL(req.url);
if(url.pathname.endsWith("/")){
const sourcePath = url.pathname === "/" ? "index.md" : url.pathname.substring(0, url.pathname.length - 1) + ".md"
const source = new URL(sourcePath, url);
return getMarkdown(source.toString());
}
return repo(req);
})
async function getMarkdown(url){
let resp = await repo(url)
if(resp.status !== 200 && resp.headers.get("content-type") !== "text/plain; charset=utf-8"){
return resp;
}
let body = await resp.text();
const meta = yamlFront.loadFront(body)
if (meta && meta.__content) {
body = meta.__content
meta.__content = undefined
}
const html = `<html><head><base href="/"></head><body>${marked(body)}</body></html>`;
return new Response(html, { status: 200, headers: { "Content-Type": "text/html"}})
}