javascript - 如何将 tocify 与 asciidoctor 一起用于动态目录?

标签 javascript jquery tableofcontents asciidoctor jquery-tocify-js

默认情况下,asciidoctor创建一个静态目录。这非常酷,但对于长文档,如果目录可以更紧凑一些并突出显示读者当前在文档中的位置,那就太好了。

此时,tocify进来了。Tocify 是一个很好的 Javascript 解决方案,正好可以解决这个问题。

现在的问题是:如何将 tocify 与 asciidoctor 一起使用?

Asciidoctor issue 699在这里是相关的。

最佳答案

启用docinfo processing在你的 asciidoc 文件中,例如通过将以下选项传递给 asciidoctor:

-a toc=left -a docinfo=shared

在您的 asciidoc 文件旁边添加一个 docinfo.html 文件,内容如下:

<!-- Generate a nice TOC -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>
<!-- We do not need the tocify CSS because the asciidoc CSS already provides most of what we neeed -->

<style>
.tocify-header {
    font-style: italic;
}

.tocify-subheader {
    font-style: normal;
    font-size: 90%;
}

.tocify ul {
    margin: 0;
 }

.tocify-focus {
    color: #7a2518; 
    background-color: rgba(0, 0, 0, 0.1);
}

.tocify-focus > a {
    color: #7a2518; 
}
</style>

<script type="text/javascript">
    $(function () {
        // Add a new container for the tocify toc into the existing toc so we can re-use its
        // styling
        $("#toc").append("<div id='generated-toc'></div>");
        $("#generated-toc").tocify({
            extendPage: true,
            context: "#content",
            highlightOnScroll: true,
            hideEffect: "slideUp",
            // Use the IDs that asciidoc already provides so that TOC links and intra-document
            // links are the same. Anything else might confuse users when they create bookmarks.
            hashGenerator: function(text, element) {
                return $(element).attr("id");
            },
            // Smooth scrolling doesn't work properly if we use the asciidoc IDs
            smoothScroll: false,
            // Set to 'none' to use the tocify classes
            theme: "none",
            // Handle book (may contain h1) and article (only h2 deeper)
            selectors: $( "#content" ).has( "h1" ).size() > 0 ? "h1,h2,h3,h4,h5" : "h2,h3,h4,h5",
            ignoreSelector: ".discrete"
        });

        // Switch between static asciidoc toc and dynamic tocify toc based on browser size
        // This is set to match the media selectors in the asciidoc CSS
        // Without this, we keep the dynamic toc even if it is moved from the side to preamble
        // position which will cause odd scrolling behavior
        var handleTocOnResize = function() {
            if ($(document).width() < 768) {
                $("#generated-toc").hide();
                $(".sectlevel0").show();
                $(".sectlevel1").show();
            }
            else {
                $("#generated-toc").show();
                $(".sectlevel0").hide();
                $(".sectlevel1").hide();
            }
        }

        $(window).resize(handleTocOnResize);
        handleTocOnResize();
    });
</script>

关于javascript - 如何将 tocify 与 asciidoctor 一起用于动态目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481638/

相关文章:

php - Laravel 应用程序中的 Bootstrap 模式窗口未显示

jquery - 如何序列化 JSON Post 的 jQuery 对象数组?

markdown - 将 Rmd 中的目录和附属列表分离为 PDF 文件

javascript - jQuery Ajax 在 Android 平板电脑浏览器上失败

javascript - 如何使用我自己的所见即所得编辑器插入图像

javascript - 如何将 $(this) 传递给回调函数

javascript - 带音频的桌面捕获

jquery - 如何停止动画 "mid-stream"并反转它?

tableofcontents - 在代码中使用目录?

用于编号目录的 Javascript