private void RefreshDocumentOutline() { var oldSelection = ActiveEditor.GetSelectionRange(); var oldScrollPosition = ActiveEditor.GetScrollPosition(); var md = ActiveEditor.GetMarkdown(); if (!md.Contains(STR_StartDocumentOutline)) { return; } var oldToc = StringUtils.ExtractString(md, STR_StartDocumentOutline, STR_EndDocumentOutline, returnDelimiters: true); var mdLines = md.GetLines(); int tocPosition = -1; for (int i = 0; i < mdLines.Length; i++) { if (mdLines[i].Contains(STR_StartDocumentOutline, StringComparison.Ordinal)) { tocPosition = i; break; } } var newToc = STR_StartDocumentOutline + Environment.NewLine + Environment.NewLine + OutlineModel.CreateMarkdownOutline(ActiveDocument, tocPosition).TrimEnd() + Environment.NewLine + Environment.NewLine + STR_EndDocumentOutline; // We don't want to disturb the user more than needed. // So let's check if we really have to replace the TOC. if (!SelectiveStringComparer.Equals(oldToc, newToc)) { ActiveEditor.FindAndReplaceText(oldToc, newToc); // Restore the old selection. To do so we need to get the row offset var offset = oldSelection.StartRow > tocPosition?StringUtils.CountLines(newToc) - StringUtils.CountLines(oldToc) : 0; ActiveEditor.SetScrollPosition(oldScrollPosition + offset); ActiveEditor.SetSelectionRange(oldSelection.StartRow + offset, oldSelection.StartColumn, oldSelection.EndRow + offset, oldSelection.EndColumn); ActiveEditor.SetEditorFocus(); RefreshPreview(); ShowStatus("Your TOC was updated", 2000); } }
public void MarkdownOutlineTest() { var appModel = new AppModel(null); mmApp.Model = appModel; var doc = new MarkdownDocument(); doc.CurrentText = MarkdownText; var model = new DocumentOutlineModel(); var md = model.CreateMarkdownOutline(doc); Console.WriteLine(md); }
public void MarkdownOutlineWithMultipleLevelSkips() { var appModel = new AppModel(null); mmApp.Model = appModel; var doc = new MarkdownDocument(); doc.CurrentText = markdownWithSkippedHeaders; var model = new DocumentOutlineModel(); var md = model.CreateMarkdownOutline(doc); Console.WriteLine(md); }