private MarkdownModel ParseMarkdownToModel(string markdown, MarkdownModel model = null) { if (model == null) { model = new MarkdownModel(); } if (model.FolderConfiguration.ExtractTitle) { var firstLines = StringUtils.GetLines(markdown, 50).ToList(); var firstLinesText = String.Join("\n", firstLines); // Assume YAML if (markdown.StartsWith("---")) { var yaml = StringUtils.ExtractString(firstLinesText, "---", "---", returnDelimiters: true); if (yaml != null) { model.Title = StringUtils.ExtractString(yaml, "title: ", "\n"); model.YamlHeader = yaml.Replace("---", "").Trim(); } } // if we don't have Yaml headers the header has to be closer to the top firstLines = firstLines.Take(10).ToList(); if (string.IsNullOrEmpty(model.Title)) { foreach (var line in firstLines) { if (line.TrimStart().StartsWith("# ")) { model.Title = line.TrimStart(new char[] { ' ', '\t', '#' }); break; } } } if (string.IsNullOrEmpty(model.Title)) { for (var index = 0; index < firstLines.Count; index++) { var line = firstLines[index]; if (line.TrimStart().StartsWith("===") && index > 0) { // grab the previous line model.Title = firstLines[index - 1].Trim(); break; } } } } model.RawMarkdown = markdown; model.RenderedMarkdown = Markdown.ParseHtmlString(markdown, sanitizeHtml: model.FolderConfiguration.SanitizeHtml); return(model); }
private MarkdownModel ParseMarkdownToModel(string markdown, MarkdownModel model = null) { if (model == null) { model = new MarkdownModel(); } if (model.FolderConfiguration.ExtractTitle) { var firstLines = StringUtils.GetLines(markdown, 30); var firstLinesText = String.Join("\n", firstLines); // Assume YAML if (markdown.StartsWith("---")) { var yaml = StringUtils.ExtractString(firstLinesText, "---", "---", returnDelimiters: true); if (yaml != null) { model.Title = StringUtils.ExtractString(yaml, "title: ", "\n"); model.YamlHeader = yaml.Replace("---", "").Trim(); } } if (model.Title == null) { foreach (var line in firstLines.Take(10)) { if (line.TrimStart().StartsWith("# ")) { model.Title = line.TrimStart(new char[] { ' ', '\t', '#' }); break; } } } } model.RawMarkdown = markdown; model.RenderedMarkdown = Markdown.ParseHtmlString(markdown, sanitizeHtml: model.FolderConfiguration.SanitizeHtml); return(model); }
public Task InvokeAsync(HttpContext context) { var path = context.Request.Path.Value; if (string.IsNullOrEmpty(path)) { return(_next(context)); } bool hasExtension = !string.IsNullOrEmpty(Path.GetExtension(path)); bool hasMdExtension = path.EndsWith(".md"); bool isRoot = path == "/"; bool processAsMarkdown = false; var basePath = _env.WebRootPath; var relativePath = path; relativePath = PathHelper.NormalizePath(relativePath).Substring(1).TrimEnd('\\', '/'); var pageFile = Path.Combine(basePath, relativePath); // process any Markdown file that has .md extension explicitly foreach (var folder in _configuration.MarkdownProcessingFolders) { if (!path.StartsWith(folder.RelativePath, StringComparison.InvariantCultureIgnoreCase)) { continue; } if (isRoot && folder.RelativePath != "/") { continue; } if (context.Request.Path.Value.EndsWith(".md", StringComparison.InvariantCultureIgnoreCase)) { processAsMarkdown = true; } else if (path.StartsWith(folder.RelativePath, StringComparison.InvariantCultureIgnoreCase) && (folder.ProcessExtensionlessUrls && !hasExtension || hasMdExtension && folder.ProcessMdFiles)) { if (!hasExtension && Directory.Exists(pageFile)) { continue; } if (!hasExtension) { pageFile += ".md"; } if (!File.Exists(pageFile)) { continue; } processAsMarkdown = true; } if (processAsMarkdown) { var model = new MarkdownModel { FolderConfiguration = folder, RelativePath = path, PhysicalPath = pageFile }; // push the model into the context for controller to pick up context.Items["MarkdownProcessor_Model"] = model; // rewrite path to our controller so we can use _layout page context.Request.Path = "/markdownprocessor/markdownpage"; break; } } return(_next(context)); }