craftsnippets/table-of-contents
Composer 安装命令:
composer require craftsnippets/table-of-contents
包简介
This plugin generates a table of contents from HTML headers in text.
README 文档
README
This plugin generates a table of contents from HTML headers in text. Anchor links in table direct to corresponding headers in text.
Table of contents can be nested - nesting level will be based on header level.
Here's screenshot of example table of contents generated by plugin:
Requirements
This plugin requires Craft CMS 3.0.0-beta.23 or later.
It also requires Anchors plugin, since it makes use of its internal functions.
Installation
You can install this plugin from the Plugin Store or with Composer.
To install plugin with composer, run following command:
composer require craftsnippets/table-of-contents
Then in the control panel go to settings, plugins - and click "install" next to "table of contents".
Usage
To create the table of contents, you need to pass HTML containing headers to craft.toc.getLinks function and output returned data using {% nav %} Twig tag.
Don't forget to also put your HTML through anhors filter provided by Anchors plugin - it will add id attribute to headers, so anchor links inside the table of contents have something to direct to.
Remember to use anchors filter after passing HTML to getLinks function. Otherwise, links in table of contents would contain escaped HTML content of links that were added to headers by Anchors plugin.
{% set text %}
some text with headers...
{% endset %}
{% set table = craft.toc.getLinks(text) %}
<ul>
{% nav link in table %}
<li>
<a href="{{ link.hash }}">{{ link.text }}</a>
{% ifchildren %}
<ul>
{% children %}
</ul>
{% endifchildren %}
</li>
{% endnav %}
</ul>
{{text|anchors}}
Don't forget to give each of your links bit of left margin to show their hierarchy.
li{
margin-left: 1rem;
}
Nested numeric list
To display numeric count before links within the table of contents, you can use bit of CSS. This will work also for nested lists.
ul {
counter-reset: section;
list-style-type: none;
}
li::before {
counter-increment: section;
content: counters(section, ".") " ";
}
Alternative header tags
By default, Table of contents plugin searches for h1, h2 and h3 tags. Just like in Anchors plugin, this can be overwritten by passing the second argument to getLinks function.
{% table = craft.toc.getLinks(text, 'header1,header2,header3') %}
Don't forget to do the same when using anchors filter.
non-ASCII characters mapping
The third parameter of getLinks() method can be used to change non-ASCII character used to generate link hashes, just like with the anchors plugin. Here's a quote from anchors plugin documentation:
The anchors filter will convert any non-ASCII characters to ASCII, using the current site’s language’s ASCII character mappings by default. If you are displaying content in a different language than the current site, use the language argument to override which ASCII character mappings should be used.
Here's the example usage:
{% set tableOfContents = craft.toc.getLinks(html, 'h1,h2,h3', entry.site.language) %}
Stripping tags from links text
If headers which are converted to links contain some html tags inside them, links within table of contents will contain these tags too. You can disable this by setting fourth parameter of getLinks() method to true.
{% set tableOfContents = craft.toc.getLinks(html, 'h1,h2,h3', null, true) %}
Smooth scrolling
You can achieve smooth scrolling effect with single CSS property.
html{
scroll-behavior: smooth;
}
Unfortunetly it does not wok on Safari or IE - see more on caniuse.
Same effect that works in all modern browsers can be achieved with this jQuery code:
$('.table-of-contents a').on('click', function(event) {
var hash = '#' + $(this).attr('href').split('#')[1]
var element = $(hash)
if (element.length) {
event.preventDefault();
history.pushState(hash, undefined, hash)
$('html, body').animate({scrollTop: element.offset().top}, 500)
}
});
window.addEventListener('popstate', function(e) {
if(e.state && e.state.startsWith('#') && $(e.state).length){
$('html, body').animate({scrollTop: $(e.state).offset().top}, 500)
}
});
$('html, body').on("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove", function(){
$('html, body').stop();
});
If a user start to scroll (using mouse scroll wheel) during an animation, scrolling will be canceled to avoid "fighting" with it.
Despite animation replacing click event, hash will still be appended to URL and browser back or forward buttons will work - thanks to use of JavaScript history API.
Icon made by Dave Gandy from www.flaticon.com.
Brought to you by Piotr Pogorzelski
craftsnippets/table-of-contents 适用场景与选型建议
craftsnippets/table-of-contents 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.26k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「table of contents」 「craftcms」 「craft cms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 craftsnippets/table-of-contents 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 craftsnippets/table-of-contents 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 craftsnippets/table-of-contents 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
Plug-ins for DataTables
A component that allows creating responsive HTML tables or lists from data object
GraphQL authentication for your headless Craft CMS applications.
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
Supercharged text field validation.
统计信息
- 总下载量: 4.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-30