Skip to content

Commit 4d1df99

Browse files
authored
Fix img src url (#532)
* Fix img src url * Refactor * Cleanup * Use Path.GetRelativePath * Also use Path.GetRelativePath in DiagnosticLinkeInlineParser * Format * Add explaining comment
1 parent 7baf876 commit 4d1df99

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// See the license.txt file in the project root for more information.
77

88
using Elastic.Markdown.Diagnostics;
9-
using Elastic.Markdown.Myst.CodeBlocks;
109
using Elastic.Markdown.Myst.FrontMatter;
1110
using Elastic.Markdown.Myst.Settings;
1211
using Elastic.Markdown.Myst.Substitution;
@@ -85,11 +84,24 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo
8584

8685
private void WriteImage(HtmlRenderer renderer, ImageBlock block)
8786
{
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+
}
93105
var slice = Image.Create(new ImageViewModel
94106
{
95107
Label = block.Label,

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con
258258
private static string GetRootRelativePath(ParserContext context, IFileInfo file)
259259
{
260260
var docsetDirectory = context.Configuration.SourceFile.Directory;
261-
return file.FullName.Replace(docsetDirectory!.FullName, string.Empty);
261+
return "/" + Path.GetRelativePath(docsetDirectory!.FullName, file.FullName);
262262
}
263263

264264
private static bool IsCrossLink([NotNullWhen(true)] Uri? uri) =>

0 commit comments

Comments
 (0)