Ejemplo n.º 1
0
        // Inspired by https://github.com/naokazuterada/MarkdownTOC
        // http://pandoc.org/MANUAL.html#extension-auto_identifiers
        // Auto-identifiers from Markdig project makes this easier
        public static string GenerateToc(this string html)
        {
            int startOfToc = html.IndexOf("<!-- MarkdownTOC -->");
            int endOfToc   = html.IndexOf("<!-- /MarkdownTOC -->");

            if ((startOfToc < 0) || (endOfToc < 0))
            {
                return(html);
            }

            startOfToc = startOfToc + 20;

            //HeadingItemCollection coll = new HeadingItemCollection();
            //coll.Add("Heading 1", 1);
            //coll.Add("Heading 2", 2);
            //coll.Add("Heading 3", 3);
            //coll.Add("Test 4", 3);
            //coll.Add("Test 5", 2);

            // Headings before MarkdownTOC tags will be ignored.  (You know, the one that says "Table of Contents" <G>)
            HeadingItemCollection coll = BuildHeadingCollection(html.Substring(endOfToc + 21));

            string insert = FormatToc(coll);

            string newHtml = html.Substring(0, startOfToc) + "\n" + insert + "\n" + html.Substring(endOfToc);

            return(newHtml);
        }
Ejemplo n.º 2
0
        private static HeadingItemCollection BuildHeadingCollection(string html)
        {
            HeadingItemCollection coll = new HeadingItemCollection();

            string[] lines = html.Split('\n');
            int      level = 1;
            string   text  = string.Empty;

            foreach (string str in lines)
            {
                level = 0;
                if (!string.IsNullOrEmpty(str))
                {
                    // there may be "better" ways, but since we are only looking for H1 .. H6, I'm hard coding
                    if (str.StartsWith("# "))
                    {
                        level = 1;
                    }
                    else if (str.StartsWith("## "))
                    {
                        level = 2;
                    }
                    else if (str.StartsWith("### "))
                    {
                        level = 3;
                    }
                    else if (str.StartsWith("#### "))
                    {
                        level = 4;
                    }
                    else if (str.StartsWith("##### "))
                    {
                        level = 5;
                    }
                    else if (str.StartsWith("###### "))
                    {
                        level = 6;
                    }
                    text = str.Substring(level + 1);

                    if (level > 0)
                    {
                        coll.Add(text, level);
                    }
                }
            }

            return(coll);
        }
Ejemplo n.º 3
0
        private static string FormatToc(HeadingItemCollection coll)
        {
            StringBuilder sb = new StringBuilder();

            int    indentLevel  = 0;
            string indentSpaces = string.Empty;

            foreach (HeadingItem hi in coll)
            {
                indentLevel  = hi.Level - 1;
                indentSpaces = new string(' ', indentLevel * 4);

                sb.AppendFormat("{0}- [{1}]\n", indentSpaces, hi.Text);
            }

            //sb.AppendLine("- Heading 1");
            //sb.AppendLine("  - Heading 2");

            return(sb.ToString());
        }