private string CleanMarkdown(string markdown)
        {
            var fixedMarkdown = markdown.Replace(" & ", " & ");

            Regex spanRegex              = new Regex(SpanRegexPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Regex tableRegex             = new Regex(TableRegexPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Regex divRegex               = new Regex(DivRegexPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Regex badHeadingSpacingRegex = new Regex(BadHeadingSpacingRegexPattern, RegexOptions.Multiline | RegexOptions.IgnoreCase);

            MatchCollection tableMatches = tableRegex.Matches(markdown);

            foreach (Match match in tableMatches)
            {
                string tableMarkdown = this.CreateMarkdownForTable(match.Groups[0].Value);
                markdown = markdown.Replace(match.Groups[0].Value, tableMarkdown);
            }

            MatchCollection spanMatches = spanRegex.Matches(markdown);

            while (spanMatches.Count > 0)
            {
                foreach (Match match in spanMatches)
                {
                    if (match.Groups[0].Value.StartsWith("<span class=\"tag\">"))
                    {
                        // Span with a tag of class is an indicator that it is supposed to be formatted as code
                        string codeChar    = match.Groups[1].Value.Contains("\n") ? "```" : "`";
                        string replacement = codeChar + match.Groups[1].Value + codeChar;
                        markdown = markdown.Replace(match.Groups[0].Value, replacement);
                    }
                    else
                    {
                        // Other span elements should just be replaced with their inner content
                        markdown = markdown.Replace(match.Groups[0].Value, match.Groups[1].Value);
                    }
                }
                spanMatches = spanRegex.Matches(markdown);
            }

            MatchCollection divMatches = divRegex.Matches(markdown);

            while (divMatches.Count > 0)
            {
                foreach (Match match in divMatches)
                {
                    // Replace all divs with their content
                    markdown = markdown.Replace(match.Groups[0].Value, match.Groups[1].Value);
                }
                divMatches = divRegex.Matches(markdown);
            }

            MatchCollection badHeadingSpacingMatches = badHeadingSpacingRegex.Matches(markdown);

            foreach (Match match in badHeadingSpacingMatches)
            {
                markdown = markdown.Replace(match.Groups[0].Value, match.Groups[1].Value + match.Groups[2].Value);
            }

            return(markdown);
        }
        private string ConvertWikiToMarkdown(string wiki, int implementationGuideId, int templateId, string property, int?constraintId = null)
        {
            var    converter = new Html2Markdown.Converter(this.markdown);
            string markdown  = wiki;
            string cleanWiki = wiki
                               .Trim()
                               .Replace(" [1..0]", " [[]1..0]")
                               .Replace(" [0..1]", " [[]0..1]")
                               .Replace(" [0..*]", " [[]0..*]")
                               .Replace(" [1..1]", " [[]1..1]")
                               .Replace(" [1..*]", " [[]1..*]")
                               .Replace(" [urn:", " [[]urn:");

            if (string.IsNullOrEmpty(cleanWiki))
            {
                return(string.Empty);
            }

            try
            {
                var html = this.ConvertWikiToHtml(cleanWiki);
                html = this.CleanHtml(html);

                if (string.IsNullOrEmpty(html))
                {
                    throw new Exception("Error converting WIKI content to HTML");
                }

                markdown = converter.Convert(html);
                markdown = this.CleanMarkdown(markdown);
            }
            catch
            {
                this.WriteVerbose("Could not convert from WIKI to HTML due to syntax error. Converting original content directly to MarkDown.");
                markdown = converter.Convert(wiki);
            }

            if (markdown != wiki)
            {
                this.logs.Add(new MigrateMarkdownLog()
                {
                    TemplateId   = templateId,
                    ConstraintId = constraintId,
                    Property     = property,
                    Original     = wiki,
                    New          = markdown
                });
            }

            return(markdown);
        }
        public string ConvertHtmlToMarkdown(string html, int implementationGuideId, string property, int?sectionId = null)
        {
            if (string.IsNullOrEmpty(html))
            {
                return(html);
            }

            string cleanHtml = html
                               .Replace("<div>", "")
                               .Replace("</div>", "")
                               .Replace("<br>", "")
                               .Replace("<br />", "")
                               .Replace("<br/>", "");

            cleanHtml = this.CleanHtml(cleanHtml);

            var    converter = new Html2Markdown.Converter(this.markdown);
            string markdown;

            try
            {
                markdown = converter.Convert(cleanHtml);
            }
            catch (Exception ex)
            {
                this.WriteVerbose("Error converting to html: " + ex.Message);
                this.WriteVerbose(cleanHtml);
                return(html);
            }

            var cleanMarkdown = this.CleanMarkdown(markdown);

            if (html != cleanMarkdown)
            {
                this.logs.Add(new MigrateMarkdownLog()
                {
                    ImplementationGuideId = implementationGuideId,
                    SectionId             = sectionId,
                    Property = property,
                    Original = html,
                    New      = cleanMarkdown
                });
            }

            return(!string.IsNullOrEmpty(markdown) ? cleanMarkdown : html);
        }