Skip to content

Image loading priority

whistyun edited this page Jan 3, 2021 · 1 revision

If you insert an image to markdown, Markdown.Avalonia load the image from indicated path.

By Default, if a path is relative, it attempt to load the image from the resource or filesystem.

First, Markdown.Avalonia tries to read from AvaloniaResource. The assembly names are listed from the MarkdownScrollViewer or Markdown generation process call stack.

Second, Markdown.Avalonia tries to read from the filesystem. the base path of relative path is MarkdownScrollViewer.AssetPathRoot or Markdown.AssetPathRoot. By default, Environment.CurrentDirectory is set.

If you will open a .md file from filesystem. you will probably need two mvvm properties.

View

<md:MarkdownScrollViewer
    AssetPathRoot="{Binding RootDir}"
    Markdown="{Binding Text}" />

ViewModel

    public string Text { ... }
    public string RootDir { ... }

    public void OpenFile(string filepath){
        Text = File.ReadAllText(filepath);
        RootDir = Path.GetDirectoryName(filepath);
    }

If you want to customise load rule. see 'how to customise loader'.