Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs-builder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs-assembler", "src\docs-
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "authoring", "tests\authoring\authoring.fsproj", "{018F959E-824B-4664-B345-066784478D24}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs-mover", "src\docs-mover\docs-mover.csproj", "{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +97,10 @@ Global
{018F959E-824B-4664-B345-066784478D24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{018F959E-824B-4664-B345-066784478D24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{018F959E-824B-4664-B345-066784478D24}.Release|Any CPU.Build.0 = Release|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4D198E25-C211-41DC-9E84-B15E89BD7048} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
Expand All @@ -106,5 +112,6 @@ Global
{A2A34BBC-CB5E-4100-9529-A12B6ECB769C} = {245023D2-D3CA-47B9-831D-DAB91A2FFDC7}
{28350800-B44B-479B-86E2-1D39E321C0B4} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
{018F959E-824B-4664-B345-066784478D24} = {67B576EE-02FA-4F9B-94BC-3630BC09ECE5}
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions docs/docset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ toc:
- file: req.md
- folder: nested
- file: cross-links.md
- folder: mover
children:
- file: first-page.md
- file: second-page.md
3 changes: 3 additions & 0 deletions docs/testing/mover/first-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# First Page

[Link to second page](second-page.md)
5 changes: 5 additions & 0 deletions docs/testing/mover/second-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Second Page

[Link to first page](first-page.md)

[Absolut link to first page](/testing/mover/first-page.md)
32 changes: 31 additions & 1 deletion src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Collections.Generic;
using System.IO.Abstractions;
using Actions.Core.Services;
using ConsoleAppFramework;
using Documentation.Builder.Diagnostics;
using Documentation.Builder.Diagnostics.Console;
using Documentation.Builder.Http;
using Documentation.Mover;
using Elastic.Markdown;
using Elastic.Markdown.IO;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -102,4 +103,33 @@ public async Task<int> GenerateDefault(
Cancel ctx = default
) =>
await Generate(path, output, pathPrefix, force, strict, allowIndexing, ctx);


/// <summary>
/// Move a file from one location to another and update all links in the documentation
/// </summary>
/// <param name="source">The source file or folder path to move from</param>
/// <param name="target">The target file or folder path to move to</param>
/// <param name="path"> -p, Defaults to the`{pwd}` folder</param>
/// <param name="dryRun">Dry run the move operation</param>
/// <param name="ctx"></param>
[Command("mv")]
public async Task<int> Move(
[Argument] string? source = null,
[Argument] string? target = null,
bool? dryRun = null,
string? path = null,
Cancel ctx = default
)
{
var fileSystem = new FileSystem();
var context = new BuildContext(fileSystem, fileSystem, path, null)
{
Collector = new ConsoleDiagnosticsCollector(logger, null),
};
var set = new DocumentationSet(context);

var moveCommand = new Move(fileSystem, fileSystem, set, logger);
return await moveCommand.Execute(source, target, dryRun ?? false, ctx);
}
}
4 changes: 2 additions & 2 deletions src/docs-builder/docs-builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\docs-mover\docs-mover.csproj" />
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
</ItemGroup>

</Project>
</Project>
247 changes: 247 additions & 0 deletions src/docs-mover/Move.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Collections.ObjectModel;
using System.IO.Abstractions;
using System.Text.RegularExpressions;
using Elastic.Markdown.IO;
using Elastic.Markdown.Slices;
using Microsoft.Extensions.Logging;

namespace Documentation.Mover;

public class Move(IFileSystem readFileSystem, IFileSystem writeFileSystem, DocumentationSet documentationSet, ILoggerFactory loggerFactory)
{
private readonly ILogger _logger = loggerFactory.CreateLogger<Move>();
private readonly List<(string filePath, string originalContent, string newContent)> _changes = [];
private readonly List<LinkModification> _linkModifications = [];
private const string ChangeFormatString = "Change \e[31m{0}\e[0m to \e[32m{1}\e[0m at \e[34m{2}:{3}:{4}\e[0m";

public record LinkModification(string OldLink, string NewLink, string SourceFile, int LineNumber, int ColumnNumber);


public ReadOnlyCollection<LinkModification> LinkModifications => _linkModifications.AsReadOnly();

public async Task<int> Execute(string? source, string? target, bool isDryRun, Cancel ctx = default)
{
if (isDryRun)
_logger.LogInformation("Running in dry-run mode");

if (!ValidateInputs(source, target))
{
return 1;
}


var sourcePath = Path.GetFullPath(source!);
var targetPath = Path.GetFullPath(target!);

var sourceContent = await readFileSystem.File.ReadAllTextAsync(sourcePath, ctx);

var markdownLinkRegex = new Regex(@"\[([^\]]*)\]\(((?:\.{0,2}\/)?[^:)]+\.md(?:#[^)]*)?)\)", RegexOptions.Compiled);

var change = Regex.Replace(sourceContent, markdownLinkRegex.ToString(), match =>
{
var originalPath = match.Value.Substring(match.Value.IndexOf('(') + 1, match.Value.LastIndexOf(')') - match.Value.IndexOf('(') - 1);

var newPath = originalPath;
var isAbsoluteStylePath = originalPath.StartsWith('/');
if (!isAbsoluteStylePath)
{
var targetDirectory = Path.GetDirectoryName(targetPath)!;
var sourceDirectory = Path.GetDirectoryName(sourcePath)!;
var fullPath = Path.GetFullPath(Path.Combine(sourceDirectory, originalPath));
var relativePath = Path.GetRelativePath(targetDirectory, fullPath);

if (originalPath.StartsWith("./") && !relativePath.StartsWith("./"))
newPath = "./" + relativePath;
else
newPath = relativePath;
}
var newLink = $"[{match.Groups[1].Value}]({newPath})";
var lineNumber = sourceContent.Substring(0, match.Index).Count(c => c == '\n') + 1;
var columnNumber = match.Index - sourceContent.LastIndexOf('\n', match.Index);
_linkModifications.Add(new LinkModification(
match.Value,
newLink,
sourcePath,
lineNumber,
columnNumber
));
return newLink;
});

_changes.Add((sourcePath, sourceContent, change));

foreach (var (_, markdownFile) in documentationSet.MarkdownFiles)
{
await ProcessMarkdownFile(
sourcePath,
targetPath,
markdownFile,
ctx
);
}

foreach (var (oldLink, newLink, sourceFile, lineNumber, columnNumber) in LinkModifications)
{
_logger.LogInformation(string.Format(
ChangeFormatString,
oldLink,
newLink,
sourceFile == sourcePath && !isDryRun ? targetPath : sourceFile,
lineNumber,
columnNumber
));
}

if (isDryRun)
return 0;


try
{
foreach (var (filePath, _, newContent) in _changes)
await writeFileSystem.File.WriteAllTextAsync(filePath, newContent, ctx);
var targetDirectory = Path.GetDirectoryName(targetPath);
readFileSystem.Directory.CreateDirectory(targetDirectory!);
readFileSystem.File.Move(sourcePath, targetPath);
}
catch (Exception)
{
foreach (var (filePath, originalContent, _) in _changes)
await writeFileSystem.File.WriteAllTextAsync(filePath, originalContent, ctx);
writeFileSystem.File.Move(targetPath, sourcePath);
_logger.LogError("An error occurred while moving files. Reverting changes");
throw;
}
return 0;
}

private bool ValidateInputs(string? source, string? target)
{

if (string.IsNullOrEmpty(source))
{
_logger.LogError("Source path is required");
return false;
}

if (string.IsNullOrEmpty(target))
{
_logger.LogError("Target path is required");
return false;
}

if (!Path.GetExtension(source).Equals(".md", StringComparison.OrdinalIgnoreCase))
{
_logger.LogError("Source path must be a markdown file. Directory paths are not supported yet");
return false;
}

if (!Path.GetExtension(target).Equals(".md", StringComparison.OrdinalIgnoreCase))
{
_logger.LogError("Target path must be a markdown file. Directory paths are not supported yet");
return false;
}

if (!readFileSystem.File.Exists(source))
{
_logger.LogError($"Source file {source} does not exist");
return false;
}

if (readFileSystem.File.Exists(target))
{
_logger.LogError($"Target file {target} already exists");
return false;
}

return true;
}

private async Task ProcessMarkdownFile(
string source,
string target,
MarkdownFile value,
Cancel ctx)
{
var content = await readFileSystem.File.ReadAllTextAsync(value.FilePath, ctx);
var currentDir = Path.GetDirectoryName(value.FilePath)!;
var pathInfo = GetPathInfo(currentDir, source, target);
var linkPattern = BuildLinkPattern(pathInfo);

if (Regex.IsMatch(content, linkPattern))
{
var newContent = ReplaceLinks(content, linkPattern, pathInfo.absoluteStyleTarget, target, value);
_changes.Add((value.FilePath, content, newContent));
}
}

private (string relativeSource, string relativeSourceWithDotSlash, string absolutStyleSource, string absoluteStyleTarget) GetPathInfo(
string currentDir,
string sourcePath,
string targetPath
)
{
var relativeSource = Path.GetRelativePath(currentDir, sourcePath);
var relativeSourceWithDotSlash = Path.Combine(".", relativeSource);
var relativeToDocsFolder = Path.GetRelativePath(documentationSet.SourcePath.FullName, sourcePath);
var absolutStyleSource = $"/{relativeToDocsFolder}";
var relativeToDocsFolderTarget = Path.GetRelativePath(documentationSet.SourcePath.FullName, targetPath);
var absoluteStyleTarget = $"/{relativeToDocsFolderTarget}";
return (
relativeSource,
relativeSourceWithDotSlash,
absolutStyleSource,
absoluteStyleTarget
);
}

private static string BuildLinkPattern(
(string relativeSource, string relativeSourceWithDotSlash, string absolutStyleSource, string _) pathInfo) =>
$@"\[([^\]]*)\]\((?:{pathInfo.relativeSource}|{pathInfo.relativeSourceWithDotSlash}|{pathInfo.absolutStyleSource})(?:#[^\)]*?)?\)";

private string ReplaceLinks(
string content,
string linkPattern,
string absoluteStyleTarget,
string target,
MarkdownFile value
) =>
Regex.Replace(
content,
linkPattern,
match =>
{
var originalPath = match.Value.Substring(match.Value.IndexOf('(') + 1, match.Value.LastIndexOf(')') - match.Value.IndexOf('(') - 1);
var anchor = originalPath.Contains('#')
? originalPath[originalPath.IndexOf('#')..]
: "";

string newLink;
if (originalPath.StartsWith('/'))
{
newLink = $"[{match.Groups[1].Value}]({absoluteStyleTarget}{anchor})";
}
else
{
var relativeTarget = Path.GetRelativePath(Path.GetDirectoryName(value.FilePath)!, target);
newLink = originalPath.StartsWith("./") && !relativeTarget.StartsWith("./")
? $"[{match.Groups[1].Value}](./{relativeTarget}{anchor})"
: $"[{match.Groups[1].Value}]({relativeTarget}{anchor})";
}

var lineNumber = content.Substring(0, match.Index).Count(c => c == '\n') + 1;
var columnNumber = match.Index - content.LastIndexOf('\n', match.Index);
_linkModifications.Add(new LinkModification(
match.Value,
newLink,
value.SourceFile.FullName,
lineNumber,
columnNumber
));
return newLink;
});
}
19 changes: 19 additions & 0 deletions src/docs-mover/docs-mover.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Documentation.Mover</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Documentation.Mover</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Elastic.Markdown\Elastic.Markdown.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
</ItemGroup>

</Project>
Loading