示例#1
0
 private static IMarkdownTokenRewriter InitMarkdownStyleCop(CompositionHost host)
 {
     try
     {
         if (File.Exists(MarkdownStyleCopFileName))
         {
             var rules = JsonUtility.Deserialize<MarkdownTagValidationRule[]>(MarkdownStyleCopFileName);
             var builder = new MarkdownRewriterBuilder(host);
             builder.AddValidators(rules);
             return builder.Create();
         }
     }
     catch (Exception ex)
     {
         Logger.LogWarning($"Fail to init markdown stylecop, details:{Environment.NewLine}{ex.ToString()}");
     }
     return null;
 }
        public void TestDfmTagValidate()
        {
            var builder = new DfmEngineBuilder(new MarkdownLite.Options() { Mangle = false });
            var mrb = new MarkdownRewriterBuilder(null);
            mrb.AddValidators(
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "em", "div" },
                    MessageFormatter = "Invalid tag({0})!",
                    Behavior = TagRewriteBehavior.Error,
                    OpeningTagOnly = true,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "h1" },
                    MessageFormatter = "Invalid tag({0}), Removed!",
                    Behavior = TagRewriteBehavior.ErrorAndRemove,
                });
            builder.Rewriter = mrb.Create();

            var engine = builder.CreateEngine(new DfmRenderer());
            var listener = new TestLoggerListener("test!!!!");
            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>z</div>", result);
            Assert.Equal(4, listener.Items.Count);
            Assert.Equal(new[] { "Invalid tag(div)!", "Invalid tag(EM)!", "Invalid tag(h1), Removed!", "Invalid tag(h1), Removed!" }, from item in listener.Items select item.Message);
        }