Utility method for replacing built-in object renderer #103

Closed
opened 2026-01-29 14:26:48 +00:00 by claunia · 5 comments
Owner

Originally created by @Daniel15 on GitHub (Mar 19, 2017).

Hello! I'm trying to use Markdig to replace MarkdownDeep. Currently I pre-process GitHub-style code fences via a regular expression, as MarkdownDeep does not support them natively. I'd like to remove this as Markdig is nice and supports code fences out-of-the-box, which is awesome. However, I want to output some different HTML to support my current configuration using highlight.js.

Currently I'm doing something like this to replace the built-in CodeBlockRenderer with my own one

var originalCodeRenderer = renderer.ObjectRenderers.FindExact<CodeBlockRenderer>();
if (originalCodeRenderer != null)
{
	renderer.ObjectRenderers.InsertAfter<CodeBlockRenderer>(new HighlightCodeBlockRenderer());
	renderer.ObjectRenderers.Remove(originalCodeRenderer);
}

This works, but it seems a bit verbose. It would be nice to have a ReplaceWith utility method so I could do something like:

renderer.ObjectRenderers.ReplaceWith<CodeBlockRenderer>(new HighlightCodeBlockRenderer());

I guess I could just have an extension method for this though. Just an idea :)

Thanks for the awesome project!

Originally created by @Daniel15 on GitHub (Mar 19, 2017). Hello! I'm trying to use Markdig to replace MarkdownDeep. Currently I pre-process GitHub-style code fences via a regular expression, as MarkdownDeep does not support them natively. I'd like to remove this as Markdig is nice and supports code fences out-of-the-box, which is awesome. However, I want to output some different HTML to support my current configuration using highlight.js. Currently I'm doing something like this to replace the built-in CodeBlockRenderer with my own one ```csharp var originalCodeRenderer = renderer.ObjectRenderers.FindExact<CodeBlockRenderer>(); if (originalCodeRenderer != null) { renderer.ObjectRenderers.InsertAfter<CodeBlockRenderer>(new HighlightCodeBlockRenderer()); renderer.ObjectRenderers.Remove(originalCodeRenderer); } ``` This works, but it seems a bit verbose. It would be nice to have a `ReplaceWith` utility method so I could do something like: ``` renderer.ObjectRenderers.ReplaceWith<CodeBlockRenderer>(new HighlightCodeBlockRenderer()); ``` I guess I could just have an extension method for this though. Just an idea :) Thanks for the awesome project!
claunia added the enhancement label 2026-01-29 14:26:48 +00:00
Author
Owner

@Daniel15 commented on GitHub (Mar 19, 2017):

This is the extension method I wrote, seems to be working fine so far:

public static ObjectRendererCollection Replace<TOriginal>(
	this ObjectRendererCollection renderers, 
	IMarkdownObjectRenderer replacement
) where TOriginal : IMarkdownObjectRenderer
{
	var index = renderers.FindIndex(renderer => renderer.GetType() == typeof(TOriginal));
	if (index == -1)
	{
		throw new InvalidOperationException($"Could not find original object renderer for {typeof(TOriginal)}");
	}
	renderers.RemoveAt(index);
	renderers.Insert(index, replacement);
	return renderers;
}

Usage:

renderer.ObjectRenderers.Replace<CodeBlockRenderer>(new HighlightCodeBlockRenderer());
@Daniel15 commented on GitHub (Mar 19, 2017): This is the extension method I wrote, seems to be working fine so far: ```csharp public static ObjectRendererCollection Replace<TOriginal>( this ObjectRendererCollection renderers, IMarkdownObjectRenderer replacement ) where TOriginal : IMarkdownObjectRenderer { var index = renderers.FindIndex(renderer => renderer.GetType() == typeof(TOriginal)); if (index == -1) { throw new InvalidOperationException($"Could not find original object renderer for {typeof(TOriginal)}"); } renderers.RemoveAt(index); renderers.Insert(index, replacement); return renderers; } ``` Usage: ``` renderer.ObjectRenderers.Replace<CodeBlockRenderer>(new HighlightCodeBlockRenderer()); ```
Author
Owner

@xoofx commented on GitHub (Mar 19, 2017):

It makes sense. Feel free to open a PR for OrderedList.cs

@xoofx commented on GitHub (Mar 19, 2017): It makes sense. Feel free to open a PR for [OrderedList.cs](https://github.com/lunet-io/markdig/blob/master/src/Markdig/Helpers/OrderedList.cs)
Author
Owner

@Daniel15 commented on GitHub (Mar 19, 2017):

Thanks, I'll do that soon! :)

@Daniel15 commented on GitHub (Mar 19, 2017): Thanks, I'll do that soon! :)
Author
Owner

@xoofx commented on GitHub (Mar 19, 2017):

Note that I have reverted back the changes related to VS2017 on master. Make sure to use the latest

@xoofx commented on GitHub (Mar 19, 2017): Note that I have reverted back the changes related to VS2017 on master. Make sure to use the latest
Author
Owner

@Daniel15 commented on GitHub (Mar 19, 2017):

Submitted as #103

@Daniel15 commented on GitHub (Mar 19, 2017): Submitted as #103
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#103