protected override void Write()

in src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockHtmlRenderer.cs [122:234]


	protected override void Write(HtmlRenderer renderer, EnhancedCodeBlock block)
	{
		if (block is AppliesToDirective appliesToDirective)
		{
			RenderAppliesToHtml(renderer, appliesToDirective);
			return;
		}

		var callOuts = block.UniqueCallOuts;

		var slice = Code.Create(new CodeViewModel
		{
			CrossReferenceName = string.Empty,// block.CrossReferenceName,
			Language = block.Language,
			Caption = block.Caption,
			ApiCallHeader = block.ApiCallHeader
		});

		RenderRazorSlice(slice, renderer, block);
		if (!block.InlineAnnotations && callOuts.Count > 0)
		{
			var index = block.Parent!.IndexOf(block);
			if (index == block.Parent!.Count - 1)
			{
				block.EmitError("Code block with annotations is not followed by any content, needs numbered list");
				return;
			}

			var nonCommentNonListCount = 0;
			ListBlock? listBlock = null;
			var currentIndex = index + 1;

			// Process blocks between the code block and ordered list, removing comments and allowing only one non-comment block
			while (currentIndex < block.Parent.Count)
			{
				var nextBlock = block.Parent[currentIndex];

				if (nextBlock is ListBlock lb)
				{
					listBlock = lb;
					break;
				}
				else if (IsCommentBlock(nextBlock))
				{
					_ = renderer.Render(nextBlock);
					_ = block.Parent.Remove(nextBlock);
				}
				else
				{
					nonCommentNonListCount++;
					if (nonCommentNonListCount > 1)
					{
						block.EmitError("More than one content block between code block with annotations and its list");
						return;
					}

					_ = renderer.Render(nextBlock);
					_ = block.Parent.Remove(nextBlock);
				}
			}

			if (listBlock == null)
			{
				block.EmitError("Code block with annotations is not followed by a list");
				return;
			}

			if (listBlock.Count < callOuts.Count)
			{
				block.EmitError($"Code block has {callOuts.Count} callouts but the following list only has {listBlock.Count}");
				return;
			}

			_ = block.Parent.Remove(listBlock);

			_ = renderer.WriteLine("<ol class=\"code-callouts\">");
			foreach (var child in listBlock)
			{
				var listItem = (ListItemBlock)child;
				var previousImplicit = renderer.ImplicitParagraph;
				renderer.ImplicitParagraph = !listBlock.IsLoose;

				_ = renderer.EnsureLine();
				if (renderer.EnableHtmlForBlock)
				{
					_ = renderer.Write("<li");
					_ = renderer.WriteAttributes(listItem);
					_ = renderer.Write('>');
				}

				renderer.WriteChildren(listItem);

				if (renderer.EnableHtmlForBlock)
					_ = renderer.WriteLine("</li>");

				_ = renderer.EnsureLine();
				renderer.ImplicitParagraph = previousImplicit;
			}
			_ = renderer.WriteLine("</ol>");
		}
		else if (block.InlineAnnotations)
		{
			_ = renderer.WriteLine("<ol class=\"code-callouts\">");
			foreach (var c in block.UniqueCallOuts)
			{
				_ = renderer.WriteLine("<li>");
				_ = renderer.WriteLine(c.Text);
				_ = renderer.WriteLine("</li>");
			}

			_ = renderer.WriteLine("</ol>");
		}
	}