This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
MarkdownViewer.cs
87 lines (74 loc) · 3.18 KB
/
MarkdownViewer.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
// Copyright (c) Nicolas Musset. All rights reserved.
// This file is licensed under the MIT license.
// See the LICENSE.md file in the project root for more information.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace Markdig.Wpf
{
/// <summary>
/// A markdown viewer control.
/// </summary>
public class MarkdownViewer : Control
{
protected static readonly MarkdownPipeline DefaultPipeline = new MarkdownPipelineBuilder().UseSupportedExtensions().Build();
private static readonly DependencyPropertyKey DocumentPropertyKey =
DependencyProperty.RegisterReadOnly(nameof(Document), typeof(FlowDocument), typeof(MarkdownViewer), new FrameworkPropertyMetadata());
/// <summary>
/// Defines the <see cref="Document"/> property.
/// </summary>
public static readonly DependencyProperty DocumentProperty = DocumentPropertyKey.DependencyProperty;
/// <summary>
/// Defines the <see cref="Markdown"/> property.
/// </summary>
public static readonly DependencyProperty MarkdownProperty =
DependencyProperty.Register(nameof(Markdown), typeof(string), typeof(MarkdownViewer), new FrameworkPropertyMetadata(MarkdownChanged));
/// <summary>
/// Defines the <see cref="Markdown"/> property.
/// </summary>
public static readonly DependencyProperty PipelineProperty =
DependencyProperty.Register(nameof(Pipeline), typeof(MarkdownPipeline), typeof(MarkdownViewer), new FrameworkPropertyMetadata(PipelineChanged));
static MarkdownViewer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MarkdownViewer), new FrameworkPropertyMetadata(typeof(MarkdownViewer)));
}
/// <summary>
/// Gets the flow document to display.
/// </summary>
public FlowDocument? Document
{
get { return (FlowDocument)GetValue(DocumentProperty); }
protected set { SetValue(DocumentPropertyKey, value); }
}
/// <summary>
/// Gets or sets the markdown to display.
/// </summary>
public string? Markdown
{
get { return (string)GetValue(MarkdownProperty); }
set { SetValue(MarkdownProperty, value); }
}
/// <summary>
/// Gets or sets the markdown pipeline to use.
/// </summary>
public MarkdownPipeline Pipeline
{
get { return (MarkdownPipeline)GetValue(PipelineProperty); }
set { SetValue(PipelineProperty, value); }
}
private static void MarkdownChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var control = (MarkdownViewer)sender;
control.RefreshDocument();
}
private static void PipelineChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var control = (MarkdownViewer)sender;
control.RefreshDocument();
}
protected virtual void RefreshDocument()
{
Document = Markdown != null ? Wpf.Markdown.ToFlowDocument(Markdown, Pipeline ?? DefaultPipeline) : null;
}
}
}