-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProgram.cs
87 lines (71 loc) · 2.56 KB
/
Program.cs
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
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
WebRootPath = "markdown"
});
var app = builder.Build();
app.Use((HttpContext context, RequestDelegate next) =>
{
var markdown = new MarkdownMiddleware(next, app.Environment);
return markdown.Invoke(context);
});
app.Run(context =>
{
return context.Response.WriteAsync("\nIf you read this, the file does not exist.");
});
app.Run();
public class MarkdownMiddleware
{
readonly RequestDelegate _next;
readonly IWebHostEnvironment _env;
public MarkdownMiddleware(RequestDelegate next, IWebHostEnvironment env)
{
_next = next;
_env = env;
}
public async Task Invoke(HttpContext context)
{
_env.ContentRootPath = Directory.GetCurrentDirectory();
_env.WebRootPath = Path.Combine(_env.ContentRootPath, "markdown");
var requestPath = context.Request.Path;
//Get default page
if (requestPath == "/")
{
var defaultMd = Path.Combine(_env.WebRootPath, "index.md");
if (!File.Exists(defaultMd))
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("File Not Found");
await _next.Invoke(context);
//write here for post logic
return;
}
//no more processing. This code is shortcircuited.
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(ProduceMarkdown(defaultMd));
return;
}
//Replace the path and remove the beginning \ of the path
//every request path segment represent a folder within markdown folder, e.g.
// /about/us is mapped to markdown\about\us.md File
// /hello is mapped to markdown\hello.md
var localPath = requestPath.ToString().Replace('/', '\\').TrimStart(new char[] { '\\' }) + ".md";
var md = Path.Combine(_env.WebRootPath, localPath);
if (!File.Exists(md))
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("File Not Found");
await _next.Invoke(context);
//write here for post logic
return;
}
//no more processing. This code is shortcircuited.
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(ProduceMarkdown(md));
}
string ProduceMarkdown(string path)
{
var md = File.ReadAllText(path);
var res = Markdig.Markdown.ToHtml(md);
return res;
}
}