I try to add a custom tag helper to my project that would convert a markdown text to html. For the conversion I tried using both Makrdig.Markdown and CommonMarkConverter.Convert, without success, but I think that the problem lies in not detecting my implementation by the razor page.
My TagHelper:
using Markdig;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace CustomTagHelpers.Helpers
{
[HtmlTargetElement("markdown")]
public class MarkdownTagHelper : TagHelper
{
[HtmlAttributeName("for-content")]
public ModelExpression Content { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagMode = TagMode.SelfClosing;
output.TagName = "markdown-helper";
var markdown = Content.Model.ToString();
//var html = CommonMarkConverter.Convert(markdown);
var html = Markdig.Markdown.ToHtml(markdown);
output.Content.SetHtmlContent(html);
}
}
I've added it to the _ViewImports file
@addTagHelper *, CustomTagHelpers.Helpers MarkdownTagHelper
And that's the code using my tag helper in my view, HelloMarkdown is an attribute located in my viewmodel linked to the current view, it's a public string with [BindProperty]
<markdown for-content="HelloMarkdown"> </markdown>
Lastly, if I inspect the html code in my browser, it seems to interpret it literally, so it's the same as in the code:
<markdown for-content="HelloMarkdown"> </markdown>
While I would like to get something like that
<markdown-helper><p><em>Hello World</em></p></markdown-helper>
