Example #1
0
 private dynamic BuildNodeShape(SitemapNode node)
 {
     var childShapes = node.Children.Values.Select(BuildNodeShape).ToList();
     return Shape.Sitemap_Node(Title: node.Title, Url: node.Url, Children: childShapes);
 }
        public SitemapNode GetSitemapRoot()
        {
            // Create dictionary indexed by routes
            var routeSettings = GetRoutes().ToDictionary(
                k => k.Slug,
                v => v,
                StringComparer.OrdinalIgnoreCase);

            var sitemapRoot = _cacheManager.Get("WebAdvanced.Sitemap.Root", ctx => {
                ctx.Monitor(_signals.When("WebAdvanced.Sitemap.Refresh"));

                var sitemap = new SitemapNode("Root", null);

                foreach (var provider in _routeProviders.OrderByDescending(p => p.Priority)) {
                    var validRoutes = provider.GetDisplayRoutes()
                        .Where(r => _routeFilters.All(filter => filter.AllowUrl(r.Url)))
                        .AsEnumerable();

                    foreach (var item in validRoutes) {
                        var alias = item.UrlAlias ?? item.Url;
                        var slugs = alias.Split('/').ToArray();
                        var routeSetting = routeSettings.ContainsKey(slugs[0]) ? routeSettings[slugs[0]] : null;

                        // Only add this to the sitemap if the route settings exist and accept it
                        if (routeSetting == null || !routeSetting.Active || slugs.Count() > routeSetting.DisplayLevels)
                            continue;

                        int i = 0;
                        SitemapNode currentNode = sitemap;
                        while (i < slugs.Length) {
                            var isLeaf = i == slugs.Length - 1;
                            if (!currentNode.Children.ContainsKey(slugs[i])) {
                                string name = isLeaf ? item.Title : slugs[i].SlugToTitle();
                                string url = isLeaf ? item.Url : null;
                                currentNode.Children.Add(slugs[i], new SitemapNode(name, url));
                            }
                            else if (isLeaf) { // Only replace existing items if the current is a leaf
                                currentNode.Children[slugs[i]].Url = item.Url;
                                // Keep current title if the over-riding one is empty  when a custom route is over-riding a content route
                                if (!string.IsNullOrWhiteSpace(item.Title)) {
                                    currentNode.Children[slugs[i]].Title = item.Title;
                                }
                            }
                            currentNode = currentNode.Children[slugs[i]];
                            i++;
                        }
                    }
                }

                return sitemap;
            });

            return sitemapRoot;
        }
        public SitemapNode GetSitemapRoot()
        {
            var activeTypes = GetActiveDisplayContentTypes();

            // Create dictionary indexed by routes
            var routeSettings = GetRoutes().ToDictionary(
                k => k.Slug,
                v => v);

            var sitemapRoot = _cacheManager.Get("WebAdvanced.Sitemap.Root", ctx => {
                ctx.Monitor(_signals.When("WebAdvanced.Sitemap.Refresh"));

                var sitemap = new SitemapNode("Root", null);

                foreach (var provider in _routeProviders) {
                    var validRoutes = provider.GetDisplayRoutes()
                        .Where(r => _routeFilters.All(filter => filter.AllowUrl(r.Url)))
                        .AsEnumerable();

                    foreach (var item in validRoutes) {

                        var slugs = item.Url.Split('/').ToArray();
                        var routeSetting = routeSettings.ContainsKey(slugs[0]) ? routeSettings[slugs[0]] : null;

                        // Only add this to the sitemap if the route settings exist and accept it
                        if (routeSetting == null || !routeSetting.Active || slugs.Count() > routeSetting.DisplayLevels)
                            continue;

                        int i = 0;
                        SitemapNode currentNode = sitemap;
                        while (i < slugs.Length) {
                            if (!currentNode.Children.ContainsKey(slugs[i])) {
                                var isLeaf = i == slugs.Length - 1;
                                string name = isLeaf ? item.Title : slugs[i].SlugToTitle();
                                string url = isLeaf ? item.Url : null;
                                currentNode.Children.Add(slugs[i], new SitemapNode(name, url));
                            }
                            currentNode = currentNode.Children[slugs[i]];
                            i++;
                        }
                    }
                }

                return sitemap;
            });

            return sitemapRoot;
        }