Thank you, this worked perfectly after a few minor code fixes and after prepending # to the identifier.

For the convenience of those, who, like me, don't really know any Lua, I attach the fixed code.

local chapter_level = 2
local toc_level = 3
local headings = {}
local current_chapter = nil

local function collect_headings (head)
if head.level == chapter_level then
local id = head.identifier
current_chapter = {
chapter = id,
toc = {},
}
headings[id] = current_chapter
elseif head.level == toc_level then
if current_chapter then
local toc = current_chapter.toc
toc[#toc+1] = head
end
end
return nil
end

local function build_toc (heads)
local toc = {}
for _,head in ipairs(heads) do
local entry = {
pandoc.Plain{
pandoc.Link(
head.content:clone(), -- text
'#' .. head.identifier, -- target
"", -- empty title
pandoc.Attr(
"", -- empty identifier
{'local-toc-link'} -- class
)
)
}
}
toc[#toc+1] = entry
end
return pandoc.Div(
{ pandoc.BulletList(toc) },
pandoc.Attr( "", {'local-toc'} )
)
end


local function insert_toc (head)
if head.level == chapter_level then
local id = head.identifier
if headings[id] then
local toc = build_toc(
headings[id].toc
)
return {head,toc}
end
end
return nil
end

return {
{ Header = collect_headings },
{ Header = insert_toc },
}


On Friday, 31 January 2020 21:31:31 UTC+1, BP wrote:
I believe a change has been made so that Lua filters now traverse the document in linear order. If this is the case in the latest version of Pandoc it should be doable with a two-pass filter: first a pass which collects the headings and then a pass which inserts the TsoC. If you want hierarchical section numbering you probably also need to do a first pandoc run which inserts the numbering and then a second pandoc run with the filter to insert the TsoC.  Something like this:

(Warning 1: Untested code!
  Warning 2: Make sure not to overwrite any existing file!
  Warning 3: Assumes that all chapters are heading level 2 — change the chapter_level and toc_level variables to match!
   Warning 4: Assumes that each section/chapter has a unique identifier!
)

local chapter_level = 2
local toc_level = 3
local headings = {}
local current_chapter = nil

local function collect_headings (head)
  if head.level == chapter_level then
    local id = head.identifier
    current_chapter = {
      chapter = id
      toc = {}
    }
    headings[id] = current_chapter
  elseif head.level = toc_level then
    if current_chapter then
      local toc = current_chapter.toc
      toc[#toc+1] = head
    end
  end
  return nil
end

local function build_toc (heads)
  local toc = {}
  for _,head in ipairs(heads) do
    local entry = {
      pandoc.Plain{
        pandoc.Link(
          head.content:clone(), -- text
          head.identifier, -- target
          "", -- empty title
          pandoc.Attr(
            "", -- empty identifier
            {'local-toc-link'} -- class
          )
        )
      }
    }
    toc[#toc+1] = entry
  end
  return pandoc.Div(
    { pandoc.BulletList(toc) },
    pandoc.Attr( "", {'local-toc'} )
  )
end


local function insert_toc (head)
  if head.level = chapter_level then
    local id = head.identifier
    if headings[id] then
      local toc = build_toc(
        headings[id].toc
      )
      return {head,toc}
    end
  end
  return nil
end

return {
  { Header = collect_headings },
  { Header = insert_toc },
}


Den fre 31 jan. 2020 19:27Szabolcs Horvát <szho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
Hello everyone,

I am converting a Markdown file to HTML.

I can insert a table of contents at the beginning using the --toc option.

Is it possible to insert additional tables of contents under each level-two or level-three heading?

I am looking to create something similar to the organization of this page: https://igraph.org/c/doc/igraph-Visitors.html#idm209455316208

Szabolcs

--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/3d838861-862c-4b15-8d91-a9b37d671736%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/2cf07390-db35-422e-887a-bfde62a12a63%40googlegroups.com.