Skip to content

Commit 1371ac9

Browse files
Copilotreakaleek
andauthored
Fix inline image resize parameter to require space before = when title present (#2017)
* Initial plan * Update regex to make space before = optional in image sizing Co-authored-by: reakaleek <[email protected]> * Update tests and documentation for flexible image sizing syntax Co-authored-by: reakaleek <[email protected]> * Require space before = when title is present, optional when no title Co-authored-by: reakaleek <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: reakaleek <[email protected]> Co-authored-by: Jan Calanog <[email protected]>
1 parent 79ece48 commit 1371ac9

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

docs/syntax/images.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ If `H` is omitted `W` is used as the height as well.
9696
![alt](img.png "=50%")
9797
```
9898

99+
When specifying just the size without a title, no space is required before the `=` sign. When combining a title with sizing, a space is required before the `=`:
100+
101+
```markdown
102+
![alt](img.png "=50%") <!-- Just size, no space needed -->
103+
![alt](img.png "My Title =50%") <!-- With title, space required -->
104+
```
105+
99106

100107

101108
### SVG

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
4040

4141
internal sealed partial class LinkRegexExtensions
4242
{
43-
[GeneratedRegex(@"\s\=(?<width>\d+%?)(?:x(?<height>\d+%?))?$", RegexOptions.IgnoreCase, "en-US")]
43+
[GeneratedRegex(@"(?:^|\s)\=(?<width>\d+%?)(?:x(?<height>\d+%?))?$", RegexOptions.IgnoreCase, "en-US")]
4444
public static partial Regex MatchTitleStylingInstructions();
4545
}
4646

@@ -92,7 +92,7 @@ private static void ParseStylingInstructions(LinkInline link, ParserContext cont
9292
attributes.AddProperty("width", width);
9393
attributes.AddProperty("height", height);
9494

95-
title = title[..matches.Index];
95+
title = title[..matches.Index].TrimEnd();
9696
}
9797
link.Title = title.ReplaceSubstitutions(context);
9898
}

tests/Elastic.Markdown.Tests/Inline/InlineImageTest.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,93 @@ public void GeneratesAttributesInHtml() =>
3939
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" /></p>"""
4040
);
4141
}
42+
43+
// Test image sizing with space before =
44+
public class InlineImageWithSizingSpaceBeforeTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
45+
"""
46+
![Elasticsearch](/_static/img/observability.png " =50%")
47+
"""
48+
)
49+
{
50+
[Fact]
51+
public void ParsesBlock() => Block.Should().NotBeNull();
52+
53+
[Fact]
54+
public void GeneratesAttributesInHtml() =>
55+
// language=html
56+
Html.ShouldContainHtml(
57+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" /></p>"""
58+
);
59+
}
60+
61+
// Test image sizing without space before =
62+
public class InlineImageWithSizingNoSpaceBeforeTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
63+
"""
64+
![Elasticsearch](/_static/img/observability.png "=50%")
65+
"""
66+
)
67+
{
68+
[Fact]
69+
public void ParsesBlock() => Block.Should().NotBeNull();
70+
71+
[Fact]
72+
public void GeneratesAttributesInHtml() =>
73+
// language=html
74+
Html.ShouldContainHtml(
75+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="50%" height="50%" /></p>"""
76+
);
77+
}
78+
79+
// Test image sizing with pixels
80+
public class InlineImageWithPixelSizingTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
81+
"""
82+
![Elasticsearch](/_static/img/observability.png "=250x330")
83+
"""
84+
)
85+
{
86+
[Fact]
87+
public void ParsesBlock() => Block.Should().NotBeNull();
88+
89+
[Fact]
90+
public void GeneratesAttributesInHtml() =>
91+
// language=html
92+
Html.ShouldContainHtml(
93+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="330px" /></p>"""
94+
);
95+
}
96+
97+
// Test image sizing with title and sizing
98+
public class InlineImageWithTitleAndSizingTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
99+
"""
100+
![Elasticsearch](/_static/img/observability.png "My Title =50%")
101+
"""
102+
)
103+
{
104+
[Fact]
105+
public void ParsesBlock() => Block.Should().NotBeNull();
106+
107+
[Fact]
108+
public void GeneratesAttributesInHtml() =>
109+
// language=html
110+
Html.ShouldContainHtml(
111+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" title="My Title" width="50%" height="50%" /></p>"""
112+
);
113+
}
114+
115+
// Test image sizing with width only
116+
public class InlineImageWithWidthOnlyTest(ITestOutputHelper output) : InlineTest<LinkInline>(output,
117+
"""
118+
![Elasticsearch](/_static/img/observability.png "=250")
119+
"""
120+
)
121+
{
122+
[Fact]
123+
public void ParsesBlock() => Block.Should().NotBeNull();
124+
125+
[Fact]
126+
public void GeneratesAttributesInHtml() =>
127+
// language=html
128+
Html.ShouldContainHtml(
129+
"""<p><img src="/docs/_static/img/observability.png" alt="Elasticsearch" width="250px" height="250px" /></p>"""
130+
);
131+
}

0 commit comments

Comments
 (0)