Jump to content

Module:Auto compact TOC/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by HouseBlaster (talk | contribs) at 01:36, 10 September 2023 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local horizontal = require('Module:List').horizontal

local function make_TOC_item(anchor, link_text)
	link_text = link_text or anchor
	return ("[[#%s|%s]]"):format(anchor, link_text)
end

local Array_mt = { __index = table }
local function Array()
	return setmetatable({}, Array_mt)
end

function p.make_TOC(frame)
	local content = mw.title.getCurrentTitle():getContent()
	
	if not content then
		error "The current page has no content"
	end
	
	local letters = Array()
	-- Find uppermost headers containing a single ASCII letter.
	for letter in content:gmatch "%f[^\n]==%s*(%a)%s*==%f[^=]" do
		letter = letter:upper()
		letters:insert(make_TOC_item(letter))
	end
	
	
	return   horizontal(letters) .. horizontal(rest) 
end

return p