public static TableOfContentsItem GetPreviousTopic(IHaveTopics topic, TopicViewModel topicViewModel) { var tocItem = topic as TableOfContentsItem; if (tocItem != null && topicViewModel?.FlatTopics != null) { var index = topicViewModel.FlatTopics.IndexOf(tocItem); return(index > 0 ? topicViewModel.FlatTopics[index - 1] : null); } return(null); }
public static TableOfContentsItem GetNextTopic(IHaveTopics topic, TopicViewModel topicViewModel) { var tocItem = topic as TableOfContentsItem; if (tocItem != null && topicViewModel?.FlatTopics != null) { var index = topicViewModel.FlatTopics.IndexOf(tocItem); return(index < topicViewModel.FlatTopics.Count - 1 ? topicViewModel.FlatTopics[index + 1] : null); } return(null); }
public static List <TableOfContentsItem> BuildTocFromDynamicToc(dynamic toc, IHaveTopics parent, string selectedTopicTitle, out List <TableOfContentsItem> flatTopicList) { var rootTopics = new List <TableOfContentsItem>(); flatTopicList = new List <TableOfContentsItem>(); if (toc == null) { return(rootTopics); } try { if (toc.topics != null) { AddTopics(toc.topics, rootTopics, parent, parent, selectedTopicTitle, flatTopicList); } } catch { // ignored } return(rootTopics); }
public static bool LinkMatchesTopic(string link, IHaveTopics topic) // This method also exists in topic.js as linkMatchesTopic() and should be kept in sync with this method. { if (link == null) { return(false); } if (topic == null) { return(false); } if (string.Compare(GetNormalizedName(topic.Slug), link, StringComparison.OrdinalIgnoreCase) == 0) { return(true); } var normalizedName = GetNormalizedName(link); if (string.Compare(GetNormalizedName(topic.Title), normalizedName, StringComparison.OrdinalIgnoreCase) == 0) { return(true); } var normalizedLink = GetNormalizedName(topic.Link); if (normalizedLink == normalizedName) { return(true); } var normalizedLinkParts = normalizedLink.Split('.'); if (normalizedLinkParts.Length > 0 && string.Compare(normalizedLinkParts[0], normalizedName, StringComparison.OrdinalIgnoreCase) == 0) { return(true); } return(false); }
public static bool SlugMatchesTopic(string slug, IHaveTopics topic, bool ignoreCase = false) // This method also exists in topic.js as slugMatchesTopic() and should be kept in sync with this method. { // This is a more discriminating version of LinkMatchesTopic() if (slug == null) { return(false); } if (topic == null) { return(false); } var topicSlug = topic.Slug; if (topicSlug == null) { return(false); } if (ignoreCase) { slug = slug.ToLowerInvariant(); topicSlug = topicSlug.ToLowerInvariant(); } while (slug.Length > 0 && slug.StartsWith('/')) { slug = slug.Substring(1); } while (topicSlug.Length > 0 && topicSlug.StartsWith('/')) { topicSlug = topicSlug.Substring(1); } return(slug == topicSlug); }
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); } } }
public static List <TableOfContentsItem> BuildTocFromJson(string tocJson, IHaveTopics parent, string selectedTopicTitle, out List <TableOfContentsItem> flatTopicList) { dynamic toc = GetDynamicTocFromJson(tocJson); return(BuildTocFromDynamicToc(toc, parent, selectedTopicTitle, out flatTopicList)); }
public TableOfContentsItem(IHaveTopics parent, IHaveTopics root) { Parent = parent; Root = root; }