Skip to content

Commit c6ef6ad

Browse files
committed
Have the command parse docset
1 parent 88ac874 commit c6ef6ad

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

docs/_docset.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ toc:
123123
- file: index.md
124124
- file: build.md
125125
- file: diff-validate.md
126+
- file: format.md
126127
- file: index-command.md
127128
- file: mv.md
128129
- file: serve.md

docs/cli/docset/format.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ docs-builder format [options...] [-h|--help] [--version]
1818

1919
## Description
2020

21-
The `format` command automatically detects and fixes formatting issues in your documentation files. Currently, it handles irregular whitespace characters that may impair Markdown rendering.
21+
The `format` command automatically detects and fixes formatting issues in your documentation files. The command only processes Markdown files (`.md`) that are included in your `_docset.yml` table of contents, ensuring that only intentional documentation files are modified.
22+
23+
Currently, it handles irregular whitespace characters that may impair Markdown rendering.
2224

2325
### Irregular Whitespace Detection
2426

src/authoring/Elastic.Documentation.Refactor/FormatService.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
using System.Text;
88
using Elastic.Documentation.Configuration;
99
using Elastic.Documentation.Diagnostics;
10+
using Elastic.Documentation.Links.CrossLinks;
1011
using Elastic.Documentation.Services;
12+
using Elastic.Markdown.IO;
1113
using Microsoft.Extensions.Logging;
1214

1315
namespace Elastic.Documentation.Refactor;
1416

1517
public class FormatService(
16-
ILoggerFactory logFactory
18+
ILoggerFactory logFactory,
19+
IConfigurationContext configurationContext
1720
) : IService
1821
{
1922
private readonly ILogger _logger = logFactory.CreateLogger<FormatService>();
@@ -58,37 +61,33 @@ Cancel ctx
5861
)
5962
{
6063
var isDryRun = dryRun ?? false;
61-
var rootPath = string.IsNullOrEmpty(path) ? fs.Directory.GetCurrentDirectory() : path;
62-
var rootDir = fs.DirectoryInfo.New(rootPath);
6364

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);
6968

70-
_logger.LogInformation("Formatting documentation in: {Path}", rootDir.FullName);
69+
_logger.LogInformation("Formatting documentation in: {Path}", set.SourceDirectory.FullName);
7170
if (isDryRun)
7271
_logger.LogInformation("Running in dry-run mode - no files will be modified");
7372

74-
var markdownFiles = rootDir.GetFiles("*.md", SearchOption.AllDirectories);
7573
var totalFilesProcessed = 0;
7674
var totalFilesModified = 0;
7775
var totalReplacements = 0;
7876

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>())
8079
{
8180
if (ctx.IsCancellationRequested)
8281
break;
8382

8483
totalFilesProcessed++;
85-
var (modified, replacements) = await ProcessFile(file, isDryRun, fs);
84+
var (modified, replacements) = await ProcessFile(docFile.SourceFile, isDryRun, fs);
8685

8786
if (modified)
8887
{
8988
totalFilesModified++;
9089
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);
9291
}
9392
}
9493

@@ -140,17 +139,4 @@ Cancel ctx
140139

141140
return (modified, replacements);
142141
}
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-
}
156142
}

src/tooling/docs-builder/Commands/FormatCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Documentation.Builder.Commands;
1414

1515
internal sealed class FormatCommand(
1616
ILoggerFactory logFactory,
17-
IDiagnosticsCollector collector
17+
IDiagnosticsCollector collector,
18+
IConfigurationContext configurationContext
1819
)
1920
{
2021
/// <summary>
@@ -32,7 +33,7 @@ public async Task<int> Format(
3233
{
3334
await using var serviceInvoker = new ServiceInvoker(collector);
3435

35-
var service = new FormatService(logFactory);
36+
var service = new FormatService(logFactory, configurationContext);
3637
var fs = new FileSystem();
3738

3839
serviceInvoker.AddCommand(service, (path, dryRun, fs),

0 commit comments

Comments
 (0)