diff --git a/docfx.sln b/docfx.sln index 860f2299103..39de1228752 100644 --- a/docfx.sln +++ b/docfx.sln @@ -100,8 +100,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Docfx.MarkdigEngine.Extensi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Docfx.MarkdigEngine.Tests", "test\Docfx.MarkdigEngine.Tests\Docfx.MarkdigEngine.Tests.csproj", "{DD9736B2-8AA2-44AE-B0B3-57510FF0D634}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Docfx.MarkdigEngine.Validators", "src\Docfx.MarkdigEngine.Validators\Docfx.MarkdigEngine.Validators.csproj", "{BD35FD90-4F00-4A19-9530-2279A6A15EE8}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Markdown", "Markdown", "{377A4748-C426-4D5E-9A25-6D1809A7B85C}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{C533106C-5699-450C-A18C-249364CBF58B}" @@ -126,6 +124,18 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x64.ActiveCfg = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x64.Build.0 = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x86.ActiveCfg = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x86.Build.0 = Debug|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|Any CPU.Build.0 = Release|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x64.ActiveCfg = Release|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x64.Build.0 = Release|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x86.ActiveCfg = Release|Any CPU + {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x86.Build.0 = Release|Any CPU {7E877810-DC13-4A6C-8DFC-184C00A20B7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7E877810-DC13-4A6C-8DFC-184C00A20B7C}.Debug|Any CPU.Build.0 = Debug|Any CPU {7E877810-DC13-4A6C-8DFC-184C00A20B7C}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -402,18 +412,6 @@ Global {958DF90F-0528-4C50-9AC2-E86C60971B7D}.Release|x64.Build.0 = Release|Any CPU {958DF90F-0528-4C50-9AC2-E86C60971B7D}.Release|x86.ActiveCfg = Release|Any CPU {958DF90F-0528-4C50-9AC2-E86C60971B7D}.Release|x86.Build.0 = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x64.ActiveCfg = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x64.Build.0 = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x86.ActiveCfg = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Debug|x86.Build.0 = Debug|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|Any CPU.Build.0 = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x64.ActiveCfg = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x64.Build.0 = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x86.ActiveCfg = Release|Any CPU - {EF53214F-BA98-4026-BEED-CF771865C312}.Release|x86.Build.0 = Release|Any CPU {E9795C9A-1F98-4716-A0FC-843C6B000BE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E9795C9A-1F98-4716-A0FC-843C6B000BE5}.Debug|Any CPU.Build.0 = Debug|Any CPU {E9795C9A-1F98-4716-A0FC-843C6B000BE5}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -594,18 +592,6 @@ Global {DD9736B2-8AA2-44AE-B0B3-57510FF0D634}.Release|x64.Build.0 = Release|Any CPU {DD9736B2-8AA2-44AE-B0B3-57510FF0D634}.Release|x86.ActiveCfg = Release|Any CPU {DD9736B2-8AA2-44AE-B0B3-57510FF0D634}.Release|x86.Build.0 = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|x64.ActiveCfg = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|x64.Build.0 = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|x86.ActiveCfg = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Debug|x86.Build.0 = Debug|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|Any CPU.Build.0 = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|x64.ActiveCfg = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|x64.Build.0 = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|x86.ActiveCfg = Release|Any CPU - {BD35FD90-4F00-4A19-9530-2279A6A15EE8}.Release|x86.Build.0 = Release|Any CPU {4C9CF944-2489-4AC2-9B18-497D12B53769}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4C9CF944-2489-4AC2-9B18-497D12B53769}.Debug|Any CPU.Build.0 = Debug|Any CPU {4C9CF944-2489-4AC2-9B18-497D12B53769}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -673,6 +659,7 @@ Global GlobalSection(NestedProjects) = preSolution {F922E543-9AD4-4031-8BD1-8A7AA9771D1E} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {C3023E7B-AA8B-469E-8333-5737093A9D60} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} + {EF53214F-BA98-4026-BEED-CF771865C312} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {7E877810-DC13-4A6C-8DFC-184C00A20B7C} = {C533106C-5699-450C-A18C-249364CBF58B} {B6B96738-C338-4904-B932-4C05DB7D4DDE} = {C533106C-5699-450C-A18C-249364CBF58B} {59CF2F0F-4D4D-4096-8D22-595856CCCF72} = {C533106C-5699-450C-A18C-249364CBF58B} @@ -696,7 +683,6 @@ Global {A95C5BAE-1D0D-44FD-A984-7CBF055A62E2} = {926A0726-B806-4215-82EF-AF8E22D0FACF} {B3EEB5FE-CF39-4CC7-9650-5709E71D2BA9} = {926A0726-B806-4215-82EF-AF8E22D0FACF} {958DF90F-0528-4C50-9AC2-E86C60971B7D} = {926A0726-B806-4215-82EF-AF8E22D0FACF} - {EF53214F-BA98-4026-BEED-CF771865C312} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {E9795C9A-1F98-4716-A0FC-843C6B000BE5} = {926A0726-B806-4215-82EF-AF8E22D0FACF} {FF8D61D0-01C9-44B9-98F9-37FCBF20CD72} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {1D2D316B-4C70-4410-817A-DEDB5B0072A5} = {926A0726-B806-4215-82EF-AF8E22D0FACF} @@ -712,7 +698,6 @@ Global {EA44B820-4C3A-476C-A514-C89CAA6A0F46} = {377A4748-C426-4D5E-9A25-6D1809A7B85C} {FD0365CB-3F03-43B0-9FB2-FB50047BF3E5} = {377A4748-C426-4D5E-9A25-6D1809A7B85C} {DD9736B2-8AA2-44AE-B0B3-57510FF0D634} = {926A0726-B806-4215-82EF-AF8E22D0FACF} - {BD35FD90-4F00-4A19-9530-2279A6A15EE8} = {377A4748-C426-4D5E-9A25-6D1809A7B85C} {377A4748-C426-4D5E-9A25-6D1809A7B85C} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {C533106C-5699-450C-A18C-249364CBF58B} = {6A65A769-DAC3-4E99-96F9-991F4CE8D164} {4C9CF944-2489-4AC2-9B18-497D12B53769} = {926A0726-B806-4215-82EF-AF8E22D0FACF} diff --git a/src/Docfx.Build.Engine/DocumentBuilder.cs b/src/Docfx.Build.Engine/DocumentBuilder.cs index a0cab69c6a5..f10f3eebe5f 100644 --- a/src/Docfx.Build.Engine/DocumentBuilder.cs +++ b/src/Docfx.Build.Engine/DocumentBuilder.cs @@ -205,7 +205,6 @@ IMarkdownServiceProvider GetMarkdownServiceProvider() { return new MarkdigServiceProvider { - Container = _container.GetExport(), ConfigureMarkdig = parameters[0].ConfigureMarkdig }; } @@ -277,7 +276,6 @@ private MarkdigMarkdownService CreateMarkdigMarkdownService(DocumentBuildParamet Extensions = parameters.MarkdownEngineParameters, Tokens = TemplateProcessorUtility.LoadTokens(resourceProvider)?.ToImmutableDictionary(), }, - new CompositionContainer(CompositionContainer.DefaultContainer), parameters.ConfigureMarkdig); } diff --git a/src/Docfx.MarkdigEngine.Extensions/Docfx.MarkdigEngine.Extensions.csproj b/src/Docfx.MarkdigEngine.Extensions/Docfx.MarkdigEngine.Extensions.csproj index 720040f9224..933e2646ecd 100644 --- a/src/Docfx.MarkdigEngine.Extensions/Docfx.MarkdigEngine.Extensions.csproj +++ b/src/Docfx.MarkdigEngine.Extensions/Docfx.MarkdigEngine.Extensions.csproj @@ -4,8 +4,4 @@ - - - - diff --git a/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectRewriterFactory.cs b/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectRewriterFactory.cs deleted file mode 100644 index 0de542b3411..00000000000 --- a/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectRewriterFactory.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Markdig.Syntax; -using Docfx.MarkdigEngine.Validators; - -namespace Docfx.MarkdigEngine.Extensions; - -public static class MarkdownObjectRewriterFactory -{ - public static IMarkdownObjectRewriter FromValidators( - IEnumerable validators, - Action preProcess = null, - Action postProcess = null) - { - if (validators == null) - { - throw new ArgumentNullException(nameof(validators)); - } - - return new MarkdownObjectValidatorAdapter(validators, preProcess, postProcess); - } - - public static IMarkdownObjectRewriter FromValidator( - IMarkdownObjectValidator validator, - Action preProcess = null, - Action postProcess = null) - { - if (validator == null) - { - throw new ArgumentNullException(nameof(validator)); - } - - return new MarkdownObjectValidatorAdapter(validator, preProcess, postProcess); - } -} diff --git a/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectValidatorAdapter.cs b/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectValidatorAdapter.cs deleted file mode 100644 index 7d20b831ca7..00000000000 --- a/src/Docfx.MarkdigEngine.Extensions/Rewriter/MarkdownObjectValidatorAdapter.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Immutable; - -using Markdig.Syntax; -using Docfx.MarkdigEngine.Validators; - -namespace Docfx.MarkdigEngine.Extensions; - -internal class MarkdownObjectValidatorAdapter : IMarkdownObjectRewriter -{ - private Action _preProcess; - private Action _postProcess; - - public ImmutableArray Validators { get; } - - public MarkdownObjectValidatorAdapter( - IEnumerable validators, - Action preProcess, - Action postProcess) - { - Validators = validators.ToImmutableArray(); - _preProcess = preProcess; - _postProcess = postProcess; - } - - public MarkdownObjectValidatorAdapter( - IMarkdownObjectValidator validator, - Action preProcess, - Action postProcess) - { - Validators = new[] { validator }.ToImmutableArray(); - _preProcess = preProcess; - _postProcess = postProcess; - } - - public IMarkdownObject Rewrite(IMarkdownObject markdownObject) - { - foreach (var validator in Validators) - { - validator.Validate(markdownObject); - } - - return markdownObject; - } - - public void PreProcess(IMarkdownObject markdownObject) - { - _preProcess?.Invoke(markdownObject); - foreach (var validator in Validators) - { - validator.PreValidate(markdownObject); - } - } - - public void PostProcess(IMarkdownObject markdownObject) - { - foreach (var validator in Validators) - { - validator.PostValidate(markdownObject); - } - _postProcess?.Invoke(markdownObject); - } -} diff --git a/src/Docfx.MarkdigEngine.Validators/Docfx.MarkdigEngine.Validators.csproj b/src/Docfx.MarkdigEngine.Validators/Docfx.MarkdigEngine.Validators.csproj deleted file mode 100644 index 3d8c1dbe945..00000000000 --- a/src/Docfx.MarkdigEngine.Validators/Docfx.MarkdigEngine.Validators.csproj +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidator.cs b/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidator.cs deleted file mode 100644 index cf970e1f6da..00000000000 --- a/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidator.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Markdig.Syntax; - -namespace Docfx.MarkdigEngine.Validators; - -public interface IMarkdownObjectValidator -{ - void PreValidate(IMarkdownObject markdownObject); - - void Validate(IMarkdownObject markdownObject); - - void PostValidate(IMarkdownObject markdownObject); -} diff --git a/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidatorProvider.cs b/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidatorProvider.cs deleted file mode 100644 index bcf7df2b870..00000000000 --- a/src/Docfx.MarkdigEngine.Validators/IMarkdownObjectValidatorProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Immutable; - -namespace Docfx.MarkdigEngine.Validators; - -public interface IMarkdownObjectValidatorProvider -{ - ImmutableArray GetValidators(); -} diff --git a/src/Docfx.MarkdigEngine.Validators/MarkdownLambdaObjectValidator.cs b/src/Docfx.MarkdigEngine.Validators/MarkdownLambdaObjectValidator.cs deleted file mode 100644 index 7bb1cf256d5..00000000000 --- a/src/Docfx.MarkdigEngine.Validators/MarkdownLambdaObjectValidator.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Markdig.Syntax; - -namespace Docfx.MarkdigEngine.Validators; - -internal class MarkdownLambdaObjectValidator : IMarkdownObjectValidator where TObject : class, IMarkdownObject -{ - private Action _preAction; - private Action _validator; - private Action _postAction; - - public MarkdownLambdaObjectValidator( - Action validator, - Action preAction, - Action postAction - ) - { - _preAction = preAction; - _validator = validator; - _postAction = postAction; - } - - public void PreValidate(IMarkdownObject markdownObject) - { - _preAction?.Invoke(markdownObject); - } - - public void Validate(IMarkdownObject markdownObject) - { - if (markdownObject is TObject obj) - { - _validator?.Invoke(obj); - } - } - - public void PostValidate(IMarkdownObject markdownObject) - { - _postAction?.Invoke(markdownObject); - } -} diff --git a/src/Docfx.MarkdigEngine.Validators/MarkdownObjectValidatorFactory.cs b/src/Docfx.MarkdigEngine.Validators/MarkdownObjectValidatorFactory.cs deleted file mode 100644 index 2c8867d1593..00000000000 --- a/src/Docfx.MarkdigEngine.Validators/MarkdownObjectValidatorFactory.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Markdig.Syntax; - -namespace Docfx.MarkdigEngine.Validators; - -public static class MarkdownObjectValidatorFactory -{ - public static IMarkdownObjectValidator FromLambda( - Action validator, - Action preAction = null, - Action postAction = null) - where TObject : class, IMarkdownObject - { - return new MarkdownLambdaObjectValidator(validator, preAction, postAction); - } -} diff --git a/src/Docfx.MarkdigEngine/Docfx.MarkdigEngine.csproj b/src/Docfx.MarkdigEngine/Docfx.MarkdigEngine.csproj index 81a77a4620a..ed2a988828c 100644 --- a/src/Docfx.MarkdigEngine/Docfx.MarkdigEngine.csproj +++ b/src/Docfx.MarkdigEngine/Docfx.MarkdigEngine.csproj @@ -1,7 +1,6 @@  - diff --git a/src/Docfx.MarkdigEngine/MarkdigMarkdownService.cs b/src/Docfx.MarkdigEngine/MarkdigMarkdownService.cs index a543ff07cca..5464797b52d 100644 --- a/src/Docfx.MarkdigEngine/MarkdigMarkdownService.cs +++ b/src/Docfx.MarkdigEngine/MarkdigMarkdownService.cs @@ -22,12 +22,11 @@ public class MarkdigMarkdownService : IMarkdownService public MarkdigMarkdownService( MarkdownServiceParameters parameters, - ICompositionContainer container = null, Func configureMarkdig = null) { _parameters = parameters; _configureMarkdig = configureMarkdig; - _mvb = MarkdownValidatorBuilder.Create(parameters, container); + _mvb = MarkdownValidatorBuilder.Create(parameters); _context = new MarkdownContext( key => _parameters.Tokens.TryGetValue(key, out var value) ? value : null, (code, message, origin, line) => Logger.LogInfo(message, null, InclusionContext.File.ToString(), line?.ToString(), code), diff --git a/src/Docfx.MarkdigEngine/MarkdigServiceProvider.cs b/src/Docfx.MarkdigEngine/MarkdigServiceProvider.cs index 5178c214a3c..e1db981ca98 100644 --- a/src/Docfx.MarkdigEngine/MarkdigServiceProvider.cs +++ b/src/Docfx.MarkdigEngine/MarkdigServiceProvider.cs @@ -8,11 +8,10 @@ namespace Docfx.MarkdigEngine; public class MarkdigServiceProvider : IMarkdownServiceProvider { - public ICompositionContainer Container { get; init; } public Func ConfigureMarkdig { get; init; } public IMarkdownService CreateMarkdownService(MarkdownServiceParameters parameters) { - return new MarkdigMarkdownService(parameters, Container, ConfigureMarkdig); + return new MarkdigMarkdownService(parameters, ConfigureMarkdig); } } diff --git a/src/Docfx.MarkdigEngine/Validation/MarkdownValidatorBuilder.cs b/src/Docfx.MarkdigEngine/Validation/MarkdownValidatorBuilder.cs index 6803b9cd304..a3dc73ed9b4 100644 --- a/src/Docfx.MarkdigEngine/Validation/MarkdownValidatorBuilder.cs +++ b/src/Docfx.MarkdigEngine/Validation/MarkdownValidatorBuilder.cs @@ -3,67 +3,35 @@ using System.Collections.Immutable; -using Markdig.Syntax; using Docfx.Common; -using Docfx.MarkdigEngine.Validators; using Docfx.Plugins; namespace Docfx.MarkdigEngine.Extensions; public class MarkdownValidatorBuilder { - private readonly List> _validators = - new(); - private readonly List> _tagValidators = - new(); - private readonly Dictionary _globalValidators = - new(); - private readonly List _settings = - new(); - private List _validatorProviders = - new(); + private readonly List> _validators = new(); + private readonly List> _tagValidators = new(); + private readonly Dictionary _globalValidators = new(); + private readonly List _settings = new(); public const string DefaultValidatorName = "default"; public const string MarkdownValidatePhaseName = "Markdown style"; - private ICompositionContainer Container { get; } - - public MarkdownValidatorBuilder(ICompositionContainer container) - { - Container = container; - } - - public static MarkdownValidatorBuilder Create( - MarkdownServiceParameters parameters, - ICompositionContainer container) + public static MarkdownValidatorBuilder Create(MarkdownServiceParameters parameters) { - var builder = new MarkdownValidatorBuilder(container); + var builder = new MarkdownValidatorBuilder(); if (parameters != null) { LoadValidatorConfig(parameters.BasePath, parameters.TemplateDir, builder); } - if (container != null) - { - builder.LoadEnabledRulesProvider(); - } - return builder; } public IMarkdownObjectRewriter CreateRewriter(MarkdownContext context) { - var tagValidator = new TagValidator(GetEnabledTagRules().ToImmutableList(), context); - var validators = from vp in _validatorProviders - from p in vp.GetValidators() - select p; - - return MarkdownObjectRewriterFactory.FromValidators( - validators.Concat( - new[] - { - MarkdownObjectValidatorFactory.FromLambda(tagValidator.Validate) - })); + return new TagValidator(GetEnabledTagRules().ToImmutableList(), context); } public void AddValidators(MarkdownValidationRule[] rules) @@ -204,36 +172,6 @@ private static void LoadValidatorDefinition(string mdStyleDefPath, MarkdownValid } } - public void LoadEnabledRulesProvider() - { - HashSet enabledContractName = new(); - foreach (var item in _validators) - { - if (IsDisabledBySetting(item) ?? item.Rule.Disable) - { - enabledContractName.Remove(item.Rule.ContractName); - } - else - { - enabledContractName.Add(item.Rule.ContractName); - } - } - foreach (var pair in _globalValidators) - { - if (pair.Value.Disable) - { - enabledContractName.Remove(pair.Value.ContractName); - } - else - { - enabledContractName.Add(pair.Value.ContractName); - } - } - _validatorProviders = (from name in enabledContractName - from vp in Container?.GetExports(name) - select vp).ToList(); - } - private IEnumerable GetEnabledTagRules() { foreach (var item in _tagValidators) diff --git a/src/Docfx.MarkdigEngine/Validation/TagValidator.cs b/src/Docfx.MarkdigEngine/Validation/TagValidator.cs index 75ddf7a2312..bd830c902ea 100644 --- a/src/Docfx.MarkdigEngine/Validation/TagValidator.cs +++ b/src/Docfx.MarkdigEngine/Validation/TagValidator.cs @@ -8,7 +8,7 @@ namespace Docfx.MarkdigEngine.Extensions; -internal class TagValidator +internal class TagValidator : IMarkdownObjectRewriter { public ImmutableList Validators { get; } @@ -64,4 +64,14 @@ private void ValidateCore(Tag tag, MarkdownTagValidationRule validator) return; } } + + void IMarkdownObjectRewriter.PreProcess(IMarkdownObject markdownObject) { } + + void IMarkdownObjectRewriter.PostProcess(IMarkdownObject markdownObject) { } + + IMarkdownObject IMarkdownObjectRewriter.Rewrite(IMarkdownObject markdownObject) + { + Validate(markdownObject); + return markdownObject; + } } diff --git a/src/Docfx.Plugins/MarkdownTagValidationRule.cs b/src/Docfx.Plugins/MarkdownTagValidationRule.cs index e309f965443..200270f5e4e 100644 --- a/src/Docfx.Plugins/MarkdownTagValidationRule.cs +++ b/src/Docfx.Plugins/MarkdownTagValidationRule.cs @@ -14,31 +14,31 @@ public class MarkdownTagValidationRule /// [JsonProperty("tagNames", Required = Required.Always)] public List TagNames { get; set; } + /// /// The relation for tags. /// [JsonProperty("relation")] public TagRelation Relation { get; set; } + /// /// Define tag's behavior. /// [JsonProperty("behavior", Required = Required.Always)] public TagValidationBehavior Behavior { get; set; } + /// /// The message formatter for warning and error. '{0}' is name of tag, '{1}' is the full tag. /// [JsonProperty("messageFormatter", Required = Required.Always)] public string MessageFormatter { get; set; } - /// - /// The contract name for custom validator . - /// - [JsonProperty("customValidatorContractName")] - public string CustomValidatorContractName { get; set; } + /// /// Only validate opening tag. /// [JsonProperty("openingTagOnly")] public bool OpeningTagOnly { get; set; } + /// /// Whether to disable this rule by default. /// diff --git a/test/Docfx.MarkdigEngine.Tests/HtmlMarkdownObjectValidatorProvider.cs b/test/Docfx.MarkdigEngine.Tests/HtmlMarkdownObjectValidatorProvider.cs deleted file mode 100644 index 09d824ed781..00000000000 --- a/test/Docfx.MarkdigEngine.Tests/HtmlMarkdownObjectValidatorProvider.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Immutable; -using System.Composition; - -using Markdig.Syntax; -using Docfx.Common; -using Docfx.MarkdigEngine.Validators; - -namespace Docfx.MarkdigEngine.Tests; - -[Export(ContractName, typeof(IMarkdownObjectValidatorProvider))] -public class HtmlMarkdownObjectValidatorProvider : IMarkdownObjectValidatorProvider -{ - public const string ContractName = "Html"; - - public const string WarningMessage = "Html Tag!"; - - ImmutableArray IMarkdownObjectValidatorProvider.GetValidators() - { - return ImmutableArray.Create( - MarkdownObjectValidatorFactory.FromLambda( - block => Logger.LogWarning(WarningMessage))); - } -} diff --git a/test/Docfx.MarkdigEngine.Tests/ValidationTest.cs b/test/Docfx.MarkdigEngine.Tests/ValidationTest.cs index 78a04a05698..c58e9dbb5fa 100644 --- a/test/Docfx.MarkdigEngine.Tests/ValidationTest.cs +++ b/test/Docfx.MarkdigEngine.Tests/ValidationTest.cs @@ -1,13 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Composition.Hosting; - using Markdig; -using Markdig.Syntax; using Docfx.Common; using Docfx.MarkdigEngine.Extensions; -using Docfx.MarkdigEngine.Validators; using Docfx.Plugins; using Xunit; @@ -42,12 +38,7 @@ public void TestHtmlBlockTagValidation() "; - var builder = MarkdownValidatorBuilder.Create( - null, - new CompositionContainer( - new ContainerConfiguration() - .WithAssembly(typeof(ValidationTest).Assembly) - .CreateContainer())); + var builder = MarkdownValidatorBuilder.Create(null); builder.AddTagValidators(new[] { @@ -79,15 +70,6 @@ public void TestHtmlBlockTagValidation() } }); - builder.AddValidators(new[] - { - new MarkdownValidationRule - { - ContractName = HtmlMarkdownObjectValidatorProvider.ContractName, - } - }); - - builder.LoadEnabledRulesProvider(); var listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(MarkdownValidatePhaseName); using (new LoggerPhaseScope(MarkdownValidatePhaseName)) { @@ -106,10 +88,8 @@ public void TestHtmlBlockTagValidation() ".Replace("\r\n", "\n"), html); } - Assert.Equal(8, listener.Items.Count); Assert.Equal(new[] { - "Html Tag!", "Invalid tag(div)!", "Invalid tag(EM)!", "Warning tag(h1)!", @@ -137,7 +117,7 @@ public void TestHtmlBlockTagNotInRelationValidation() "; - var builder = MarkdownValidatorBuilder.Create(null, null); + var builder = MarkdownValidatorBuilder.Create(null); builder.AddTagValidators(new[] { new MarkdownTagValidationRule @@ -185,12 +165,7 @@ public void TestHtmlInlineTagValidation() end."; - var builder = MarkdownValidatorBuilder.Create( - null, - new CompositionContainer( - new ContainerConfiguration() - .WithAssembly(typeof(ValidationTest).Assembly) - .CreateContainer())); + var builder = MarkdownValidatorBuilder.Create(null); builder.AddTagValidators(new[] { @@ -244,130 +219,6 @@ public void TestHtmlInlineTagValidation() }, from item in listener.Items select item.Message); } - [Fact] - [Trait("Related", "Validation")] - public void TestTokenValidator() - { - const string content = "# Hello World"; - const string expected = "

Hello World

\n"; - const string expectedMessage = "a space is expected after '#'"; - string message = null; - - var rewriter = MarkdownObjectRewriterFactory.FromValidator( - MarkdownObjectValidatorFactory.FromLambda( - block => - { - if (!block.Lines.ToString().StartsWith("# ")) - { - message = expectedMessage; - } - }) - ); - - var html = Markup(content, rewriter, null); - Assert.Equal(expected.Replace("\r\n", "\n"), html); - Assert.Equal(expectedMessage, message); - } - - [Fact] - [Trait("Related", "Validation")] - public void TestValidatorWithContext() - { - const string content = @"# Title-1 -# Title-2"; - const string expected = @"

Title-1

-

Title-2

-"; - const string expectedMessage = "expected one title in one document."; - string message = null; - - var context = new Dictionary(); - var validator = MarkdownObjectValidatorFactory.FromLambda( - block => - { - if (block.Level == 1) - { - if (context.TryGetValue("count", out object countObj) && countObj is int count) - { - context["count"] = count + 1; - } - } - }, - tree => - { - context.Add("count", 0); - }, - tree => - { - if (context.TryGetValue("count", out object countObj) && countObj is int count) - { - if (count != 1) - { - message = expectedMessage; - } - } - } - ); - - var rewriter = MarkdownObjectRewriterFactory.FromValidator(validator); - var html = Markup(content, rewriter, null); - Assert.Equal(expected.Replace("\r\n", "\n"), html); - Assert.Equal(expectedMessage, message); - } - - [Fact] - [Trait("Related", "Validation")] - public void TestMarkdownDocumentValidator() - { - const string content = "## Hello World"; - const string expected = "

Hello World

\n"; - const string expectedMessage = "H1 should be in the first line"; - string message = null; - - var rewriter = MarkdownObjectRewriterFactory.FromValidator( - MarkdownObjectValidatorFactory.FromLambda( - root => - { - if (root.First() is HeadingBlock heading) - { - if (heading.Level != 1) - { - message = expectedMessage; - } - } - }) - ); - - var html = Markup(content, rewriter, null); - Assert.Equal(expected.Replace("\r\n", "\n"), html); - Assert.Equal(expectedMessage, message); - } - - [Fact] - [Trait("Related", "Validation")] - public void TestGetSchemaName() - { - const string expectedSchemaName = "YamlMime:ModuleUnit"; - const string yamlFilename = "moduleunit.yml"; - const string yamlContent = @"### YamlMime:ModuleUnit -uid: learn.azure.introduction"; - File.WriteAllText(yamlFilename, yamlContent); - InclusionContext.PushFile(yamlFilename); - InclusionContext.PushInclusion("introduction-included.md"); - - string schemaName = string.Empty; - - var rewriter = MarkdownObjectRewriterFactory.FromValidator( - MarkdownObjectValidatorFactory.FromLambda( - root => - { - schemaName = root.GetData("SchemaName")?.ToString(); - }) - ); - var html = Markup("# Hello World", rewriter, null); - Assert.Equal(expectedSchemaName, schemaName); - } - private string Markup(string content, IMarkdownObjectRewriter rewriter, TestLoggerListener listener = null) { var pipelineBuilder = new MarkdownPipelineBuilder();