public static Dictionary <string, MarkdownFragment> LoadMarkdownFragment(string fileName) { if (string.IsNullOrEmpty(fileName)) { return(null); } if (!File.Exists(fileName)) { return(new Dictionary <string, MarkdownFragment>()); } var markdown = File.ReadAllText(fileName); var ast = MDService.Parse(markdown, Path.GetFileName(fileName)); var models = new MarkdownFragmentsCreater().Create(ast).ToList(); return(models.ToDictionary(m => m.Uid, m => m.ToMarkdownFragment(markdown))); }
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()); }