Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ Code copied from `AutoIdentifierExtension`, then added some code and options.
* `aClass`: `string? : default null`

Class attribute for `a` element.

* `CurrentUrl`: `string? : default null`

Current Url for toc items to work on subpages like `/help#anker`

* `MaxLevel`: `int : default 6`

Maximum heading level to include in the table of content. e.g. set to 2 to only include # and ## headings.



Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/HeadingInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void renderHtmlMixed(HtmlRenderer renderer, TocOptions options)
renderer.Write("<li");
if (options.liClass is not null)
renderer.Write($" class='{options.liClass}'");
renderer.Write($"><a href='#{Id}'");
renderer.Write($"><a href='{options.CurrentUrl}#{Id}'");
if (options.aClass is not null)
renderer.Write($" class='{options.aClass}'");
renderer.Write($">{Content}</a></li>");
Expand Down Expand Up @@ -70,7 +70,7 @@ void renderHtmlLint(HtmlRenderer renderer, TocOptions options)

if (!item.IsLocator)
{
renderer.Write($"<a href='#{item.Id}'");
renderer.Write($"<a href='{options.CurrentUrl}#{item.Id}'");
if (options.aClass is not null)
renderer.Write($" class='{options.aClass}'");
renderer.Write($">{item.Content}</a>");
Expand Down
16 changes: 15 additions & 1 deletion src/TocOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public TocOptions()
/// Classes for ContainerTag, work on ContainerTag is <strong> not null</strong>.
/// </summary>
public string? ContainerClass { get; set; }
/// <summary>
/// Current Url for toc items to work on subpages like /help.<br/>
/// </summary>
public string? CurrentUrl { get; set; }
#endregion
#region toc tag
/// <summary>
Expand Down Expand Up @@ -102,6 +106,12 @@ public TocOptions()
/// </summary>
public string? TitleClass { get; set; }
#endregion
/// <summary>
/// Maximum heading level to include in the table of content <em>(default 6)</em>.<br/>
/// e.g. set to 2 to only include # and ## headings.
/// </summary>
public int MaxLevel { get; set; } = 6;

#region toc items
/// <summary>
/// Class names for <strong>ul</strong> emement.<br/>
Expand All @@ -124,7 +134,11 @@ public TocOptions()
#endregion

internal void AddHeading(HeadingInfo info)
=> Headings.Append(HeadingInfos.FromHeading(info));
{
if (info.Level > MaxLevel)
return;
Headings.Append(HeadingInfos.FromHeading(info));
}

}
}