<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using CommonMark;
using HtmlAgilityPack;
using mshtml;
using MarkdownEdit.Models;
using MarkdownEdit.Properties;

namespace MarkdownEdit.Controls
{
    public partial class Preview : INotifyPropertyChanged
    {
        public readonly Action<Editor> UpdatePreview;
        private FileSystemWatcher _templateWatcher;
        private int _wordCount;

        public Preview()
        {
            InitializeComponent();
            Browser.Navigate(new Uri(UserTemplate.Load()));
            Loaded += OnLoaded;
            Unloaded += (sender, args) => _templateWatcher?.Dispose();
            Browser.Navigating += BrowserOnNavigating;
            Browser.PreviewKeyDown += BrowserPreviewKeyDown;
            UpdatePreview = Utility.Debounce<Editor>(editor => Dispatcher.InvokeAsync(() => Update(editor.Text)));
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            Task.Factory.StartNew(() =>
            {
                _templateWatcher = UserTemplate.TemplateFile.WatchFile(() => Dispatcher.Invoke(UpdateTemplate));

                // kill popups
                dynamic activeX = Browser.GetType().InvokeMember("ActiveXInstance",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, Browser, new object[] {});

                activeX.Silent = true;
            });
        }

        private void Update(string markdown)
        {
            if (markdown == null) return;
            try
            {
                markdown = Utility.RemoveYamlFrontMatter(markdown);
                var html = Markdown.ToHtml(markdown);
                UpdateBaseTag();
                var div = GetContentsDiv();
                div.innerHTML = ScrubHtml(html);
                WordCount = div.innerText.WordCount();
                FirePreviewUpdatedEvent();
            }
            catch (CommonMarkException e)
            {
                MessageBox.Show(e.ToString(), App.Title);
            }
        }

        private void FirePreviewUpdatedEvent()
        {
            try
            {
                dynamic doc = Browser.Document;
                var ev = doc.createEvent("event");
                ev.initEvent("previewUpdated", true, true);<commit_msg>rename func, add null check<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using CommonMark;
using HtmlAgilityPack;
using mshtml;
using MarkdownEdit.Models;
using MarkdownEdit.Properties;

namespace MarkdownEdit.Controls
{
    public partial class Preview : INotifyPropertyChanged
    {
        public readonly Action<Editor> UpdatePreview;
        private FileSystemWatcher _templateWatcher;
        private int _wordCount;

        public Preview()
        {
            InitializeComponent();
            Browser.Navigate(new Uri(UserTemplate.Load()));
            Loaded += OnLoaded;
            Unloaded += (sender, args) => _templateWatcher?.Dispose();
            Browser.Navigating += BrowserOnNavigating;
            Browser.PreviewKeyDown += BrowserPreviewKeyDown;
            UpdatePreview = Utility.Debounce<Editor>(editor => Dispatcher.InvokeAsync(() => Update(editor.Text)));
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            Task.Factory.StartNew(() =>
            {
                _templateWatcher = UserTemplate.TemplateFile.WatchFile(() => Dispatcher.Invoke(UpdateTemplate));

                // kill popups
                dynamic activeX = Browser.GetType().InvokeMember("ActiveXInstance",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, Browser, new object[] {});

                activeX.Silent = true;
            });
        }

        private void Update(string markdown)
        {
            if (markdown == null) return;
            try
            {
                markdown = Utility.RemoveYamlFrontMatter(markdown);
                var html = Markdown.ToHtml(markdown);
                UpdateBaseTag();
                var div = GetContentsDiv();
                div.innerHTML = ScrubHtml(html);
                WordCount = div.innerText.WordCount();
                EmitFirePreviewUpdatedEvent();
            }
            catch (CommonMarkException e)
            {
                MessageBox.Show(e.ToString(), App.Title);
            }
        }

        private void EmitFirePreviewUpdatedEvent()
        {
            try
            {
                dynamic doc = Browser.Document;
                var ev = doc.createEvent("event");
                if (ev == null) return;
                ev.initEvent("previewUpdated", true, true);
{'fraction_non_alphanumeric': 0.09121122599704579, 'fraction_numerical': 0.0, 'mean_word_length': 2.077840909090909, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5616903', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1272, 'n_words': 281}