Asciidoctor: Include TOC if not included

Created on 5 Apr 2018  路  3Comments  路  Source: asciidoctor/asciidoctor

My document in split into several documents.

All sub documents are included into main document. All sub documents have :toc:

The main document also have ':toc:'

I want the sub document :toc: to not work when included in main document.

Main Document Heading

:toc:
:toclevels: 5
:sectnums:
:sectnumlevels: 5

Sub Document Heading

ifndef::toc::[]
:toc:
:toclevels: 4
endif::[]

ifndef::sectnums::[]
:sectnums:
:sectnumlevels: 4
endif::[]

Can someone please confirm whether this is the best option or not? Or, what can be the better option?

Most helpful comment

to not work

I missed the "not" part.

You could also check the docname.

ifeval::["{docname}" == "main"]
toc::[]
endif::[]

But I like @elextr's solution of having a designated attribute set in the main document better.

All 3 comments

AsciiDoc does not have a concept of sub-documents. Include directives are preprocessors, which means they simply joins the source together. By the time processor receives it, it's as though you have one large source document. See #894 for a broader discussion about this.

One way to fake a subdocument is to put the document inside an AsciiDoc table cell. Then you can have a toc per include. (you could argue that this is one way AsciiDoc supports sub-documents)

A better approach would be to use the extension API to manipulate the AST to insert a TOC at strategic places. You could also use a custom converter or templates for this.

To make the "subdocument" toc __not__ work can't you just have an attribute you only set in the parent document, eg

in parent:

:no_child_tocs:
include::children[]

in child

ifndef::no_child_tocs[]
toc::[]
endif::[]

to not work

I missed the "not" part.

You could also check the docname.

ifeval::["{docname}" == "main"]
toc::[]
endif::[]

But I like @elextr's solution of having a designated attribute set in the main document better.

Was this page helpful?
0 / 5 - 0 ratings