@@ -11,6 +11,7 @@ namespace MarkdownLinter;
1111using System . Globalization ;
1212using System . IO ;
1313using System . Text . RegularExpressions ;
14+ using System . Threading ;
1415using System . Threading . Tasks ;
1516using Microsoft ;
1617using Microsoft . VisualStudio . Extensibility . Editor ;
@@ -19,27 +20,44 @@ namespace MarkdownLinter;
1920using Microsoft . VisualStudio . RpcContracts . DiagnosticManagement ;
2021using Microsoft . VisualStudio . Threading ;
2122
23+ #pragma warning disable VSEXTPREVIEW_SETTINGS // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
24+
2225/// <summary>
2326/// Helper class for running linter on a string or file.
2427/// </summary>
25- internal static class LinterUtilities
28+ internal class LinterUtilities
2629{
2730 private static readonly Regex LinterOutputRegex = new ( @"(?<File>[^:]+):(?<Line>\d*)(:(?<Column>\d*))? (?<Error>.*)/(?<Description>.*)" , RegexOptions . Compiled ) ;
2831
32+ private readonly Settings . MarkdownLinterCategoryObserver settingsObserver ;
33+
34+ public LinterUtilities ( Settings . MarkdownLinterCategoryObserver settingsObserver )
35+ {
36+ this . settingsObserver = settingsObserver ;
37+ }
38+
2939 /// <summary>
3040 /// Runs markdown linter on a file uri and returns diagnostic entries.
3141 /// </summary>
3242 /// <param name="fileUri">File uri to run markdown linter on.</param>
43+ /// <param name="cancellationToken">Cancellation token to monitor.</param>
3344 /// <returns>an enumeration of <see cref="DocumentDiagnostic"/> entries for warnings in the markdown file.</returns>
34- public static async Task < IEnumerable < DocumentDiagnostic > > RunLinterOnFileAsync ( Uri fileUri )
45+ public async Task < IEnumerable < DocumentDiagnostic > > RunLinterOnFileAsync ( Uri fileUri , CancellationToken cancellationToken )
3546 {
3647 using var linter = new Process ( ) ;
3748 var lineQueue = new AsyncQueue < string > ( ) ;
3849
50+ var snapshot = await this . settingsObserver . GetSnapshotAsync ( cancellationToken ) ;
51+ string disabledRules = snapshot . DisabledRules . ValueOrDefault ( string . Empty ) ;
52+
53+ string args = "/c \" npx markdownlint-cli" +
54+ ( disabledRules . Length > 0 ? $ " --disable { disabledRules } --" : string . Empty ) +
55+ $ " \" { fileUri . LocalPath } \" \" ";
56+
3957 linter . StartInfo = new ProcessStartInfo ( )
4058 {
41- FileName = "node .exe" ,
42- Arguments = $ " \" { Environment . ExpandEnvironmentVariables ( "%APPDATA% \\ npm \\ node_modules \\ markdownlint-cli \\ markdownlint.js" ) } \" \" { fileUri . LocalPath } \" " ,
59+ FileName = "cmd .exe" ,
60+ Arguments = args ,
4361 RedirectStandardError = true ,
4462 UseShellExecute = false ,
4563 CreateNoWindow = true ,
@@ -76,18 +94,25 @@ public static async Task<IEnumerable<DocumentDiagnostic>> RunLinterOnFileAsync(U
7694 /// Runs markdown linter on a given text document and returns diagnostic entries.
7795 /// </summary>
7896 /// <param name="textDocument">Document to run markdown linter on.</param>
97+ /// <param name="cancellationToken">Cancellation token to monitor.</param>
7998 /// <returns>an enumeration of <see cref="DocumentDiagnostic"/> entries for warnings in the markdown file.</returns>
80- public static async Task < IEnumerable < DocumentDiagnostic > > RunLinterOnDocumentAsync ( ITextDocumentSnapshot textDocument )
99+ public async Task < IEnumerable < DocumentDiagnostic > > RunLinterOnDocumentAsync ( ITextDocumentSnapshot textDocument , CancellationToken cancellationToken )
81100 {
82101 using var linter = new Process ( ) ;
83102 var lineQueue = new AsyncQueue < string > ( ) ;
84103
85104 var content = textDocument . Text . CopyToString ( ) ;
86105
106+ var snapshot = await this . settingsObserver . GetSnapshotAsync ( cancellationToken ) ;
107+ string disabledRules = snapshot . DisabledRules . ValueOrDefault ( string . Empty ) ;
108+
109+ string args = "/k \" npx markdownlint-cli --stdin" +
110+ ( disabledRules . Length > 0 ? $ " --disable { disabledRules } " : string . Empty ) + "\" " ;
111+
87112 linter . StartInfo = new ProcessStartInfo ( )
88113 {
89114 FileName = "cmd.exe" ,
90- Arguments = $ "/k \" { Environment . ExpandEnvironmentVariables ( "%APPDATA% \\ npm \\ markdownlint.cmd" ) } \" -s" ,
115+ Arguments = args ,
91116 RedirectStandardError = true ,
92117 RedirectStandardInput = true ,
93118 UseShellExecute = false ,
@@ -125,16 +150,6 @@ public static async Task<IEnumerable<DocumentDiagnostic>> RunLinterOnDocumentAsy
125150 return CreateDocumentDiagnosticsForOpenDocument ( textDocument , markdownDiagnostics ) ;
126151 }
127152
128- /// <summary>
129- /// Checks if the given path is a valid markdown file.
130- /// </summary>
131- /// <param name="localPath">Local file path to verify.</param>
132- /// <returns>true if file is a markdown file, false otherwise.</returns>
133- public static bool IsValidMarkdownFile ( string localPath )
134- {
135- return localPath is not null && Path . GetExtension ( localPath ) . Equals ( ".md" , StringComparison . OrdinalIgnoreCase ) ;
136- }
137-
138153 private static IEnumerable < DocumentDiagnostic > CreateDocumentDiagnosticsForOpenDocument ( ITextDocumentSnapshot document , IEnumerable < MarkdownDiagnosticInfo > diagnostics )
139154 {
140155 foreach ( var diagnostic in diagnostics )
0 commit comments