/// <summary>
 /// Gets the internal url of a page based on the page title.
 /// </summary>
 /// <param name="id">The page id</param>
 /// <param name="title">The title of the page</param>
 /// <returns>An absolute path to the page.</returns>
 public virtual string GetInternalUrlForTitle(int id, string title)
 {
     if (_httpContext != null)
     {
         UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext);
         return(helper.Action("Index", "Wiki", new { id = id, title = PageViewModel.EncodePageTitle(title) }));
     }
     else
     {
         // This is really here as a fallback, for tests
         return(string.Format("/wiki/{0}/{1}", id, PageViewModel.EncodePageTitle(title)));
     }
 }
Beispiel #2
0
        public Item AddItemAtLevel(int level, string title)
        {
            // Generate a unique id for the A tag, using the title name
            // If the title already exists, add a guid after it
            string titleId = PageViewModel.EncodePageTitle(title);

            if (_titleIdLookup.Contains(titleId))
            {
                int uniqueSuffix = _titleIdLookup.Count(x => x.StartsWith(titleId)) + 1;
                titleId += "-" + uniqueSuffix;
            }
            _titleIdLookup.Add(titleId);

            Item item = new Item(title, titleId);

            if (level == _currentLevel)
            {
                if (_currentItem.Parent != null)
                {
                    _currentItem.Parent.AddChild(item);
                }
                else
                {
                    _currentItem.AddChild(item);
                }
            }
            else if (level > _currentLevel)
            {
                while (_currentLevel + 1 < level)
                {
                    // e.g. current level 1
                    // level of new item is 4
                    //
                    // Fix the levels by putting items to warn about missing levels in,
                    // and move the current level/item forward to that level.
                    string warningTitle = string.Format("(Missing level {0} header)", _currentLevel + 1);                     // zero based
                    Item   dummyItem    = new Item(warningTitle, Guid.NewGuid().ToString());
                    _currentItem.AddChild(dummyItem);
                    _currentItem = dummyItem;
                    _currentLevel++;
                }

                _currentItem.AddChild(item);
            }
            else if (level < _currentLevel)
            {
                // e.g. at level h4, and a h1 tag appears
                // h4.parent = h3
                // h3.parent = h2
                // h2.parent = h1
                // (ding, same level)
                Item parent = _currentItem.Parent;
                while (parent != null && parent.Parent != null && parent.Level > level)
                {
                    parent = parent.Parent;
                }

                if (parent != null && parent.Parent != null)
                {
                    parent.Parent.AddChild(item);
                }
            }

            _currentItem  = item;
            _currentLevel = level;

            return(item);
        }