Indentation Removed From Code Block Inside Custom HtmlObjectRenderer #219

Closed
opened 2026-01-29 14:30:43 +00:00 by claunia · 1 comment
Owner

Originally created by @basiphobe on GitHub (Aug 27, 2018).

Hi,

I've created an extension that converts custom markdown elements (i.e. [note blah]...[/note]) into figure elements (i.e. <figure><figcaption>blah</figcaption>...</figure>). My parser implements FencedBlockParserBase with a custom object that implements ContainerBlock and IFencedBlock, and my renderer implements HtmlObjectRenderer with the same custom object. I'm not sure if that's the best way to do things or not, but it's working pretty well.

The only problem I'm having is that indentation is being stripped from any code blocks that exist within the custom markdown element. Here's the markdown code (using images to avoid github markdown rendering):

markdown code example

Without my extension loaded, code block indentation is in place, and the output looks like this:

rendered code without custom extension

With my extension loaded, indentation is removed, and the output looks like this:

rendered code with custom extension

I suspect the WriteChildren call in my Write method is the problem:

renderer.Write("<figure class=\"" + obj.Info + "\">\n");
renderer.Write("<figcaption>").Write(caption).Write("</figcaption>\n");
renderer.WriteChildren(obj);
renderer.Write("</figure>\n");

Any suggestions on getting the indentation to remain intact when writing out the custom html?

Originally created by @basiphobe on GitHub (Aug 27, 2018). Hi, I've created an extension that converts custom markdown elements (i.e. [note blah]...[/note]) into figure elements (i.e. ```<figure><figcaption>blah</figcaption>...</figure>```). My parser implements ```FencedBlockParserBase``` with a custom object that implements ```ContainerBlock``` and ```IFencedBlock```, and my renderer implements ```HtmlObjectRenderer``` with the same custom object. I'm not sure if that's the best way to do things or not, but it's working pretty well. The only problem I'm having is that indentation is being stripped from any code blocks that exist within the custom markdown element. Here's the markdown code (using images to avoid github markdown rendering): ![markdown code example](https://i.imgur.com/Kyx4eDq.png) Without my extension loaded, code block indentation is in place, and the output looks like this: ![rendered code without custom extension](https://i.imgur.com/9ISM3eh.png) With my extension loaded, indentation is removed, and the output looks like this: ![rendered code with custom extension](https://i.imgur.com/awAW2g2.png) I suspect the ```WriteChildren``` call in my ```Write``` method is the problem: ``` renderer.Write("<figure class=\"" + obj.Info + "\">\n"); renderer.Write("<figcaption>").Write(caption).Write("</figcaption>\n"); renderer.WriteChildren(obj); renderer.Write("</figure>\n"); ``` Any suggestions on getting the indentation to remain intact when writing out the custom html?
Author
Owner

@basiphobe commented on GitHub (Aug 28, 2018):

I think I answered my own question. Instead of being a rendering issue, it was a parser issue. At the top of TryContinue, if processor.CurrentBlock is FencedCodeBlock, then returning BlockState.Skip avoids the unwanted indentation mangling.

Thanks.

@basiphobe commented on GitHub (Aug 28, 2018): I think I answered my own question. Instead of being a rendering issue, it was a parser issue. At the top of ```TryContinue```, if ```processor.CurrentBlock``` is ```FencedCodeBlock```, then returning ```BlockState.Skip``` avoids the unwanted indentation mangling. Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#219