Example #1
0
        /// <summary>
        /// Attempts to update the content of the supplied <see cref="BlogSnippet"/> from
        /// a YAML front matter block in the supplied array of lines.
        /// </summary>
        /// <param name="snippet">The <see cref="BlogSnippet"/> to be updated.</param>
        /// <param name="lines">An array of lines possibly containing a YAML front matter block.</param>
        private void UpdateFromYamlFrontMatterBlock(BlogSnippet snippet, string[] lines)
        {
            var linesText = string.Join("\n", lines);
            var yaml      = StringUtils.ExtractString(linesText, "---", "---", returnDelimiters: true);

            if (yaml != null)
            {
                snippet.Title      = StringUtils.ExtractString(yaml, "title: ", "\n");
                snippet.Abstract   = StringUtils.ExtractString(yaml, "abstract: ", "\n");
                snippet.Categories = StringUtils.ExtractString(yaml, "categories: ", "\n")
                                     .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                snippet.Keywords = StringUtils.ExtractString(yaml, "keywords: ", "\n")
                                   .Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (DateTime.TryParse(StringUtils.ExtractString(yaml, "postDate: ", "\n"), out DateTime postedOn))
                {
                    snippet.PostedOn = postedOn;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets a collection containing <see cref="BlogSnippet"/> items for all blog posts.
        /// </summary>
        /// <returns>
        /// A collection containing <see cref="BlogSnippet"/> items for all blog posts.
        /// </returns>
        private async Task <IEnumerable <BlogSnippet> > RetrieveAllAsync()
        {
            var snippets = new List <BlogSnippet>();

            foreach (var path in GetAllPaths())
            {
                var snippet = new BlogSnippet
                {
                    Url = $"/{path.Substring(path.LastIndexOf("posts")).Replace("\\", "/")}"
                };

                string markdown = await File.ReadAllTextAsync(path);

                string[] firstLines = StringUtils.GetLines(markdown, 30); // Assume YAML front matter block is in first 30 lines

                if (markdown.StartsWith("---"))
                {
                    UpdateFromYamlFrontMatterBlock(snippet, firstLines);
                }

                if (string.IsNullOrEmpty(snippet.Title))
                {
                    foreach (var line in firstLines.Take(10))
                    {
                        if (line.TrimStart().StartsWith("# "))
                        {
                            snippet.Title = line.TrimStart(new char[] { ' ', '\t', '#' });
                            break;
                        }
                    }
                }

                if (snippet.PostedOn.Equals(DateTime.MinValue))
                {
                    snippet.PostedOn = GetPostDateFromPath(path);
                }

                snippets.Add(snippet);
            }

            return(snippets);
        }