Skip to content

Commit 0f4a85f

Browse files
committed
include url path prefix in images
1 parent 746eefd commit 0f4a85f

File tree

14 files changed

+60
-52
lines changed

14 files changed

+60
-52
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ namespace Elastic.Markdown;
44

55
public record BuildContext
66
{
7+
private readonly string? _urlPathPrefix;
78
public bool Force { get; init; }
8-
public string? UrlPathPrefix { get; init; }
9+
10+
public string? UrlPathPrefix
11+
{
12+
get => string.IsNullOrWhiteSpace(_urlPathPrefix) ? "" : $"/{_urlPathPrefix.Trim('/')}";
13+
init => _urlPathPrefix = value;
14+
}
915

1016
public required IFileSystem ReadFileSystem { get; init; }
1117
public required IFileSystem WriteFileSystem { get; init; }

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public DocumentationSet(IDirectoryInfo? sourcePath, IDirectoryInfo? outputPath,
3636
{
3737
".svg" => new ImageFile(file, SourcePath, "image/svg+xml"),
3838
".png" => new ImageFile(file, SourcePath),
39-
".md" => new MarkdownFile(file, SourcePath, MarkdownParser, context.UrlPathPrefix),
39+
".md" => new MarkdownFile(file, SourcePath, MarkdownParser, context),
4040
_ => new StaticFile(file, SourcePath)
4141
})
4242
.ToList();

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ public class MarkdownFile : DocumentationFile
1515
private readonly SlugHelper _slugHelper = new();
1616
private string? _tocTitle;
1717

18-
public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParser parser, string? urlPath)
18+
public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParser parser, BuildContext context)
1919
: base(sourceFile, rootPath)
2020
{
2121
ParentFolders = RelativePath.Split(Path.DirectorySeparatorChar).SkipLast(1).ToArray();
2222
FileName = sourceFile.Name;
23-
UrlPath = string.IsNullOrWhiteSpace(urlPath) ? "" : $"/{urlPath.Trim('/')}";
23+
UrlPathPrefix = context.UrlPathPrefix;
2424
MarkdownParser = parser;
2525
}
2626

27-
public string UrlPath { get; }
27+
public string? UrlPathPrefix { get; }
2828
private MarkdownParser MarkdownParser { get; }
2929
private FrontMatterParser FrontMatterParser { get; } = new();
3030
public YamlFrontMatter? YamlFrontMatter { get; private set; }
@@ -38,7 +38,7 @@ public string? TocTitle
3838
public List<PageTocItem> TableOfContents { get; } = new();
3939
public IReadOnlyList<string> ParentFolders { get; }
4040
public string FileName { get; }
41-
public string Url => $"{UrlPath}/{RelativePath.Replace(".md", ".html")}";
41+
public string Url => $"{UrlPathPrefix}/{RelativePath.Replace(".md", ".html")}";
4242

4343
public async Task ParseAsync(Cancel ctx) => await ParseFullAsync(ctx);
4444

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ namespace Elastic.Markdown.Myst.Directives;
1212
/// </summary>
1313
/// <seealso cref="ContainerBlock" />
1414
/// <seealso cref="IFencedBlock" />
15-
public abstract class DirectiveBlock : ContainerBlock, IFencedBlock
15+
/// <remarks>
16+
/// Initializes a new instance of the <see cref="DirectiveBlock"/> class.
17+
/// </remarks>
18+
/// <param name="parser">The parser used to create this block.</param>
19+
/// <param name="properties"></param>
20+
/// <param name="context"></param>
21+
public abstract class DirectiveBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
22+
: ContainerBlock(parser), IFencedBlock
1623
{
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="DirectiveBlock"/> class.
19-
/// </summary>
20-
/// <param name="parser">The parser used to create this block.</param>
21-
/// <param name="properties"></param>
22-
public DirectiveBlock(DirectiveBlockParser parser, Dictionary<string, string> properties) : base(parser) =>
23-
Properties = properties;
2424

25-
public IReadOnlyDictionary<string, string> Properties { get; }
25+
public IReadOnlyDictionary<string, string> Properties { get; } = properties;
2626

27-
/// <inheritdoc />
28-
public char FencedChar { get; set; }
27+
/// <inheritdoc />
28+
public char FencedChar { get; set; }
2929

3030
/// <inheritdoc />
3131
public int OpeningFencedCharCount { get; set; }

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
3939
_admonitionData = new Dictionary<string, string>();
4040
var info = processor.Line.AsSpan();
4141

42+
if (processor.Context is not ParserContext context)
43+
throw new Exception("Expected parser context to be of type ParserContext");
44+
45+
4246
if (info.EndsWith("{toctree}"))
4347
return new TocTreeBlock(this, _admonitionData);
4448

@@ -67,30 +71,22 @@ protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
6771
return new DropdownBlock(this, _admonitionData);
6872

6973
if (info.IndexOf("{image}") > 0)
70-
return new ImageBlock(this, _admonitionData);
74+
return new ImageBlock(this, _admonitionData, context);
7175

7276
if (info.IndexOf("{figure}") > 0)
73-
return new FigureBlock(this, _admonitionData);
77+
return new FigureBlock(this, _admonitionData, context);
7478

7579
if (info.IndexOf("{figure-md}") > 0)
76-
return new FigureBlock(this, _admonitionData);
80+
return new FigureBlock(this, _admonitionData, context);
7781

7882
if (info.IndexOf("{mermaid}") > 0)
7983
return new MermaidBlock(this, _admonitionData);
8084

8185
if (info.IndexOf("{include}") > 0)
82-
{
83-
if (processor.Context is MystMarkdownParserContext context)
84-
return new IncludeBlock(this, _admonitionData, context);
85-
//todo emit error
86-
}
86+
return new IncludeBlock(this, _admonitionData, context);
8787

8888
if (info.IndexOf("{literalinclude}") > 0)
89-
{
90-
if (processor.Context is MystMarkdownParserContext context)
91-
return new LiteralIncludeBlock(this, _admonitionData, context);
92-
//todo emit error
93-
}
89+
return new LiteralIncludeBlock(this, _admonitionData, context);
9490

9591
foreach (var admonition in _admonitions)
9692
{

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ protected override void Write(HtmlRenderer renderer, DirectiveBlock directiveBlo
9191

9292
private void WriteImage(HtmlRenderer renderer, ImageBlock block)
9393
{
94+
var imageUrl = block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")
95+
? $"{block.Build.UrlPathPrefix}/{block.ImageUrl}"
96+
: block.ImageUrl;
9497
var slice = Image.Create(new ImageViewModel
9598
{
9699
Classes = block.Classes,
@@ -101,13 +104,16 @@ private void WriteImage(HtmlRenderer renderer, ImageBlock block)
101104
Scale = block.Scale,
102105
Target = block.Target,
103106
Width = block.Width,
104-
ImageUrl = block.ImageUrl,
107+
ImageUrl = imageUrl,
105108
});
106109
RenderRazorSlice(slice, renderer, block);
107110
}
108111

109112
private void WriteFigure(HtmlRenderer renderer, ImageBlock block)
110113
{
114+
var imageUrl = block.ImageUrl.StartsWith("/_static") || block.ImageUrl.StartsWith("_static")
115+
? $"{block.Build.UrlPathPrefix}/{block.ImageUrl}"
116+
: block.ImageUrl;
111117
var slice = Slices.Directives.Figure.Create(new ImageViewModel
112118
{
113119
Classes = block.Classes,
@@ -118,7 +124,7 @@ private void WriteFigure(HtmlRenderer renderer, ImageBlock block)
118124
Scale = block.Scale,
119125
Target = block.Target,
120126
Width = block.Width,
121-
ImageUrl = block.ImageUrl,
127+
ImageUrl = imageUrl,
122128
});
123129
RenderRazorSlice(slice, renderer, block);
124130
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
namespace Elastic.Markdown.Myst.Directives;
22

3-
public class ImageBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
3+
public class ImageBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
44
: DirectiveBlock(parser, properties)
55
{
6+
public BuildContext Build { get; } = context.Build;
67

78
/// <summary>
89
/// Alternate text: a short description of the image, displayed by applications that cannot display images,
@@ -50,12 +51,11 @@ public class ImageBlock(DirectiveBlockParser parser, Dictionary<string, string>
5051
/// </summary>
5152
public string? CrossReferenceName { get; private set; }
5253

53-
public string? ImageUrl { get; private set; }
54-
54+
public string ImageUrl { get; private set; } = default!;
5555

5656
public override void FinalizeAndValidate()
5757
{
58-
ImageUrl = Arguments; //todo validate
58+
ImageUrl = Arguments ?? string.Empty; //todo validate
5959
Classes = Properties.GetValueOrDefault("class");
6060
CrossReferenceName = Properties.GetValueOrDefault("name");
6161
Alt = Properties.GetValueOrDefault("alt");
@@ -68,5 +68,5 @@ public override void FinalizeAndValidate()
6868
}
6969

7070

71-
public class FigureBlock(DirectiveBlockParser parser, Dictionary<string, string> properties)
72-
: ImageBlock(parser, properties);
71+
public class FigureBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
72+
: ImageBlock(parser, properties, context);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Elastic.Markdown.Myst.Directives;
44

5-
public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, MystMarkdownParserContext context)
5+
public class IncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
66
: DirectiveBlock(parser, properties)
77
{
88
public BuildContext Build { get; } = context.Build;
@@ -43,5 +43,5 @@ public override void FinalizeAndValidate()
4343
}
4444

4545

46-
public class LiteralIncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, MystMarkdownParserContext context)
46+
public class LiteralIncludeBlock(DirectiveBlockParser parser, Dictionary<string, string> properties, ParserContext context)
4747
: IncludeBlock(parser, properties, context);

src/Elastic.Markdown/Myst/MarkdownParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
namespace Elastic.Markdown.Myst;
1111

1212

13-
public class MystMarkdownParserContext : MarkdownParserContext
13+
public class ParserContext : MarkdownParserContext
1414
{
15-
public MystMarkdownParserContext(MarkdownParser markdownParser,
15+
public ParserContext(MarkdownParser markdownParser,
1616
IFileInfo path,
1717
YamlFrontMatter? frontMatter,
1818
BuildContext context)
@@ -58,13 +58,13 @@ public class MarkdownParser(IDirectoryInfo sourcePath, BuildContext context)
5858
// TODO only scan for yaml front matter and toc information
5959
public Task<MarkdownDocument> QuickParseAsync(IFileInfo path, Cancel ctx)
6060
{
61-
var context = new MystMarkdownParserContext(this, path, null, Context);
61+
var context = new ParserContext(this, path, null, Context);
6262
return ParseAsync(path, context, ctx);
6363
}
6464

6565
public Task<MarkdownDocument> ParseAsync(IFileInfo path, YamlFrontMatter? matter, Cancel ctx)
6666
{
67-
var context = new MystMarkdownParserContext(this, path, matter, Context);
67+
var context = new ParserContext(this, path, matter, Context);
6868
return ParseAsync(path, context, ctx);
6969
}
7070

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, Cancel ctx = defau
4848
Tree = DocumentationSet.Tree,
4949
CurrentDocument = markdown,
5050
Navigation = navigationHtml,
51-
UrlPath = markdown.UrlPath
51+
UrlPathPrefix = markdown.UrlPathPrefix
5252
});
5353
return await slice.RenderAsync(cancellationToken: ctx);
5454
}

0 commit comments

Comments
 (0)