66using System . IO . Abstractions ;
77using System . Text . RegularExpressions ;
88using Elastic . Markdown . IO ;
9+ using Elastic . Markdown . Slices ;
910using Microsoft . Extensions . Logging ;
1011
1112namespace Documentation . Mover ;
@@ -24,7 +25,6 @@ public record LinkModification(string OldLink, string NewLink, string SourceFile
2425
2526 public async Task < int > Execute ( string ? source , string ? target , bool isDryRun , Cancel ctx = default )
2627 {
27- _linkModifications . Clear ( ) ;
2828 if ( isDryRun )
2929 _logger . LogInformation ( "Running in dry-run mode" ) ;
3030
@@ -37,9 +37,7 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
3737 var sourcePath = Path . GetFullPath ( source ! ) ;
3838 var targetPath = Path . GetFullPath ( target ! ) ;
3939
40- var ( _, sourceMarkdownFile ) = documentationSet . MarkdownFiles . Single ( i => i . Value . FilePath == sourcePath ) ;
41-
42- var sourceContent = await readFileSystem . File . ReadAllTextAsync ( sourceMarkdownFile . FilePath , ctx ) ;
40+ var sourceContent = await readFileSystem . File . ReadAllTextAsync ( sourcePath , ctx ) ;
4341
4442 var markdownLinkRegex = new Regex ( @"\[([^\]]*)\]\(((?:\.{0,2}\/)?[^:)]+\.md(?:#[^)]*)?)\)" , RegexOptions . Compiled ) ;
4543
@@ -52,25 +50,29 @@ public async Task<int> Execute(string? source, string? target, bool isDryRun, Ca
5250 if ( ! isAbsoluteStylePath )
5351 {
5452 var targetDirectory = Path . GetDirectoryName ( targetPath ) ! ;
55- var sourceDirectory = Path . GetDirectoryName ( sourceMarkdownFile . FilePath ) ! ;
53+ var sourceDirectory = Path . GetDirectoryName ( sourcePath ) ! ;
5654 var fullPath = Path . GetFullPath ( Path . Combine ( sourceDirectory , originalPath ) ) ;
5755 var relativePath = Path . GetRelativePath ( targetDirectory , fullPath ) ;
58- newPath = relativePath ;
56+
57+ if ( originalPath . StartsWith ( "./" ) && ! relativePath . StartsWith ( "./" ) )
58+ newPath = "./" + relativePath ;
59+ else
60+ newPath = relativePath ;
5961 }
6062 var newLink = $ "[{ match . Groups [ 1 ] . Value } ]({ newPath } )";
6163 var lineNumber = sourceContent . Substring ( 0 , match . Index ) . Count ( c => c == '\n ' ) + 1 ;
6264 var columnNumber = match . Index - sourceContent . LastIndexOf ( '\n ' , match . Index ) ;
6365 _linkModifications . Add ( new LinkModification (
6466 match . Value ,
6567 newLink ,
66- sourceMarkdownFile . SourceFile . FullName ,
68+ sourcePath ,
6769 lineNumber ,
6870 columnNumber
6971 ) ) ;
7072 return newLink ;
7173 } ) ;
7274
73- _changes . Add ( ( sourceMarkdownFile . FilePath , sourceContent , change ) ) ;
75+ _changes . Add ( ( sourcePath , sourceContent , change ) ) ;
7476
7577 foreach ( var ( _, markdownFile ) in documentationSet . MarkdownFiles )
7678 {
@@ -88,7 +90,7 @@ await ProcessMarkdownFile(
8890 ChangeFormatString ,
8991 oldLink ,
9092 newLink ,
91- sourceFile == sourceMarkdownFile . SourceFile . FullName && ! isDryRun ? targetPath : sourceFile ,
93+ sourceFile == sourcePath && ! isDryRun ? targetPath : sourceFile ,
9294 lineNumber ,
9395 columnNumber
9496 ) ) ;
@@ -226,7 +228,9 @@ MarkdownFile value
226228 else
227229 {
228230 var relativeTarget = Path . GetRelativePath ( Path . GetDirectoryName ( value . FilePath ) ! , target ) ;
229- newLink = $ "[{ match . Groups [ 1 ] . Value } ]({ relativeTarget } { anchor } )";
231+ newLink = originalPath . StartsWith ( "./" ) && ! relativeTarget . StartsWith ( "./" )
232+ ? $ "[{ match . Groups [ 1 ] . Value } ](./{ relativeTarget } { anchor } )"
233+ : $ "[{ match . Groups [ 1 ] . Value } ]({ relativeTarget } { anchor } )";
230234 }
231235
232236 var lineNumber = content . Substring ( 0 , match . Index ) . Count ( c => c == '\n ' ) + 1 ;
0 commit comments