wpf - Setting the root directory using Markdig in C# - Stack Overflow

Is there a way to tell Markdig where to look for files relative to the root directory?I am reading a R

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:

![key details](Docs/key-details.PNG)

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:

![key details](Docs/key-details.PNG)
Share Improve this question asked Nov 18, 2024 at 23:11 Kenny CasonKenny Cason 5576 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Well, 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:

![keystore](Docs/keystore.PNG)
![key details](Docs/key-details.PNG)

Voila!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745589501a4634747.html

相关推荐

  • wpf - Setting the root directory using Markdig in C# - Stack Overflow

    Is there a way to tell Markdig where to look for files relative to the root directory?I am reading a R

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信