Exemplo n.º 1
0
 private static IMarkdownTokenRewriter InitMarkdownStyle(CompositionHost host)
 {
     try
     {
         if (File.Exists(MarkdownSytleConfig.MarkdownStyleFileName))
         {
             var config = JsonUtility.Deserialize<MarkdownSytleConfig>(MarkdownSytleConfig.MarkdownStyleFileName);
             var builder = new MarkdownValidatorBuilder(host);
             builder.AddValidators(from r in config.Rules where !r.Disable select r.RuleName);
             builder.AddTagValidators(config.TagRules);
             return builder.Create();
         }
     }
     catch (Exception ex)
     {
         Logger.LogWarning($"Fail to init markdown style, details:{Environment.NewLine}{ex.ToString()}");
     }
     return null;
 }
Exemplo n.º 2
0
        public void TestDfmTagValidate()
        {
            var builder = new DfmEngineBuilder(new Options() { Mangle = false });
            var mrb = new MarkdownValidatorBuilder(
                new ContainerConfiguration()
                    .WithAssembly(typeof(DocfxFlavoredMarkdownTest).Assembly)
                    .CreateContainer());
            mrb.AddTagValidators(
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "em", "div" },
                    MessageFormatter = "Invalid tag({0})!",
                    Behavior = TagValidationBehavior.Error,
                    OpeningTagOnly = true,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "h1" },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior = TagValidationBehavior.Warning,
                });
            mrb.AddValidators(HtmlMarkdownTokenValidatorProvider.ContractName);
            builder.Rewriter = mrb.Create();

            var engine = builder.CreateDfmEngine(new DfmRenderer());
            var listener = new TestLoggerListener("test!!!!" + "." + MarkdownValidatorBuilder.MarkdownValidatePhaseName);
            Logger.RegisterListener(listener);
            string result;
            using (new LoggerPhaseScope("test!!!!"))
            {
                result = engine.Markup(@"<div><i>x</i><EM>y</EM><h1>z</h1></div>", "test");
            }
            Logger.UnregisterListener(listener);
            Assert.Equal("<div><i>x</i><EM>y</EM><h1>z</h1></div>", result);
            Assert.Equal(5, listener.Items.Count);
            Assert.Equal(new[] { HtmlMarkdownTokenValidatorProvider.WarningMessage,  "Invalid tag(div)!", "Invalid tag(EM)!", "Warning tag(h1)!", "Warning tag(h1)!" }, from item in listener.Items select item.Message);
        }