Improve TOC logic, reduce code duplication and add changelog entries

This commit is contained in:
daudix
2024-10-02 20:30:55 +03:00
parent a85b1ccfe6
commit 3080a2ca70
7 changed files with 83 additions and 80 deletions

View File

@ -7,10 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://codeberg.org/daudix/duckquill/compare/v5.3.2...main) ## [Unreleased](https://codeberg.org/daudix/duckquill/compare/v5.3.2...main)
### Added
- Add ability to enable inline table of contents in articles (#94 and #95).
- Add ability to make table of contents use numbered lists (#95).
- Add ability to open search by pressing the slash key.
### Changed ### Changed
- Allow opening search by pressing the slash key.
- Focus search after opening it. - Focus search after opening it.
- Move quick navigation buttons to the top on mobile.
- Return some `-webkit-` prefixes for wider compatibility. - Return some `-webkit-` prefixes for wider compatibility.
## [5.3.2](https://codeberg.org/daudix/duckquill/compare/v5.3.1...v5.3.2) ## [5.3.2](https://codeberg.org/daudix/duckquill/compare/v5.3.1...v5.3.2)

View File

@ -129,6 +129,21 @@ show_share_button = true
# Note: This will make your page significantly heavier. # Note: This will make your page significantly heavier.
# Instead, consider enabling it per page/section. # Instead, consider enabling it per page/section.
# katex = true # katex = true
#
# Whether to render table of contents on all pages.
# Will not be rendered if page doesn't have any headings.
# Can be set per page/section.
# toc = true
#
# Whether to render inline table of contents at the top of all pages,
# in addition to floating quick navigation buttons.
# Can be set per page/section.
# toc_inline = true
#
# Whether to use numbered (ordered) list for table of contents.
# Can be set per page/section.
# toc_ordered = true
#
# Display outlines around all elements for debugging purposes # Display outlines around all elements for debugging purposes
# debug_layout = true # debug_layout = true
@ -206,12 +221,3 @@ show_qr = true
# #
# Your GoatCounter username # Your GoatCounter username
user = "duckquill" user = "duckquill"
# Table of Contents (TOC) configs
[extra.toc]
# whether to render inline static TOC at the top of the articles or not
inline = true
# where to show the TOC as a pop-up button
popup = true
# Show numbered TOC
numbered = true

View File

@ -9,6 +9,8 @@ tags = ["Demo", "Test"]
[extra] [extra]
banner = "banner.webp" banner = "banner.webp"
toc = true toc = true
toc_inline = true
toc_ordered = true
trigger = "This page contains blackjack and hookers, and bad jokes such as this one." trigger = "This page contains blackjack and hookers, and bad jokes such as this one."
disclaimer = """ disclaimer = """
- All tricks in this page are performed by the lab boys, don't try this at home. - All tricks in this page are performed by the lab boys, don't try this at home.

View File

@ -180,6 +180,7 @@
overflow: auto; overflow: auto;
} }
ol,
ul { ul {
margin: 0; margin: 0;
padding-inline-start: 0.75rem; padding-inline-start: 0.75rem;

View File

@ -19,9 +19,21 @@
color: transparent; color: transparent;
user-select: none; user-select: none;
:root[dir*="rtl"] & {
transform: scaleX(-1);
&:active {
transform: scaleX(-1) var(--active);
}
}
&:hover { &:hover {
background-color: var(--accent-color); background-color: var(--accent-color);
} }
&:active {
transform: var(--active);
}
} }
a + a[href*="#fr-"], a + a[href*="#fr-"],
@ -32,10 +44,6 @@
&:hover { &:hover {
opacity: 1; opacity: 1;
} }
&:active {
transform: var(--active);
}
} }
li:target a[href*="#fr-"] { li:target a[href*="#fr-"] {

View File

@ -5,11 +5,11 @@
{%- set date_locale = macros_translate::translate(key="date_locale", default="en_US", language_strings=language_strings) -%} {%- set date_locale = macros_translate::translate(key="date_locale", default="en_US", language_strings=language_strings) -%}
{%- set rel_attributes = macros_rel_attributes::rel_attributes() | trim -%} {%- set rel_attributes = macros_rel_attributes::rel_attributes() | trim -%}
{% if config.extra.toc.numbered %} {%- if page.extra.toc_ordered or section.extra.toc_ordered or config.extra.toc_ordered -%}
{% set list_element = "ol" %} {%- set list_element = "ol" -%}
{% else %} {%- else -%}
{% set list_element = "ul" %} {%- set list_element = "ul" -%}
{% endif %} {%- endif -%}
<article> <article>
{%- if page.extra.banner -%} {%- if page.extra.banner -%}
@ -77,28 +77,28 @@
</div> </div>
<div id="buttons-container"> <div id="buttons-container">
{%- if page.extra.toc and config.extra.toc.popup -%} {%- if page.extra.toc and page.toc | length > 0 -%}
<details id="toc" class="closable"> <details id="toc" class="closable">
<summary title="{{ macros_translate::translate(key='table_of_contents', default='Table of Contents', language_strings=language_strings) }}"><i class="icon"></i></summary> <summary title="{{ macros_translate::translate(key='table_of_contents', default='Table of Contents', language_strings=language_strings) }}"><i class="icon"></i></summary>
<div id="toc-dropdown"> <div id="toc-dropdown">
<strong>{{ macros_translate::translate(key="table_of_contents", default="Table of Contents", language_strings=language_strings) }}</strong> <strong>{{ macros_translate::translate(key="table_of_contents", default="Table of Contents", language_strings=language_strings) }}</strong>
<div> <div>
<{{list_element}}> <{{ list_element }}>
{%- for h1 in page.toc -%} {%- for h1 in page.toc -%}
<li> <li>
<a href="{{ h1.permalink | safe }}">{{ h1.title }}</a> <a href="{{ h1.permalink | safe }}">{{ h1.title }}</a>
{%- if h1.children -%} {%- if h1.children -%}
<{{list_element}}> <{{ list_element }}>
{%- for h2 in h1.children -%} {%- for h2 in h1.children -%}
<li> <li>
<a href="{{ h2.permalink | safe }}">{{ h2.title }}</a> <a href="{{ h2.permalink | safe }}">{{ h2.title }}</a>
</li> </li>
{%- endfor -%} {%- endfor -%}
</{{list_element}}> </{{ list_element }}>
{%- endif -%} {%- endif -%}
</li> </li>
{%- endfor -%} {%- endfor -%}
</{{list_element}}> </{{ list_element }}>
</div> </div>
</div> </div>
</details> </details>
@ -114,26 +114,10 @@
{%- include "partials/statements.html" -%} {%- include "partials/statements.html" -%}
{%- if page.extra.toc_inline -%}
{%- if page.extra.toc and config.extra.toc.inline-%} {%- include "partials/toc.html" -%}
<h2>{{ macros_translate::translate(key="table_of_contents", default="Table of Contents", language_strings=language_strings) }}</h2> {%- elif config.extra.toc_inline -%}
{%- include "partials/toc.html" -%}
<{{list_element}} role="list">
{%- for h1 in page.toc -%}
<li>
<a href="{{ h1.permalink | safe }}">{{ h1.title }}</a>
{%- if h1.children -%}
<{{list_element}} role="list">
{%- for h2 in h1.children -%}
<li>
<a href="{{ h2.permalink | safe }}">{{ h2.title }}</a>
</li>
{%- endfor -%}
</{{list_element}}>
{%- endif -%}
</li>
{%- endfor -%}
</{{list_element}}>
{%- endif -%} {%- endif -%}
{{ page.content | safe }} {{ page.content | safe }}

View File

@ -1,37 +1,33 @@
{%- if page.extra.toc -%} {%- if page.extra.toc_ordered or section.extra.toc_ordered or config.extra.toc_ordered -%}
<h2>{{ macros_translate::translate(key="table_of_contents", default="Table of Contents", language_strings=language_strings) }}</h2> {%- set list_element = "ol" -%}
<ul> {%- else -%}
{%- for h1 in page.toc -%} {%- set list_element = "ul" -%}
<li> {%- endif -%}
<a href="{{ h1.permalink | safe }}">{{ h1.title }}</a>
{%- if h1.children -%} {%- if page.toc -%}
<ul> {%- set toc = page.toc -%}
{%- for h2 in h1.children -%} {%- elif section.toc -%}
<li> {%- set toc = section.toc -%}
<a href="{{ h2.permalink | safe }}">{{ h2.title }}</a> {%- endif -%}
</li>
{%- endfor -%} {%- if page.extra.toc or section.extra.toc -%}
</ul> {%- if toc | length > 0 -%}
{%- endif -%} <h2>{{ macros_translate::translate(key="toc", default="Table of Contents", language_strings=language_strings) }}</h2>
</li> <{{ list_element }}>
{%- endfor -%} {%- for h1 in toc -%}
</ul> <li>
{%- elif section.extra.toc -%} <a href="{{ h1.permalink | safe }}">{{ h1.title }}</a>
<h2>{{ macros_translate::translate(key="table_of_contents", default="Table of Contents", language_strings=language_strings) }}</h2> {%- if h1.children -%}
<ul> <{{ list_element }}>
{%- for h1 in section.toc -%} {%- for h2 in h1.children -%}
<li> <li>
<a href="{{ h1.permalink | safe }}">{{ h1.title }}</a> <a href="{{ h2.permalink | safe }}">{{ h2.title }}</a>
{%- if h1.children -%} </li>
<ul> {%- endfor -%}
{%- for h2 in h1.children -%} </{{ list_element }}>
<li> {%- endif -%}
<a href="{{ h2.permalink | safe }}">{{ h2.title }}</a> </li>
</li> {%- endfor -%}
{%- endfor -%} </{{ list_element }}>
</ul> {%- endif -%}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif -%} {%- endif -%}