How to Populate Metadata When Converting Markdown to HTML in ASP.NET
Using a JSON Metadata file, a single view and a dynamic action method to pass metadata into Markdown converted HTML pages
In a previous blog post, I explained how I used a Markdown to HTML conversion tool to turn our Markdown files into HTML webpages for the Aireforge documentation. In summary, all Markdown files were routed through a single dynamic action method with the markdown filename passed as an input parameter. The action method then extracted the content of the Markdown file, converted it to HTML and used a single view to generate each file’s content as and when it was called.
Typically, action methods represent one page, and as such any metadata for a given page (title, metadescription, request path) is hard-coded within the action method:
Given that all of our Markdown files are being converted to HTML from one dynamic action method, this called for a different technique.
Creating a JSON Metadata file
There are only a few metadata properties for each page, so a database call seemed unnecessary. In accordance of Microsoft’s progressions within the .NET Core architecture, I opted for an embedded JSON file to hold this data. After considering the data required, I structured the JSON file in the following way:
This covered all properties I wanted to pass into my view. As we also intend to use the Markdown conversion process for blog posts, I prearranged the JSON to allow separation of these Markdown types to speed up metadata selection and readability. This hierarchy also allowed a unified Document class for both Blog and Documentation properties in the MarkdownLookup Model:
Extracting the JSON Property Values
With the JSON file in place, I needed a way to extract the values and pass these into the view for every Markdown file. The PopulateMDLookup method takes the filename (element) and the document type (in this case either Blog or Documentation). This method then reads the JSON file and deserializes it into the MarkdownLookup model we created. A Linq query is then used to refine the full list of each document's metadata to those that are of the same type (in this case, Documentation) and then isolates the single document by the filename:
The PopulateMDLookup method is then called by the dynamic action method by using the filename that was already being passed in, along with the type of Markdown file we are looking for. A Document object is returned with all the metadata values we require.
To check the process worked, I inspected the header elements in the browser while running the web app from my local environment:
Interested in our services?Contact Us
Here’s what we've been up to recently.
xTEN is now Cyber Essentials Plus certified
At xTEN security is a priority. Recently taken over by the IASME consortium (as of 1 April 2020), the Cyber Essentials certification consists of a self-assessment of 5 basic security controls which is then verified by a qualified assessor.