Skip to content

MarkDown For Resource Values

Rick Strahl edited this page Jun 27, 2015 · 8 revisions

You can use Markdown text for resource values when you create database resources that are retrieved using the database resource managers or providers.

By setting the ValueType of the resource record to 2 the value is assumed to be using Markdown text, which causes the content to be run through a Markdown parser (CommonMark.NET). This allows you to use rich HTML content for content.

You can set the ValueType explicitly in the raw database, via the API, or more commonly through the Web editing interface. The Edit Resource form contains a Type dropdown that allows you set the ValueType:

This opens up a number of scenarios that go beyond localization. You can use Westwind.Globalization as a simplistic CMS where content is stored in a database, and use Markdown to provide some minimal formatting for content including the ability to edit that content using the Web Resource Editor.

Where is Markdown applied?

Markdown conversion - as are all value conversions - is applied only in a couple of very specific but significant situations.

ResourceSet Generation

MarkDown conversion is applied when a ResourceSet is created which affects all ResourceManager, ResourceProvider, DbRes and strongly typed resource access that goes through the standard .NET resource interfaces. This means any localized content gets the transformed HTML from the original markdown text. Transformation occurs only once when the ResourceSet is created on first access.

The specific DbResourceDataManager methods that apply the conversion:

  • GetResourceSet
  • GetAllResources
Resx Exports

Markdown conversion is also applied when resources are exported to Resx files. Since the .NET Resx processor has no knowledge of the ValueType or any way to convert, the only way to provide the transformed data is when the values are actually exported into the Resx file.

JavaScriptResourceHandler

The JavasScriptResourceHandler internally fires GetResourceSet() so it too gets the transformed Markdown resources.

Not anywhere else

Markdown conversion is not applied in any of the API methods that return full ResourceItem data - the data is applied as uninterpreted raw Markdown text.

Manual Markdown Application

If you need to manually apply Markdown you can use the CommonMark.NET library to apply the Markdown translation manually.

For example, if you retrieved a ResourceItem through the API you can transform the text with the following code:

string html = CommonMark.CommonMarkConverter.Convert(resItem.Value as string);

See Also: