/// <summary>
 /// Renders Markdown as HTML
 /// </summary>
 /// <param name="markdown">Markdown text to turn into HTML</param>
 /// <param name="renderLinksExternal">If true creates all links with target='top'</param>
 /// <returns></returns>
 public string RenderMarkdown(string markdown, bool renderLinksExternal = false, bool usePragmaLines = false)
 {
     return(MarkdownDocument.RenderHtml(markdown, renderLinksExternal, usePragmaLines));
 }
        public TabItem OpenTab(string mdFile = null, MarkdownDocumentEditor editor = null, bool showPreviewIfActive = false, string syntax = "markdown", bool selectTab = true)
        {
            if (mdFile != null && mdFile != "untitled" && (!File.Exists(mdFile) ||
                                                           !AddinManager.Current.RaiseOnBeforeOpenDocument(mdFile)))
            {
                return(null);
            }

            var tab = new TabItem();

            tab.Margin      = new Thickness(0, 0, 3, 0);
            tab.Padding     = new Thickness(2, 0, 7, 2);
            tab.Background  = this.Background;
            tab.ContextMenu = this.Resources["TabItemContextMenu"] as ContextMenu;


            ControlsHelper.SetHeaderFontSize(tab, 13F);

            var wb = new WebBrowser
            {
                AllowDrop  = false,
                Visibility = Visibility.Hidden
            };

            tab.Content = wb;

            if (editor == null)
            {
                dynamic dom = wb.Document;
                editor = new MarkdownDocumentEditor(wb)
                {
                    Window       = this,
                    EditorSyntax = syntax
                };

                var doc = new MarkdownDocument
                {
                    Filename = mdFile ?? @"c:\temp\readme.md"
                };
                if (FileName != "untitled")
                {
                    doc.Load();
                }

                doc.PropertyChanged += (sender, e) =>
                {
                    if (e.PropertyName == "IsDirty")
                    {
                        CommandManager.InvalidateRequerySuggested();
                    }
                };
                editor.MarkdownDocument = doc;

                var headerBinding = new Binding
                {
                    Source = doc,
                    Path   = new PropertyPath("FilenameWithIndicator"),
                    Mode   = BindingMode.OneWay
                };
                BindingOperations.SetBinding(tab, TabItem.HeaderProperty, headerBinding);

                tab.ToolTip = doc.Filename;
            }


            var filename = Path.GetFileName(editor.MarkdownDocument.Filename);

            tab.Tag = editor;

            Title = filename;

            editor.LoadDocument();

            TabItem existingTab = null;

            if (filename != "untitled")
            {
                foreach (TabItem tb in TabControl.Items)
                {
                    var lEditor = tb.Tag as MarkdownDocumentEditor;
                    if (lEditor.MarkdownDocument.Filename == editor.MarkdownDocument.Filename)
                    {
                        existingTab = tb;
                        break;
                    }
                }
            }
            Model.OpenDocuments.Add(editor.MarkdownDocument);
            Model.ActiveDocument = editor.MarkdownDocument;

            if (existingTab != null)
            {
                TabControl.Items.Remove(existingTab);
            }

            tab.IsSelected = false;

            TabControl.Items.Insert(0, tab);

            // Get Tabablz control to insert at the top of the head
            //if (TabControl.Items.Count > 0)
            //    TabablzControl.AddItem(tab, TabControl.Items[0] as TabItem, AddLocationHint.First);
            //else
            //    TabControl.AddToSource(tab);

            if (selectTab)
            {
                TabControl.SelectedItem = tab;

                if (showPreviewIfActive && PreviewBrowser.Width > 5)
                {
                    PreviewMarkdown(); //Model.PreviewBrowserCommand.Execute(ButtonHtmlPreview);
                }
            }

            AddinManager.Current.RaiseOnAfterOpenDocument(editor.MarkdownDocument);

            return(tab);
        }