yum-slop/yummers.dev
source code for https://yummers.dev
git clone https://git.yummers.dev/yum-slop/yummers.dev
49b768a
master
1-- Replace an empty `::: {.article-toc}` block with the current article's 2-- table of contents. The block can appear anywhere in the article. 3 4local function escape_html ( text ) 5return text : gsub ( "&" , "&" ) 6: gsub ( "<" , "<" ) 7: gsub ( ">" , ">" ) 8: gsub ( '"' , """ ) 9: gsub ( "'" , "'" ) 10end 11 12local function make_toc ( entries ) 13if # entries == 0 then 14return nil 15end 16 17local lines ={ 18'<nav class="article-toc" aria-label="Table of contents">' , 19'<h2>Contents</h2>' , 20'<ul>' , 21} 22for _ , entry in ipairs ( entries ) do 23table . insert ( lines , string . format ( 24'<li class="toc-level-%d"><a href="#%s">%s</a></li>' , 25entry . level , escape_html ( entry . id ), escape_html ( entry . title ))) 26end 27table . insert ( lines , '</ul>' ) 28table . insert ( lines , '</nav>' ) 29return pandoc . RawBlock ( 'html' , table . concat ( lines , '\n' )) 30end 31 32function Pandoc ( doc ) 33local output =pandoc . List () 34local article =pandoc . List () 35 36local function flush_article () 37if # article == 0 then 38return 39end 40 41local entries =pandoc . List () 42for _ , block in ipairs ( article ) do 43if block . t == 'Header' and block . level == 2 then 44entries : insert ( { 45level =block . level , 46id =block . identifier , 47title =pandoc . utils . stringify ( block . content ), 48} ) 49end 50end 51 52for _ , block in ipairs ( article ) do 53if block . t == 'Div' and block . classes : includes ( 'article-toc' ) then 54local toc =make_toc ( entries ) 55if toc then 56output : insert ( toc ) 57end 58else 59output : insert ( block ) 60end 61end 62end 63 64for _ , block in ipairs ( doc . blocks ) do 65if block . t == 'Header' and block . level == 1 then 66flush_article () 67article =pandoc . List () 68end 69article : insert ( block ) 70end 71flush_article () 72 73doc . blocks =output 74return doc 75end