Skip to content

Commit bc8657e

Browse files
committed
Also change links in source file
1 parent 85d9dac commit bc8657e

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

src/docs-builder/Cli/Commands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.IO.Abstractions;
66
using Actions.Core.Services;
77
using ConsoleAppFramework;
8-
using Documentation.Builder.Diagnostics;
98
using Documentation.Builder.Diagnostics.Console;
109
using Documentation.Builder.Http;
1110
using Elastic.Markdown;
@@ -129,7 +128,7 @@ public async Task<int> Move(
129128
};
130129
var set = new DocumentationSet(context);
131130

132-
var moveCommand = new MoveCommand(fileSystem, set, logger);
131+
var moveCommand = new Move(fileSystem, set, logger);
133132
return await moveCommand.Execute(source, target, dryRun ?? false, ctx);
134133
}
135134
}

src/docs-builder/Cli/MoveCommand.cs renamed to src/docs-builder/Cli/Move.cs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
4+
45
using System.IO.Abstractions;
56
using Elastic.Markdown.IO;
67
using Microsoft.Extensions.Logging;
8+
using System.Text.RegularExpressions;
79

810
namespace Documentation.Builder.Cli;
911

10-
internal class MoveCommand(IFileSystem fileSystem, DocumentationSet documentationSet, ILoggerFactory loggerFactory)
12+
internal class Move(IFileSystem fileSystem, DocumentationSet documentationSet, ILoggerFactory loggerFactory)
1113
{
12-
private readonly ILogger _logger = loggerFactory.CreateLogger<MoveCommand>();
14+
private readonly ILogger _logger = loggerFactory.CreateLogger<Move>();
1315
private readonly List<(string filePath, string originalContent,string newContent)> _changes = [];
16+
private const string ChangeFormatString = "Change \e[31m{0}\e[0m to \e[32m{1}\e[0m at \e[34m{2}:{3}:{4}\e[0m";
1417

1518
public async Task<int> Execute(string? source, string? target, bool isDryRun, Cancel ctx = default)
1619
{
@@ -25,6 +28,42 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
2528
var sourcePath = Path.GetFullPath(source!);
2629
var targetPath = Path.GetFullPath(target!);
2730

31+
var (_, sourceMarkdownFile) = documentationSet.MarkdownFiles.Single(i => i.Value.FilePath == sourcePath);
32+
33+
var soureContent = await fileSystem.File.ReadAllTextAsync(sourceMarkdownFile.FilePath, ctx);
34+
35+
var markdownLinkRegex = new Regex(@"\[([^\]]*)\]\(((?:\.{0,2}\/)?[^:)]+\.md(?:#[^)]*)?)\)", RegexOptions.Compiled);
36+
var matches = markdownLinkRegex.Matches(soureContent);
37+
38+
var change = Regex.Replace(soureContent, markdownLinkRegex.ToString(), match =>
39+
{
40+
var originalPath = match.Value.Substring(match.Value.IndexOf('(') + 1, match.Value.LastIndexOf(')') - match.Value.IndexOf('(') - 1);
41+
// var anchor = originalPath.Contains('#') ? originalPath[originalPath.IndexOf('#')..] : "";
42+
43+
string newPath;
44+
45+
var isAbsoluteStylePath = originalPath.StartsWith('/');
46+
if (isAbsoluteStylePath)
47+
{
48+
newPath = originalPath;
49+
} else {
50+
var targetDirectory = Path.GetDirectoryName(targetPath)!;
51+
var sourceDirectory = Path.GetDirectoryName(sourceMarkdownFile.FilePath)!;
52+
var fullPath = Path.GetFullPath(Path.Combine(sourceDirectory, originalPath));
53+
var relativePath = Path.GetRelativePath(targetDirectory, fullPath);
54+
newPath = relativePath;
55+
}
56+
var newLink = $"[{match.Groups[1].Value}]({newPath})";
57+
var lineNumber = soureContent.Substring(0, match.Index).Count(c => c == '\n') + 1;
58+
var columnNumber = match.Index - soureContent.LastIndexOf('\n', match.Index);
59+
_logger.LogInformation(string.Format(ChangeFormatString, match.Value, newLink,
60+
sourceMarkdownFile.SourceFile.FullName, lineNumber, columnNumber));
61+
62+
return newLink;
63+
});
64+
65+
_changes.Add((sourceMarkdownFile.FilePath, soureContent, change));
66+
2867
foreach (var (_, markdownFile) in documentationSet.MarkdownFiles)
2968
{
3069
await ProcessMarkdownFile(
@@ -53,9 +92,6 @@ await ProcessMarkdownFile(
5392
throw;
5493
}
5594

56-
57-
58-
5995
return 0;
6096
}
6197

@@ -168,12 +204,6 @@ MarkdownFile value
168204
// Absolute style link
169205
newLink = $"[{match.Groups[1].Value}]({absoluteStyleTarget}{anchor})";
170206
}
171-
// else if (originalPath.StartsWith("./"))
172-
// {
173-
// // Relative link with ./ prefix
174-
// var relativeTarget = Path.Combine(".", Path.GetRelativePath(Path.GetDirectoryName(value.FilePath)!, target)).Replace('\\', '/');
175-
// newLink = $"[{match.Groups[1].Value}]({relativeTarget}{anchor})";
176-
// }
177207
else
178208
{
179209
// Relative link
@@ -182,7 +212,9 @@ MarkdownFile value
182212
}
183213

184214
var lineNumber = content.Substring(0, match.Index).Count(c => c == '\n') + 1;
185-
_logger.LogInformation($"Change \e[31m{match.Value}\e[0m to \e[32m{newLink}\e[0m at {value.SourceFile.FullName}:{lineNumber}");
215+
var columnNumber = match.Index - content.LastIndexOf('\n', match.Index);
216+
_logger.LogInformation(string.Format(ChangeFormatString, match.Value, newLink, value.SourceFile.FullName,
217+
lineNumber, columnNumber));
186218
return newLink;
187219
});
188220
}

src/docs-builder/docs-builder.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
<ItemGroup>
3333
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
3434
</ItemGroup>
35-
36-
</Project>
35+
</Project>

0 commit comments

Comments
 (0)