Esempio 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;
 }
Esempio 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[]
            {
                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,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "script" },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior = TagValidationBehavior.Warning,
                },
                new MarkdownTagValidationRule
                {
                    TagNames = new List<string> { "pre" },
                    MessageFormatter = "Warning tag({0})!",
                    Behavior = TagValidationBehavior.Warning,
                },
            });
            mrb.AddValidators(new[]
            {
                new MarkdownValidationRule
                {
                    ContractName =  HtmlMarkdownTokenValidatorProvider.ContractName,
                }
            });
            builder.Rewriter = mrb.CreateRewriter();

            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<pre><code>a*b*c</code></pre></h1></div>

<script>alert(1);</script>", "test");
            }
            Logger.UnregisterListener(listener);
            Assert.Equal(@"<div><i>x</i><EM>y</EM><h1>z<pre><code>a*b*c</code></pre></h1></div>

<script>alert(1);</script>".Replace("\r\n", "\n"), result);
            Assert.Equal(8, listener.Items.Count);
            Assert.Equal(new[]
            {
                HtmlMarkdownTokenValidatorProvider.WarningMessage,
                "Invalid tag(div)!",
                "Invalid tag(EM)!",
                "Warning tag(h1)!",
                "Warning tag(pre)!",
                "Warning tag(h1)!",
                "Html Tag!",
                "Warning tag(script)!",
            }, from item in listener.Items select item.Message);
        }