|
| 1 | +// Copyright (c) Duende Software. All rights reserved. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using Docs.Mcp.Database; |
| 5 | +using HtmlAgilityPack; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using ReverseMarkdown; |
| 8 | +using SimpleFeedReader; |
| 9 | + |
| 10 | +namespace Docs.Indexer.Indexers; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Indexes blog articles from the Duende Software RSS feed. |
| 14 | +/// </summary> |
| 15 | +public sealed class BlogIndexer(McpDb db, HttpClient httpClient) |
| 16 | +{ |
| 17 | + private const string RssFeedUrl = "https://duendesoftware.com/rss.xml"; |
| 18 | + private static readonly DateTime ReferenceDate = new(2024, 10, 01); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Fetch and index blog articles from the RSS feed. |
| 22 | + /// </summary> |
| 23 | + public async Task IndexAsync() |
| 24 | + { |
| 25 | + Console.WriteLine($"Fetching RSS feed: {RssFeedUrl}"); |
| 26 | + |
| 27 | + var reader = new FeedReader(); |
| 28 | + var items = await reader.RetrieveFeedAsync(RssFeedUrl); |
| 29 | + |
| 30 | + // Filter to blog posts since the reference date |
| 31 | + var blogItems = items |
| 32 | + .Where(it => it.PublishDate >= ReferenceDate && it.Categories?.Contains("blog") == true) |
| 33 | + .ToList(); |
| 34 | + |
| 35 | + Console.WriteLine($"Found {blogItems.Count} blog posts since {ReferenceDate:yyyy-MM-dd}"); |
| 36 | + |
| 37 | + var indexedCount = 0; |
| 38 | + foreach (var item in blogItems) |
| 39 | + { |
| 40 | + if (item.Uri == null) |
| 41 | + { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + await IndexBlogPostAsync(item.Title ?? "Untitled", item.GetSummary(), item.Uri); |
| 48 | + indexedCount++; |
| 49 | + Console.WriteLine($" Indexed: {item.Title}"); |
| 50 | + } |
| 51 | + catch (Exception ex) |
| 52 | + { |
| 53 | + Console.WriteLine($" Error indexing {item.Title}: {ex.Message}"); |
| 54 | + throw; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + await db.SaveChangesAsync(); |
| 59 | + Console.WriteLine($"Indexed {indexedCount} blog articles"); |
| 60 | + } |
| 61 | + |
| 62 | + private async Task IndexBlogPostAsync(string title, string? description, Uri url) |
| 63 | + { |
| 64 | + // Fetch the HTML content |
| 65 | + var htmlContent = await httpClient.GetStringAsync(url); |
| 66 | + |
| 67 | + // Parse HTML and find content section |
| 68 | + var htmlDocument = new HtmlDocument(); |
| 69 | + htmlDocument.LoadHtml(htmlContent); |
| 70 | + |
| 71 | + // Try to find the main content section |
| 72 | + var content = htmlDocument.DocumentNode.SelectSingleNode("//section[@class='page-content alt markdown']") |
| 73 | + ?? htmlDocument.DocumentNode.SelectSingleNode("//article") |
| 74 | + ?? htmlDocument.DocumentNode.SelectSingleNode("//main"); |
| 75 | + |
| 76 | + if (content == null) |
| 77 | + { |
| 78 | + Console.WriteLine($" Warning: Could not find content section for {title}"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Convert HTML to Markdown |
| 83 | + var converter = new Converter(new Config |
| 84 | + { |
| 85 | + GithubFlavored = true, |
| 86 | + RemoveComments = true |
| 87 | + }); |
| 88 | + |
| 89 | + var markdownContent = converter.Convert(content.InnerHtml); |
| 90 | + |
| 91 | + // Combine description with content if available |
| 92 | + var fullContent = !string.IsNullOrEmpty(description) |
| 93 | + ? $"Summary: {description}\n\n---\n\n{markdownContent}" |
| 94 | + : markdownContent; |
| 95 | + |
| 96 | + await db.Database.ExecuteSqlRawAsync( |
| 97 | + "INSERT INTO FTSBlogArticle (Id, Title, Content) VALUES ({0}, {1}, {2})", |
| 98 | + Guid.NewGuid().ToString(), |
| 99 | + title, |
| 100 | + fullContent); |
| 101 | + } |
| 102 | +} |
0 commit comments