admin管理员组文章数量:1432351
Is there a way to tell Markdig where to look for files relative to the root directory?
I am reading a README.md file from my project's root directory, defined by a hard coded path in C#. This is then converted from Markdown to a FlowDocument via Markdig and displayed as a WPF page. All this works, except the image links in the README.md file do NOT get rendered. I am assuming this is because the the renderer in Markdig can't find them, not knowing to look in relation to the project's root directory. Here is the code:
public partial class ReadmeWindow : Window
{
public ReadmeWindow()
{
InitializeComponent();
Loaded += OnReadmeLoaded;
}
private static Markdig.MarkdownPipeline BuildPipeline()
{
return new Markdig.MarkdownPipelineBuilder()
.UseSupportedExtensions()
.Build();
}
private void OnReadmeLoaded(object sender, RoutedEventArgs e)
{
var projectDirectory = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\"));
var readmePath = Path.Combine(projectDirectory, "README.md");
if (File.Exists(readmePath))
{
var markdown = File.ReadAllText(readmePath);
Markdown.ToXaml(markdown);
var xaml = Markdig.Wpf.Markdown.ToXaml(markdown, BuildPipeline());
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)))
{
using (var reader = new XamlXmlReader(stream, new MyXamlSchemaContext()))
{
if (XamlReader.Load(reader) is FlowDocument document)
{
Viewer.Document = document;
}
}
}
}
else
{
MessageBox.Show("Markdown file not found: " + readmePath);
}
}
You can see in the OnReadmeLoaded() above method where I read the file from the project's root directory.
Is there a way to tell Markdig where to look for files relative to the root directory? For example, with images in project root/Docs:

Is there a way to tell Markdig where to look for files relative to the root directory?
I am reading a README.md file from my project's root directory, defined by a hard coded path in C#. This is then converted from Markdown to a FlowDocument via Markdig and displayed as a WPF page. All this works, except the image links in the README.md file do NOT get rendered. I am assuming this is because the the renderer in Markdig can't find them, not knowing to look in relation to the project's root directory. Here is the code:
public partial class ReadmeWindow : Window
{
public ReadmeWindow()
{
InitializeComponent();
Loaded += OnReadmeLoaded;
}
private static Markdig.MarkdownPipeline BuildPipeline()
{
return new Markdig.MarkdownPipelineBuilder()
.UseSupportedExtensions()
.Build();
}
private void OnReadmeLoaded(object sender, RoutedEventArgs e)
{
var projectDirectory = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\"));
var readmePath = Path.Combine(projectDirectory, "README.md");
if (File.Exists(readmePath))
{
var markdown = File.ReadAllText(readmePath);
Markdown.ToXaml(markdown);
var xaml = Markdig.Wpf.Markdown.ToXaml(markdown, BuildPipeline());
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)))
{
using (var reader = new XamlXmlReader(stream, new MyXamlSchemaContext()))
{
if (XamlReader.Load(reader) is FlowDocument document)
{
Viewer.Document = document;
}
}
}
}
else
{
MessageBox.Show("Markdown file not found: " + readmePath);
}
}
You can see in the OnReadmeLoaded() above method where I read the file from the project's root directory.
Is there a way to tell Markdig where to look for files relative to the root directory? For example, with images in project root/Docs:

Share
Improve this question
asked Nov 18, 2024 at 23:11
Kenny CasonKenny Cason
5576 silver badges18 bronze badges
1 Answer
Reset to default 0Well, I don't know if Markdig has a setting for the "root" directory, I couldn't find one. But, I found a way to make it work.
Given that you have two PNG files in the root/Docs directory you want to access you can set them as Included content in an ItemGroup at compile by adding them to your csproj file. The csproj file would look like this:
<Project Sdk="Microsoft.NET.Sdk">
.
.
.
<ItemGroup>
<Content Include="Docs\keystore.PNG" Link="Docs\keystore.PNG">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Docs\key-details.PNG" Link="Docs\key-details.PNG">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
These Included files will be found by the Markdig parser when written in your Markdown file like this:


Voila!
本文标签: wpfSetting the root directory using Markdig in CStack Overflow
版权声明:本文标题:wpf - Setting the root directory using Markdig in C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745589500a2665113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论