-
Notifications
You must be signed in to change notification settings - Fork 826
Expand file tree
/
Copy pathMarkdownViewer.cs
More file actions
154 lines (142 loc) · 4.65 KB
/
MarkdownViewer.cs
File metadata and controls
154 lines (142 loc) · 4.65 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
149
150
151
152
153
154
using System.Windows.Forms;
using AutoUpdaterDotNET.ChangelogViewers;
using Markdig;
namespace AutoUpdaterDotNET.Markdown;
/// <summary>
/// A changelog viewer that renders Markdown content using either a provided viewer or a WebBrowser control.
/// </summary>
public class MarkdownViewer : IChangelogViewer
{
private readonly IChangelogViewer _innerViewer;
private readonly bool _cleanup;
private const string DefaultStyle = @"
<style>
@font-face {
font-family: 'Segoe UI Emoji';
src: local('Segoe UI Emoji');
}
body {
font-family: 'Segoe UI Emoji', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-size: 10.5pt;
line-height: 1.4;
word-wrap: break-word;
padding: 12px;
margin: 0;
background-color: #F4F4F4;
color: #000000;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 16px;
margin-bottom: 8px;
font-weight: 600;
line-height: 1.25;
}
h1 { font-size: 20pt; }
h2 { font-size: 16pt; }
h3 { font-size: 14pt; }
h4 { font-size: 12pt; }
p { margin: 8px 0; }
code {
font-family: Consolas, 'Courier New', monospace;
padding: 2px 4px;
background-color: rgba(0, 0, 0, 0.03);
border-radius: 3px;
font-size: 10pt;
}
pre code {
display: block;
padding: 8px;
margin: 8px 0;
overflow: auto;
line-height: 1.45;
}
blockquote {
padding: 0 8px;
margin: 8px 0;
color: #666666;
border-left: 3px solid #CCCCCC;
}
ul, ol {
padding-left: 24px;
margin: 8px 0;
}
table {
border-spacing: 0;
border-collapse: collapse;
margin: 8px 0;
width: 100%;
}
table th, table td {
padding: 4px 8px;
border: 1px solid #CCCCCC;
}
table tr:nth-child(2n) {
background-color: rgba(0, 0, 0, 0.02);
}
hr {
height: 1px;
padding: 0;
margin: 16px 0;
background-color: #CCCCCC;
border: 0;
}
img {
max-width: 100%;
height: auto;
}
a {
color: #0066CC;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>";
/// <summary>
/// Initializes a new instance of the MarkdownViewer class.
/// </summary>
/// <param name="viewer">Optional viewer to use for rendering. If not provided, uses WebBrowser control.</param>
/// <param name="cleanup">Whether to clean up the inner viewer when this viewer is disposed.</param>
public MarkdownViewer(IChangelogViewer viewer = null, bool cleanup = true)
{
_innerViewer = viewer ?? new WebBrowserViewer();
_cleanup = cleanup || viewer == null;
}
/// <inheritdoc />
public Control Control => _innerViewer.Control;
/// <inheritdoc />
public bool SupportsUrl => true;
/// <inheritdoc />
public void LoadContent(string content)
{
if (_innerViewer is RichTextBoxViewer or MarkdownViewer)
{
_innerViewer.LoadContent(content);
return;
}
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.Build();
var html = Markdig.Markdown.ToHtml(content, pipeline);
var fullHtml = $"<!DOCTYPE html><html><head>{DefaultStyle}</head><body>{html}</body></html>";
_innerViewer.LoadContent(fullHtml);
}
/// <inheritdoc />
public void LoadUrl(string url)
{
using var client = new System.Net.WebClient();
if (AutoUpdater.BasicAuthChangeLog != null)
{
var auth = (BasicAuthentication)AutoUpdater.BasicAuthChangeLog;
client.Credentials = new System.Net.NetworkCredential(auth.Username, auth.Password);
}
var content = client.DownloadString(url);
LoadContent(content);
}
/// <inheritdoc />
public void Cleanup()
{
if (_cleanup)
_innerViewer.Cleanup();
}
}