public static string GetToCHtml(List <TableOfContentsItem> toc, TableOfContentsItem selectedTopic) { var sb = new StringBuilder(); AddTocItems(sb, toc, 0, selectedTopic); return(sb.ToString()); }
//private static void CheckTocItems(IEnumerable<TableOfContentsItem> topics, string selectedLink) //{ // var tableOfContentsItems = topics as TableOfContentsItem[] ?? topics.ToArray(); // foreach (var topic in tableOfContentsItems) // { // if (TopicHelper.LinkMatchesTopic(selectedLink, topic)) // { // var parentTableOfContentsItem = topic.Parent as TableOfContentsItem; // if (parentTableOfContentsItem != null) // EnsureExpanded(parentTableOfContentsItem); // } // CheckTocItems(topic.Topics, selectedLink); // } //} public static void EnsureExpanded(TableOfContentsItem item) { if (item == null) { return; } item.Expanded = true; var parentTableOfContentsItem = item.Parent as TableOfContentsItem; if (parentTableOfContentsItem != null) { EnsureExpanded(parentTableOfContentsItem); } }
private static void AddTopics(IEnumerable <dynamic> topics, ICollection <TableOfContentsItem> parentTopics, IHaveTopics parent, IHaveTopics root, string selectedTopicTitle, List <TableOfContentsItem> flatTopicList) { foreach (var topic in topics) { var newTopic = new TableOfContentsItem(parent, root) { Title = topic.title ?? "Unknown topic" }; if (topic.link != null) { newTopic.Link = topic.link; } if (topic.isExpanded != null) { newTopic.Expanded = topic.isExpanded; } if (topic.keywords != null) { newTopic.KeywordsRaw = topic.keywords; } if (topic.settings != null) { newTopic.SettingsDynamic = topic.settings; } if (topic.type != null) { newTopic.Type = topic.type; } if (topic.slug != null) { newTopic.Slug = topic.slug; } if (topic.seeAlso != null) { var seeAlsos = ((string)topic.seeAlso).Split('\n'); // Multiple see-also links separated by new-line foreach (var seeAlso in seeAlsos) { var seeAlsoParts = seeAlso.Split('|'); // If title and link are different, they are separated by | if (seeAlsoParts.Length > 1) { newTopic.SeeAlso.Add(new SeeAlsoTopic { Title = seeAlsoParts[0], Link = seeAlsoParts[1] }); } else if (seeAlsoParts.Length > 0) { newTopic.SeeAlso.Add(new SeeAlsoTopic { Title = seeAlsoParts[0], Link = seeAlsoParts[0] }); } } } parentTopics.Add(newTopic); flatTopicList.Add(newTopic); //if (selectedTopicTitle != null && TopicHelper.LinkMatchesTopic(selectedTopicTitle, newTopic)) //{ // if (root is IHaveSelectedTopic selectedTopicParent) // selectedTopicParent.SelectedTopic = newTopic; // EnsureExpanded(newTopic); //} if (topic.topics != null) { AddTopics(topic.topics, newTopic.Topics, newTopic, root, selectedTopicTitle, flatTopicList); } } }
private static void AddTocItems(StringBuilder sb, IEnumerable <TableOfContentsItem> topics, int indentLevel, TableOfContentsItem selectedTopic, TableOfContentsItem parentTopic = null) { var ulClasses = "topic-list topic-list-level" + indentLevel; if (parentTopic != null) { ulClasses += parentTopic.Expanded ? " topic-expanded" : " topic-collapsed"; } sb.Append("<ul class=\"" + ulClasses + "\">"); foreach (var topic in topics) { var className = "topic-link topic-level-" + indentLevel; if (topic == selectedTopic) { className += " selected-topic"; } sb.Append("<li class=\"" + className + "\">"); if (topic.Topics.Count > 0) { sb.Append($"<a href=\"{topic.SlugSafe}\" data-slug=\"{topic.Slug}\" data-link=\"{topic.Link}\">{topic.Title}</a>"); var keywords = topic.Keywords; if (!string.IsNullOrEmpty(keywords)) { sb.Append($"<span style=\"display: none;\">{topic.Keywords}</span>"); } sb.Append("<span class=\"caret " + (topic.Expanded ? "caret-expanded" : "caret-collapsed") + "\"><svg xmlns=\"http://www.w3.org/2000/svg\" focusable=\"false\" viewBox=\"0 0 24 24\"><path fill=\"black\" stroke=\"white\" d=\"M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z\"></path></svg></span>"); AddTocItems(sb, topic.Topics, indentLevel + 1, selectedTopic, topic); } else { sb.Append($"<a href=\"{topic.SlugSafe}\" data-slug=\"{topic.Slug}\" data-link=\"{topic.Link}\">{topic.Title}</a>"); var keywords = topic.Keywords; if (!string.IsNullOrEmpty(keywords)) { sb.Append("<span style=\"display: none;\">" + topic.Keywords + "</span>"); } } sb.Append("</li>"); } sb.Append("</ul>"); }