|
6 | 6 | // See the license.txt file in the project root for more information.
|
7 | 7 |
|
8 | 8 | using Elastic.Markdown.Diagnostics;
|
9 |
| -using Elastic.Markdown.Myst.CodeBlocks; |
10 | 9 | using Elastic.Markdown.Myst.FrontMatter;
|
11 | 10 | using Elastic.Markdown.Myst.Settings;
|
12 | 11 | using Elastic.Markdown.Myst.Substitution;
|
@@ -85,11 +84,24 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo
|
85 | 84 |
|
86 | 85 | private void WriteImage(HtmlRenderer renderer, ImageBlock block)
|
87 | 86 | {
|
88 |
| - var imageUrl = |
89 |
| - block.ImageUrl != null && |
90 |
| - (block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")) |
91 |
| - ? $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}" |
92 |
| - : block.ImageUrl; |
| 87 | + var imageUrl = block.ImageUrl; |
| 88 | + if (!string.IsNullOrEmpty(block.ImageUrl)) |
| 89 | + { |
| 90 | + if (block.ImageUrl.StartsWith('/') || block.ImageUrl.StartsWith("_static")) |
| 91 | + imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; |
| 92 | + else |
| 93 | + { |
| 94 | + // `block.Build.ConfigurationPath.DirectoryName` is the directory of the docset.yml file |
| 95 | + // which is the root of the documentation source |
| 96 | + // e.g. `/User/username/Projects/docs-builder/docs` |
| 97 | + // `block.CurrentFile.DirectoryName` is the directory of the current markdown file where the image is referenced |
| 98 | + // e.g. `/User/username/Projects/docs-builder/docs/page/with/image` |
| 99 | + // `Path.GetRelativePath` will return the relative path to the docset.yml directory |
| 100 | + // e.g. `page/with/image` |
| 101 | + // Hence the full imageUrl will be something like /path-prefix/page/with/image/image.png |
| 102 | + imageUrl = block.Build.UrlPathPrefix + '/' + Path.GetRelativePath(block.Build.ConfigurationPath.DirectoryName!, block.CurrentFile.DirectoryName!) + "/" + block.ImageUrl; |
| 103 | + } |
| 104 | + } |
93 | 105 | var slice = Image.Create(new ImageViewModel
|
94 | 106 | {
|
95 | 107 | Label = block.Label,
|
|
0 commit comments