|
7 | 7 | using System.Text; |
8 | 8 | using Elastic.Documentation.Configuration; |
9 | 9 | using Elastic.Documentation.Diagnostics; |
| 10 | +using Elastic.Documentation.Links.CrossLinks; |
10 | 11 | using Elastic.Documentation.Services; |
| 12 | +using Elastic.Markdown.IO; |
11 | 13 | using Microsoft.Extensions.Logging; |
12 | 14 |
|
13 | 15 | namespace Elastic.Documentation.Refactor; |
14 | 16 |
|
15 | 17 | public class FormatService( |
16 | | - ILoggerFactory logFactory |
| 18 | + ILoggerFactory logFactory, |
| 19 | + IConfigurationContext configurationContext |
17 | 20 | ) : IService |
18 | 21 | { |
19 | 22 | private readonly ILogger _logger = logFactory.CreateLogger<FormatService>(); |
@@ -58,37 +61,33 @@ Cancel ctx |
58 | 61 | ) |
59 | 62 | { |
60 | 63 | var isDryRun = dryRun ?? false; |
61 | | - var rootPath = string.IsNullOrEmpty(path) ? fs.Directory.GetCurrentDirectory() : path; |
62 | | - var rootDir = fs.DirectoryInfo.New(rootPath); |
63 | 64 |
|
64 | | - if (!rootDir.Exists) |
65 | | - { |
66 | | - collector.EmitError(string.Empty, $"Directory not found: {rootPath}"); |
67 | | - return false; |
68 | | - } |
| 65 | + // Create BuildContext to load the documentation set |
| 66 | + var context = new BuildContext(collector, fs, fs, configurationContext, ExportOptions.MetadataOnly, path, null); |
| 67 | + var set = new DocumentationSet(context, logFactory, NoopCrossLinkResolver.Instance); |
69 | 68 |
|
70 | | - _logger.LogInformation("Formatting documentation in: {Path}", rootDir.FullName); |
| 69 | + _logger.LogInformation("Formatting documentation in: {Path}", set.SourceDirectory.FullName); |
71 | 70 | if (isDryRun) |
72 | 71 | _logger.LogInformation("Running in dry-run mode - no files will be modified"); |
73 | 72 |
|
74 | | - var markdownFiles = rootDir.GetFiles("*.md", SearchOption.AllDirectories); |
75 | 73 | var totalFilesProcessed = 0; |
76 | 74 | var totalFilesModified = 0; |
77 | 75 | var totalReplacements = 0; |
78 | 76 |
|
79 | | - foreach (var file in markdownFiles) |
| 77 | + // Only process markdown files that are part of the documentation set |
| 78 | + foreach (var docFile in set.Files.OfType<MarkdownFile>()) |
80 | 79 | { |
81 | 80 | if (ctx.IsCancellationRequested) |
82 | 81 | break; |
83 | 82 |
|
84 | 83 | totalFilesProcessed++; |
85 | | - var (modified, replacements) = await ProcessFile(file, isDryRun, fs); |
| 84 | + var (modified, replacements) = await ProcessFile(docFile.SourceFile, isDryRun, fs); |
86 | 85 |
|
87 | 86 | if (modified) |
88 | 87 | { |
89 | 88 | totalFilesModified++; |
90 | 89 | totalReplacements += replacements; |
91 | | - _logger.LogInformation("Fixed {Count} irregular whitespace(s) in: {File}", replacements, GetRelativePath(rootDir, file)); |
| 90 | + _logger.LogInformation("Fixed {Count} irregular whitespace(s) in: {File}", replacements, docFile.RelativePath); |
92 | 91 | } |
93 | 92 | } |
94 | 93 |
|
@@ -140,17 +139,4 @@ Cancel ctx |
140 | 139 |
|
141 | 140 | return (modified, replacements); |
142 | 141 | } |
143 | | - |
144 | | - private static string GetRelativePath(IDirectoryInfo root, IFileInfo file) |
145 | | - { |
146 | | - var rootPath = root.FullName.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
147 | | - var filePath = file.FullName; |
148 | | - |
149 | | - if (filePath.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase)) |
150 | | - { |
151 | | - return filePath.Substring(rootPath.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
152 | | - } |
153 | | - |
154 | | - return filePath; |
155 | | - } |
156 | 142 | } |
0 commit comments