-
Notifications
You must be signed in to change notification settings - Fork 32
Add docs-builder format command #2084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
22ac68c
Add format command
theletterf 88ac874
Add docs
theletterf c6ef6ad
Have the command parse docset
theletterf 8042702
Various refactors
theletterf f1cce89
Remove test spaces
theletterf 22db85b
Update src/Elastic.Markdown/Myst/Linters/SpaceNormalizer.cs
theletterf c749b89
Add --check and --write commands
theletterf 1a14a33
Further refactors
theletterf 0faca18
Merge branch 'main' into add-docsbuilder-format-command
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # format | ||
|
|
||
| Format documentation files by fixing common issues like irregular space | ||
|
|
||
| ## Usage | ||
|
|
||
| ``` | ||
| docs-builder format [options...] [-h|--help] [--version] | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| `-p|--path` `<string>` | ||
| : Path to the documentation folder, defaults to pwd. (optional) | ||
|
|
||
| `--dry-run` `<bool?>` | ||
| : Preview changes without modifying files (optional) | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| Currently, it handles irregular space characters that may impair Markdown rendering. | ||
|
|
||
| ### Irregular Space Detection | ||
|
|
||
| The format command detects and replaces 24 types of irregular space characters with regular spaces, including: | ||
|
|
||
| - No-Break Space (U+00A0) | ||
| - En Space (U+2002) | ||
| - Em Space (U+2003) | ||
| - Zero Width Space (U+200B) | ||
| - Line Separator (U+2028) | ||
| - Paragraph Separator (U+2029) | ||
| - And 18 other irregular space variants | ||
|
|
||
| These characters can cause unexpected rendering issues in Markdown and are often introduced accidentally through copy-paste operations from other applications. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Format current directory | ||
|
|
||
| ```bash | ||
| docs-builder format | ||
| ``` | ||
|
|
||
| ### Preview changes without modifying files | ||
|
|
||
| ```bash | ||
| docs-builder format --dry-run | ||
| ``` | ||
|
|
||
| ### Format specific documentation folder | ||
|
|
||
| ```bash | ||
| docs-builder format --path /path/to/docs | ||
| ``` | ||
|
|
||
| ## Output | ||
|
|
||
| The command provides detailed feedback about the formatting process: | ||
|
|
||
| ``` | ||
| Formatting documentation in: /path/to/docs | ||
| Fixed 2 irregular space(s) in: guide/setup.md | ||
| Fixed 1 irregular space(s) in: api/endpoints.md | ||
|
|
||
| Formatting complete: | ||
| Files processed: 155 | ||
| Files modified: 2 | ||
| Total replacements: 3 | ||
| ``` | ||
|
|
||
| When using `--dry-run`, files are not modified and the command reminds you to run without the flag to apply changes. | ||
|
|
||
| ## Future Enhancements | ||
|
|
||
| The format command is designed to be extended with additional formatting capabilities in the future, such as: | ||
|
|
||
| - Line ending normalization | ||
| - Trailing whitespace removal | ||
| - Consistent heading spacing | ||
| - And other formatting fixes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // 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.Buffers; | ||
| using Elastic.Markdown.Diagnostics; | ||
| using Markdig; | ||
| using Markdig.Helpers; | ||
| using Markdig.Parsers; | ||
| using Markdig.Parsers.Inlines; | ||
| using Markdig.Renderers; | ||
| using Markdig.Renderers.Html; | ||
| using Markdig.Renderers.Html.Inlines; | ||
| using Markdig.Syntax.Inlines; | ||
|
|
||
| namespace Elastic.Markdown.Myst.Linters; | ||
|
|
||
| public static class SpaceNormalizerBuilderExtensions | ||
| { | ||
| public static MarkdownPipelineBuilder UseSpaceNormalizer(this MarkdownPipelineBuilder pipeline) | ||
| { | ||
| pipeline.Extensions.AddIfNotAlready<SpaceNormalizerBuilderExtension>(); | ||
| return pipeline; | ||
| } | ||
| } | ||
|
|
||
| public class SpaceNormalizerBuilderExtension : IMarkdownExtension | ||
| { | ||
| public void Setup(MarkdownPipelineBuilder pipeline) => | ||
| pipeline.InlineParsers.InsertBefore<EmphasisInlineParser>(new SpaceNormalizerParser()); | ||
|
|
||
| public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) => | ||
| renderer.ObjectRenderers.InsertAfter<EmphasisInlineRenderer>(new SpaceNormalizerRenderer()); | ||
| } | ||
|
|
||
| public class SpaceNormalizerParser : InlineParser | ||
| { | ||
| // Collection of irregular space characters that may impair Markdown rendering | ||
| private static readonly char[] IrregularSpaceChars = | ||
| [ | ||
| '\u000B', // Line Tabulation (\v) - <VT> | ||
| '\u000C', // Form Feed (\f) - <FF> | ||
| '\u00A0', // No-Break Space - <NBSP> | ||
| '\u0085', // Next Line | ||
| '\u1680', // Ogham Space Mark | ||
| '\u180E', // Mongolian Vowel Separator - <MVS> | ||
| '\ufeff', // Zero Width No-Break Space - <BOM> | ||
| '\u2000', // En Quad | ||
| '\u2001', // Em Quad | ||
| '\u2002', // En Space - <ENSP> | ||
| '\u2003', // Em Space - <EMSP> | ||
| '\u2004', // Tree-Per-Em | ||
| '\u2005', // Four-Per-Em | ||
| '\u2006', // Six-Per-Em | ||
| '\u2007', // Figure Space | ||
| '\u2008', // Punctuation Space - <PUNCSP> | ||
| '\u2009', // Thin Space | ||
| '\u200A', // Hair Space | ||
| '\u200B', // Zero Width Space - <ZWSP> | ||
| '\u2028', // Line Separator | ||
| '\u2029', // Paragraph Separator | ||
| '\u202F', // Narrow No-Break Space | ||
| '\u205F', // Medium Mathematical Space | ||
| '\u3000' // Ideographic Space | ||
| ]; | ||
| private static readonly SearchValues<char> SpaceSearchValues = SearchValues.Create(IrregularSpaceChars); | ||
|
|
||
| // Track which files have already had the hint emitted to avoid duplicates | ||
| private static readonly HashSet<string> FilesWithHintEmitted = []; | ||
|
|
||
| public SpaceNormalizerParser() => OpeningCharacters = IrregularSpaceChars; | ||
|
|
||
| public override bool Match(InlineProcessor processor, ref StringSlice slice) | ||
| { | ||
| var span = slice.AsSpan().Slice(0, 1); | ||
| if (span.IndexOfAny(SpaceSearchValues) == -1) | ||
| return false; | ||
|
|
||
| processor.Inline = IrregularSpace.Instance; | ||
|
|
||
| // Emit a single hint per file on first detection | ||
| var context = processor.GetContext(); | ||
| var filePath = context.MarkdownSourcePath.FullName; | ||
|
|
||
| lock (FilesWithHintEmitted) | ||
| { | ||
| if (!FilesWithHintEmitted.Contains(filePath)) | ||
| { | ||
| _ = FilesWithHintEmitted.Add(filePath); | ||
| processor.EmitHint(processor.Inline, 1, "Irregular space detected. Run 'docs-builder format' to automatically fix all instances."); | ||
| } | ||
| } | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| slice.SkipChar(); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| public class IrregularSpace : LeafInline | ||
| { | ||
| public static readonly IrregularSpace Instance = new(); | ||
| }; | ||
|
|
||
| public class SpaceNormalizerRenderer : HtmlObjectRenderer<IrregularSpace> | ||
| { | ||
| protected override void Write(HtmlRenderer renderer, IrregularSpace obj) => | ||
| renderer.Write(' '); | ||
| } | ||
127 changes: 0 additions & 127 deletions
127
src/Elastic.Markdown/Myst/Linters/WhiteSpaceNormalizer.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.