Skip to content

Commit

Permalink
Fix matching http links
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelDomingues committed Oct 28, 2022
1 parent dbb8466 commit 28e8eab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/MarkdownLocalize.Markdown/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MarkdownParser

static RendererOptions options = new RendererOptions();
private static string REGEX_IMAGE = @"(!\[[^\]]*\]\()(.*?)\s*('(?:.*[^'])')?\s*(\))";
private static string REGEX_LINK = @"([^!]\[[^\]]*\]\()(.*?)\s*('(?:.*[^'])')?\s*(\))";
private static string REGEX_LINK = @"([^!]?\[[^\]]*\]\()(.*?)\s*('(?:.*[^'])')?\s*(\))";

public static void SetParserOptions(RendererOptions newOptions)
{
Expand Down Expand Up @@ -84,8 +84,15 @@ private static string MatchReplacer(string path, Match match)
{
string newString = match.Groups[1].Value;

string newPath = PathUtils.SimplifyRelativePath(Path.Combine(path, match.Groups[2].Value));
newString += newPath;
if (match.Groups[2].Value.StartsWith("http"))
{
newString += match.Groups[2].Value;
}
else
{
string newPath = PathUtils.SimplifyRelativePath(Path.Combine(path, match.Groups[2].Value));
newString += newPath;
}

newString += match.Groups[3].Value + match.Groups[4].Value;

Expand Down
15 changes: 15 additions & 0 deletions test/MarkdownLocalize.Tests/MarkdownInvertStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ public void UpdateImageRelativePathSkipAlt(string source, string expected)
string md = MarkdownParser.Translate(source, InvertString, null, out _);
Assert.Equal(expected, md);
}

[Theory]
[InlineData("[abc](https://example.com/image.png)", "[cba](https://example.com/image.png)")]
[InlineData("![abc](https://example.com/image.png)", "![cba](https://example.com/image.png)")]
[InlineData("[abc](./image.png)", "[cba](../../../original-doc-path/image.png)")]
public void UpdateLinkRelativePath(string source, string expected)
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
ImageRelativePath = "../../../original-doc-path/",
LinkRelativePath = "../../../original-doc-path/",
});
string md = MarkdownParser.Translate(source, InvertString, null, out _);
Assert.Equal(expected, md);
}
}

0 comments on commit 28e8eab

Please sign in to comment.