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+
45using System . IO . Abstractions ;
56using Elastic . Markdown . IO ;
67using Microsoft . Extensions . Logging ;
8+ using System . Text . RegularExpressions ;
79
810namespace 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 }
0 commit comments