Skip to content

Commit e0e6cf7

Browse files
authored
Fix regex pattern to handle clickable image badges with relative URLs
Fixes #656
1 parent 796b8f7 commit e0e6cf7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/NuGetizer.Tasks/CreatePackage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ void GeneratePackage(Stream output = null)
238238
uri.Host.EndsWith("github.com"))
239239
{
240240
// expr to match markdown links with optional title. use named groups to capture the link text, url and optional title.
241-
linkExpr ??= new Regex(@"\[(?<text>[^\]]+)\]\((?<url>[^\s)]+)(?:\s+""(?<title>[^""]*)"")?\)", RegexOptions.None);
241+
// Handle image links inside clickable badges: [![alt](img)](url) by explicitly matching the image pattern
242+
linkExpr ??= new Regex(@"\[(?<text>!\[[^\]]*\]\([^\)]*\)|[^\]]+)\]\((?<url>[^\s)]+)(?:\s+""(?<title>[^""]*)"")?\)", RegexOptions.None);
242243
var repoUrl = manifest.Metadata.Repository.Url.TrimEnd('/');
243244

244245
// Extract owner and repo from URL for raw.githubusercontent.com format

src/NuGetizer.Tests/CreatePackageTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,43 @@ public void when_readme_has_image_link_then_uses_raw_url()
443443
Assert.DoesNotContain("/blob/", readme);
444444
}
445445

446+
[Fact]
447+
public void when_readme_has_clickable_image_badge_with_relative_url_then_replaces_url()
448+
{
449+
var content = Path.GetTempFileName();
450+
File.WriteAllText(content, "[![EULA](https://img.shields.io/badge/EULA-OSMF-blue)](osmfeula.txt)");
451+
task.Contents = new[]
452+
{
453+
new TaskItem(content, new Metadata
454+
{
455+
{ MetadataName.PackageId, task.Manifest.GetMetadata("Id") },
456+
{ MetadataName.PackFolder, PackFolderKind.None },
457+
{ MetadataName.PackagePath, "readme.md" }
458+
}),
459+
};
460+
461+
task.Manifest.SetMetadata("Readme", "readme.md");
462+
task.Manifest.SetMetadata("RepositoryType", "git");
463+
task.Manifest.SetMetadata("RepositoryUrl", "https://github.com/devlooped/nugetizer");
464+
task.Manifest.SetMetadata("RepositorySha", "abc123def");
465+
466+
createPackage = true;
467+
ExecuteTask(out var manifest);
468+
469+
Assert.NotNull(manifest);
470+
471+
var file = manifest.Files.FirstOrDefault(f => Path.GetFileName(f.Target) == manifest.Metadata.Readme);
472+
Assert.NotNull(file);
473+
Assert.True(File.Exists(file.Source));
474+
475+
var readme = File.ReadAllText(file.Source);
476+
477+
// Should replace the relative URL in the clickable image badge
478+
Assert.Contains("https://raw.githubusercontent.com/devlooped/nugetizer/abc123def/osmfeula.txt", readme);
479+
// Should preserve the absolute image URL
480+
Assert.Contains("https://img.shields.io/badge/EULA-OSMF-blue", readme);
481+
}
482+
446483
[Fact]
447484
public void when_creating_package_with_simple_dependency_then_contains_dependency_group()
448485
{

src/NuGetizer.Tests/Scenarios/given_multitargeting_libraries/uilibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>net8.0;net8.0-windows;net8.0-maccatalyst</TargetFrameworks>
66
<PackOnBuild>true</PackOnBuild>
77
<IsPackable>true</IsPackable>
8+
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net8.0-maccatalyst'">14.0</SupportedOSPlatformVersion>
89
</PropertyGroup>
910

1011
<ItemGroup>

0 commit comments

Comments
 (0)