From a590bb2a20e7d7772a2fab0f6d116baf6b82e426 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 14:57:06 +0100 Subject: [PATCH 1/7] Fix img src url --- .../Myst/Directives/DirectiveHtmlRenderer.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 9fb5155fc..bb5f182b0 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -5,6 +5,7 @@ // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. +using System.IO.Abstractions; using Elastic.Markdown.Diagnostics; using Elastic.Markdown.Myst.CodeBlocks; using Elastic.Markdown.Myst.FrontMatter; @@ -12,6 +13,7 @@ using Elastic.Markdown.Myst.Substitution; using Elastic.Markdown.Slices.Directives; using Markdig; +using Markdig.Parsers; using Markdig.Renderers; using Markdig.Renderers.Html; using Markdig.Syntax; @@ -83,13 +85,28 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo } } + private static string GetRootRelativePath(ParserContext context, IFileInfo file) + { + var docsetDirectory = context.Configuration.SourceFile.Directory; + return file.FullName.Replace(docsetDirectory!.FullName, string.Empty); + } + private void WriteImage(HtmlRenderer renderer, ImageBlock block) { - var imageUrl = - block.ImageUrl != null && - (block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")) - ? $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}" - : block.ImageUrl; + string imageUrl; + + if (block.ImageUrl != null) + { + if (block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")) + imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; + else if (block.ImageUrl.StartsWith('/')) + imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; + else + imageUrl = block.Build.UrlPathPrefix + block.CurrentFile.DirectoryName!.Replace(block.Build.ConfigurationPath.DirectoryName!, "") + "/" + block.ImageUrl; + } + else + imageUrl = block.ImageUrl!; + var slice = Image.Create(new ImageViewModel { Label = block.Label, From 6605ced134de04c4f4175fde74739e617c4a7b3f Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:04:55 +0100 Subject: [PATCH 2/7] Refactor --- .../Myst/Directives/DirectiveHtmlRenderer.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index bb5f182b0..603764aed 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -93,20 +93,14 @@ private static string GetRootRelativePath(ParserContext context, IFileInfo file) private void WriteImage(HtmlRenderer renderer, ImageBlock block) { - string imageUrl; - - if (block.ImageUrl != null) + var imageUrl = block.ImageUrl; + if (!string.IsNullOrEmpty(block.ImageUrl)) { - if (block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")) - imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; - else if (block.ImageUrl.StartsWith('/')) + if (block.ImageUrl.StartsWith('/') || block.ImageUrl.StartsWith("_static")) imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; else imageUrl = block.Build.UrlPathPrefix + block.CurrentFile.DirectoryName!.Replace(block.Build.ConfigurationPath.DirectoryName!, "") + "/" + block.ImageUrl; } - else - imageUrl = block.ImageUrl!; - var slice = Image.Create(new ImageViewModel { Label = block.Label, From 37c9256da33b4a6079085eba07e1ab76549b06ee Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:07:03 +0100 Subject: [PATCH 3/7] Cleanup --- .../Myst/Directives/DirectiveHtmlRenderer.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 603764aed..e5896b86b 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -5,15 +5,12 @@ // This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. -using System.IO.Abstractions; using Elastic.Markdown.Diagnostics; -using Elastic.Markdown.Myst.CodeBlocks; using Elastic.Markdown.Myst.FrontMatter; using Elastic.Markdown.Myst.Settings; using Elastic.Markdown.Myst.Substitution; using Elastic.Markdown.Slices.Directives; using Markdig; -using Markdig.Parsers; using Markdig.Renderers; using Markdig.Renderers.Html; using Markdig.Syntax; @@ -85,12 +82,6 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo } } - private static string GetRootRelativePath(ParserContext context, IFileInfo file) - { - var docsetDirectory = context.Configuration.SourceFile.Directory; - return file.FullName.Replace(docsetDirectory!.FullName, string.Empty); - } - private void WriteImage(HtmlRenderer renderer, ImageBlock block) { var imageUrl = block.ImageUrl; From 73a3598737ff5af45c36c9b9a38bb85a521d8f56 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:21:56 +0100 Subject: [PATCH 4/7] Use Path.GetRelativePath --- src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index e5896b86b..080592b0b 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -90,7 +90,7 @@ private void WriteImage(HtmlRenderer renderer, ImageBlock block) if (block.ImageUrl.StartsWith('/') || block.ImageUrl.StartsWith("_static")) imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; else - imageUrl = block.Build.UrlPathPrefix + block.CurrentFile.DirectoryName!.Replace(block.Build.ConfigurationPath.DirectoryName!, "") + "/" + block.ImageUrl; + imageUrl = block.Build.UrlPathPrefix + '/' + Path.GetRelativePath(block.Build.ConfigurationPath.DirectoryName!, block.CurrentFile.DirectoryName!) + "/" + block.ImageUrl; } var slice = Image.Create(new ImageViewModel { From 55b4b193ad3365c0d4699cd9ed705d948f550bf6 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:26:17 +0100 Subject: [PATCH 5/7] Also use Path.GetRelativePath in DiagnosticLinkeInlineParser --- .../Myst/InlineParsers/DiagnosticLinkInlineParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs index a350aa533..f5e33cb7e 100644 --- a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs +++ b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs @@ -258,7 +258,7 @@ private static void UpdateLinkUrl(LinkInline link, string url, ParserContext con private static string GetRootRelativePath(ParserContext context, IFileInfo file) { var docsetDirectory = context.Configuration.SourceFile.Directory; - return file.FullName.Replace(docsetDirectory!.FullName, string.Empty); + return "/" + Path.GetRelativePath(docsetDirectory!.FullName, file.FullName); } private static bool IsCrossLink([NotNullWhen(true)] Uri? uri) => From 8ee6af5d11b04e23da723d5210bc6d43cb20c0c4 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:28:05 +0100 Subject: [PATCH 6/7] Format --- src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 080592b0b..5e9e57929 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -90,7 +90,7 @@ private void WriteImage(HtmlRenderer renderer, ImageBlock block) if (block.ImageUrl.StartsWith('/') || block.ImageUrl.StartsWith("_static")) imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; else - imageUrl = block.Build.UrlPathPrefix + '/' + Path.GetRelativePath(block.Build.ConfigurationPath.DirectoryName!, block.CurrentFile.DirectoryName!) + "/" + block.ImageUrl; + imageUrl = block.Build.UrlPathPrefix + '/' + Path.GetRelativePath(block.Build.ConfigurationPath.DirectoryName!, block.CurrentFile.DirectoryName!) + "/" + block.ImageUrl; } var slice = Image.Create(new ImageViewModel { From 6df6676533f94b20ead494354c975d7c822ec912 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 18 Feb 2025 15:38:36 +0100 Subject: [PATCH 7/7] Add explaining comment --- .../Myst/Directives/DirectiveHtmlRenderer.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs index 5e9e57929..f459c9aa5 100644 --- a/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs +++ b/src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs @@ -90,7 +90,17 @@ private void WriteImage(HtmlRenderer renderer, ImageBlock block) if (block.ImageUrl.StartsWith('/') || block.ImageUrl.StartsWith("_static")) imageUrl = $"{block.Build.UrlPathPrefix}/{block.ImageUrl.TrimStart('/')}"; else + { + // `block.Build.ConfigurationPath.DirectoryName` is the directory of the docset.yml file + // which is the root of the documentation source + // e.g. `/User/username/Projects/docs-builder/docs` + // `block.CurrentFile.DirectoryName` is the directory of the current markdown file where the image is referenced + // e.g. `/User/username/Projects/docs-builder/docs/page/with/image` + // `Path.GetRelativePath` will return the relative path to the docset.yml directory + // e.g. `page/with/image` + // Hence the full imageUrl will be something like /path-prefix/page/with/image/image.png imageUrl = block.Build.UrlPathPrefix + '/' + Path.GetRelativePath(block.Build.ConfigurationPath.DirectoryName!, block.CurrentFile.DirectoryName!) + "/" + block.ImageUrl; + } } var slice = Image.Create(new ImageViewModel {