Esempio n. 1
0
            public DfmService(string baseDir, string templateDir, ImmutableDictionary <string, string> tokens, IEnumerable <IMarkdownTokenTreeValidator> tokenTreeValidator, IReadOnlyList <string> fallbackFolders = null)
            {
                var options = DocfxFlavoredMarked.CreateDefaultOptions();

                options.ShouldExportSourceInfo = true;
                _builder = DocfxFlavoredMarked.CreateBuilder(baseDir, templateDir, options, fallbackFolders);
                _builder.TokenTreeValidator = MarkdownTokenTreeValidatorFactory.Combine(tokenTreeValidator);
                _tokens   = tokens;
                _renderer = new DfmRenderer {
                    Tokens = _tokens
                };
                _incrementalContextHash = ComputeIncrementalContextHash(baseDir, templateDir, tokenTreeValidator);
            }
        public MarkdigMarkdownRenderer(Stack <IMarkdownToken> processedBlockTokens, string basePath, bool useLegacyMode = true, MigrationRule rule = MigrationRule.All)
        {
            var option = DocfxFlavoredMarked.CreateDefaultOptions();

            option.LegacyMode = useLegacyMode;
            var builder = new DfmEngineBuilder(option);
            var render  = new DfmRenderer();

            _dfmEngine     = builder.CreateDfmEngine(render);
            _dfmHtmlRender = new DfmHtmlRender(useLegacyMode);
            _rule          = rule;

            var parameter = new MarkdownServiceParameters
            {
                BasePath   = basePath,
                Extensions = new Dictionary <string, object>
                {
                    { "EnableSourceInfo", false }
                }
            };

            _service = new MarkdigMarkdownService(parameter);
            _processedBlockTokens = processedBlockTokens;
        }
        private string RenderHTMLBlock(string markdown, string filepath)
        {
            //DFM engine
            var htmlRender = new DfmRenderer();
            var dfmengine  = _builder.CreateDfmEngine(htmlRender);

            //Markdig engine
            var parameter = new MarkdownServiceParameters
            {
                BasePath   = _workingFolder,
                Extensions = new Dictionary <string, object>
                {
                    { "EnableSourceInfo", false }
                }
            };

            var markdigService = new MarkdigMarkdownService(parameter);
            var markdigToken   = markdigService.Parse(markdown, Path.GetFileName(filepath));

            if (markdigToken == null)
            {
                return(markdown);
            }

            var htmlBlockTokens = markdigToken.Where(token => token is HtmlBlock).ToList();

            if (htmlBlockTokens.Count == 0)
            {
                return(markdown);
            }

            var lines = markdown.Split('\n');

            var lineIndex = 0;
            var result    = new StringBuilder();

            foreach (HtmlBlock block in htmlBlockTokens)
            {
                if (!_useLegacyMode && !string.IsNullOrEmpty(lines[block.Line]) &&
                    lines[block.Line].StartsWith("<!--"))
                {
                    continue;
                }

                var blockStart = block.Line;
                var blockEnd   = block.Line + block.Lines.Count - 1;

                while (lineIndex < blockStart)
                {
                    if (lineIndex != 0)
                    {
                        result.Append('\n');
                    }
                    result.Append(lines[lineIndex]);
                    lineIndex++;
                }

                var tempMarkdown = new StringBuilder();
                while (lineIndex <= blockEnd)
                {
                    if (lineIndex != 0)
                    {
                        if (lineIndex == blockStart)
                        {
                            result.Append('\n');
                        }
                        else
                        {
                            tempMarkdown.Append('\n');
                        }
                    }
                    tempMarkdown.Append(lines[lineIndex]);
                    lineIndex++;
                }

                // Wrap in <migratetemproot> to make sure DFM recognize it as a single Html Block
                var tempResult = dfmengine.Markup($"<migratetemproot>{tempMarkdown.ToString()}</migratetemproot>", Path.GetFileName(filepath)).TrimEnd('\n');

                if (tempResult.StartsWith("<migratetemproot>") && tempResult.EndsWith("</migratetemproot>"))
                {
                    tempResult = tempResult.Substring("<migratetemproot>".Length, tempResult.Length - "<migratetemproot></migratetemproot>".Length);
                }
                else
                {
                    tempResult = tempMarkdown.ToString();
                }

                result.Append(tempResult);
            }

            while (lineIndex < lines.Count())
            {
                if (lineIndex != 0)
                {
                    result.Append('\n');
                }
                result.Append(lines[lineIndex]);
                lineIndex++;
            }

            return(result.ToString());
        }