I embed source code in blog posts using GitHub Gists. Gists provide provide many benefits, including a live view of the code (update code in Gist, code is updated in blog post), syntax highlighting, line numbers (useful if readers have questions or comments about the code), scroll bars, and links to access source code in GitHub repository or the raw plaintext code.

One problem that occurs when embedding source code via Gists is the code is not indexed by the WordPress search engine. Therefore, searching for a class name or namespace will not find all blog posts that reference the class or use the namespace. This is easily remedied, though, by adding the source code in an HTML comment. While this violation of the DRY Principle irks me, I understand the need for it. Because Gists are embedded using a Javascript reference, and the WordPress search engine does not download and index remote content, the source code referred to by the Javascript reference is not indexed.

So, if you’d like source code you’ve embedded in your WordPress blog to be searchable, include it in HTML comments immediately after the Gist Javascript references. Like this:

<script src="https://gist.github.com/ekmadsen/80cf1ad854a74cb889a941575dd96a4a.js"></script>


<!--
using System.Data.SqlClient;
using System.Threading.Tasks;
using Dapper;
using ErikTheCoder.Sandbox.Dapper.Contract;
using Microsoft.AspNetCore.Mvc;


namespace ErikTheCoder.Sandbox.Dapper.Service.Controllers
{
    public class MappingController : Controller, IMappingService
    {
        private readonly IAppSettings _appSettings;


        public MappingController(IAppSettings AppSettings)
        {
            _appSettings = AppSettings;
        }


        [HttpGet("/mapping/getopenservicecalls")]
        public async Task<GetOpenServiceCallsResponse> GetOpenServiceCallsAsync()
        {
            GetOpenServiceCallsResponse response = new GetOpenServiceCallsResponse();
            // Each row in SQL result represents a unique service call but may contain duplicate customers and technicians.
            const string sql = @"select sc.Id, sc.Scheduled, sc.[Open], c.Id, c.Name, c.Address, c.City, c.State,
                c.ZipCode, t.Id, t.Name
                from ServiceCalls sc
                inner join Customers c on sc.CustomerId = c.Id
                inner join Technicians t on sc.TechnicianId = t.Id
                where sc.[Open] = 1";
            using (SqlConnection connection = new SqlConnection(_appSettings.Database))
            {
                await connection.OpenAsync();
                await connection.QueryAsync<ServiceCall, Customer, Technician, ServiceCall>(sql,
                  (ServiceCall, Customer, Technician) =>
                {
                    // This lambda method runs once per row in SQL result.
                    response.ServiceCalls.Add(ServiceCall);
                    // Reference existing customers and technicians or add to collection if first time encountered.
                    if (!response.Customers.TryGetValue(Customer.Id, out Customer customer))
                    {
                        // Customer mapped by Dapper not in collection.
                        customer = Customer;
                        response.Customers.Add(customer);
                    }
                    if (!response.Technicians.TryGetValue(Technician.Id, out Technician technician))
                    {
                        // Technician mapped by Dapper not in collection.
                        technician = Technician;
                        response.Technicians.Add(technician);
                    }
                    // Configure relationships among objects.
                    ServiceCall.Customer = customer;
                    ServiceCall.Technician = technician;
                    customer.ServiceCalls.Add(ServiceCall);
                    if (!customer.Technicians.Contains(technician.Id))
                    {
                        // Technician not found in customer's collection.
                        customer.Technicians.Add(technician);
                    }
                    technician.ServiceCalls.Add(ServiceCall);
                    if (!technician.Customers.Contains(customer.Id))
                    {
                        // Customer not found in technician's collection.
                        technician.Customers.Add(customer);
                    }
                    return null;
                });
            }
            return response;
        }
    }
}
-->

Test it for yourself: Search for “System.Data.SqlClient” and WordPress will find this post, my The Best Domain-Specific Language for Manipulating Data is SQL post, and any future posts I write that include code that references the System.Data.SqlClient namespace.

Leave a Reply

Your email address will not be published.