protected void DoWriteMarkdown(string text, MarkdownOptions options)
        {
            options ??= new MarkdownOptions();

            var book = options.Book?.Document ?? Document;

            book.BeginUpdate();
            try
            {
                DocumentPosition rangeStart = null, rangeEnd = null;

                var html = Markdown.ToHtml(text);

                var lineRange = book.AppendHtmlText(html, DevExpress.XtraRichEdit.API.Native.InsertOptions.KeepSourceFormatting);

                if (rangeStart == null)
                {
                    rangeStart = lineRange.Start;
                }

                if (!options.NoLineBreaks)
                {
                    lineRange = book.AppendText(Environment.NewLine);
                }

                rangeEnd = lineRange.End;

                if (rangeStart != null && rangeEnd != null)
                {
                    var range = book.CreateRange(rangeStart, rangeEnd.ToInt() - rangeStart.ToInt());

                    if (!string.IsNullOrWhiteSpace(options.ParagraphStyle))
                    {
                        var style = book.ParagraphStyles[options.ParagraphStyle] ?? throw new Exception($"Paragraph style '{options.ParagraphStyle}' does not exist.");
                        var pp    = book.BeginUpdateParagraphs(range);
                        try
                        {
                            pp.Style = style;
                        }
                        finally
                        {
                            book.EndUpdateParagraphs(pp);
                        }
                    }

                    Script.Book.SCBook.AddComments(book, range, options);

                    if (options.ExpandFields)
                    {
                        ExpandFieldsInBookRange(range, Host?.Spreadsheet?.Workbook, options.Snippets);
                    }

                    WriteRangeToConsole(book, range);
                }

                if (rangeEnd != null)
                {
                    book.CaretPosition = rangeEnd;
                    Script.Book.SCBook.ResetBookFormatting(book, rangeEnd);
                    ScrollToCaret();
                }
            }
            finally
            {
                book.EndUpdate();
            }
        }
 public SCBook WriteMarkdown(string text, MarkdownOptions options = null)
 {
     ExecuteSynchronized(options, () => DoWriteMarkdown(text, options));
     return(this);
 }