Title: Module:Section link - freem URL Source: https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link Published Time: Fri, 31 Jul 2026 09:44:31 GMT Markdown Content: _Documentation for this module may be created at [Module:Section link/doc](https://freemwiki.com/index.php?title=Module:Section\_link/doc&action=edit&redlink=1 "Module:Section link/doc (page does not exist)")_ [](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-1)-- This module implements {{[section link](https://freemwiki.com/wiki/Template:Section_link "Template:Section link")}}.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-2)require('strict');[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-3)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-4)local checkType = require('libraryUtil').checkType[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-5)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-6)local p = {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-7)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-8)local function makeSectionLink(page, section, display)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-9)display = display or section[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-10)page = page or ''[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-11)-- MediaWiki doesn't allow these in 'page', so only need to do for 'section'[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-12)if type(section) == 'string' then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-13)section = string.gsub(section, "{", "{")[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-14)section = string.gsub(section, "}", "}")[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-15)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-16)return string.format('[[%s#%s|%s]]', page, section, display)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-17)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-18)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-19)local function normalizeTitle(title)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-20)title = mw.ustring.gsub(mw.ustring.gsub(title, "'", ""), '"', '')[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-21)title = mw.ustring.gsub(title, "%b<>", "")[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-22)return mw.title.new(title).prefixedText[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-23)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-24)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-25)function p._main(page, sections, options, title)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-26)-- Validate input.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-27)checkType('_main', 1, page, 'string', true)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-28)checkType('_main', 3, options, 'table', true)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-29)if sections == nil then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-30)sections = {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-31)elseif type(sections) == 'string' then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-32)sections = {sections}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-33)elseif type(sections) ~= 'table' then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-34)error(string.format([](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-35)"type error in argument #2 to '_main' " ..[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-36)"(string, table or nil expected, got %s)",[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-37)type(sections)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-38)), 2)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-39)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-40)options = options or {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-41)title = title or mw.title.getCurrentTitle()[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-42)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-43)-- Deal with blank page names elegantly[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-44)if page and not page:find('%S') then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-45)page = nil[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-46)options.nopage = true[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-47)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-48)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-49)-- Make the link(s).[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-50)local isShowingPage = not options.nopage[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-51)if #sections <= 1 then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-52)local linkPage = page or ''[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-53)if options.permalink then-- when oldid provided[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-54)linkPage = 'Special:Permalink/' .. options.permalink;-- make Special:Permalink/oldid wikilink[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-55)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-56)local section = sections[1];-- sections[1] must have a value[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-57)local display = '§ ' .. section[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-58)if isShowingPage then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-59)page = page or title.prefixedText[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-60)if options.display and options.display ~= '' then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-61)if normalizeTitle(options.display) == normalizeTitle(page) then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-62)display = options.display .. ' ' .. display[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-63)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-64)error(string.format([](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-65)'Display title "%s" was ignored since it is ' ..[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-66)"not equivalent to the page's actual title",[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-67)options.display[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-68)), 0)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-69)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-70)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-71)display = page .. ' ' .. display[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-72)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-73)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-74)return makeSectionLink(linkPage, section, display)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-75)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-76)-- Multiple sections. First, make a list of the links to display.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-77)local ret = {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-78)for i, section in ipairs(sections) do[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-79)ret[i] = makeSectionLink(page, section)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-80)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-81)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-82)-- Assemble the list of links into a string with mw.text.listToText.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-83)-- We use the default separator for mw.text.listToText, but a custom[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-84)-- conjunction. There is also a special case conjunction if we only[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-85)-- have two links.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-86)local conjunction[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-87)if #sections == 2 then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-88)conjunction = '​ and '[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-89)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-90)conjunction = ', and '[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-91)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-92)ret = mw.text.listToText(ret, nil, conjunction)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-93)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-94)-- Add the intro text.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-95)local intro = '§§ '[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-96)if isShowingPage then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-97)intro = (page or title.prefixedText) .. ' ' .. intro[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-98)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-99)ret = intro .. ret[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-100)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-101)return ret[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-102)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-103)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-104)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-105)function p.main(frame)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-106)local yesno = require(['Module:Yesno'](https://freemwiki.com/wiki/Module:Yesno))[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-107)local args = require(['Module:Arguments'](https://freemwiki.com/wiki/Module:Arguments)).getArgs(frame, {[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-108)wrappers = 'Template:Section link',[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-109)valueFunc = function (key, value)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-110)value = value:match('^%s*(.-)%s*$') -- Trim whitespace[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-111)-- Allow blank first parameters, as the wikitext template does this.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-112)if value ~= '' or key == 1 then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-113)return value[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-114)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-115)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-116)})[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-117)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-118)for k, v in pairs(args) do-- replace underscores in the positional parameter values[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-119)if 'number' == type(k) then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-120)if not yesno (args['keep-underscores']) then-- unless |keep-underscores=yes[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-121)args[k] = mw.uri.decode (v, 'WIKI');-- percent-decode; replace underscores with space characters[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-122)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-123)args[k] = mw.uri.decode (v, 'PATH');-- percent-decode; retain underscores[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-124)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-125)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-126)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-127)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-128)-- Sort the arguments.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-129)local page[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-130)local sections, options = {}, {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-131)for k, v in pairs(args) do[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-132)if k == 1 then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-133)-- Doing this in the loop because of a bug in [[[Module:Arguments]]](https://freemwiki.com/wiki/Module:Arguments "Module:Arguments")[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-134)-- when using pairs with deleted arguments.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-135)page = mw.text.decode(v, true)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-136)elseif type(k) == 'number' then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-137)sections[k] = v[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-138)else[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-139)options[k] = v[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-140)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-141)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-142)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-143)options.nopage = yesno (options.nopage);-- make boolean[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-144)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-145)-- Extract section from page, if present[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-146)if page then[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-147)local p, s = page:match('^(.-)#(.*)$')[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-148)if p then page, sections[1] = p, s end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-149)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-150)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-151)-- Compress the sections array.[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-152)local function compressArray(t)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-153)local nums, ret = {}, {}[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-154)for num in pairs(t) do[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-155)nums[#nums + 1] = num[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-156)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-157)table.sort(nums)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-158)for i, num in ipairs(nums) do[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-159)ret[i] = t[num][](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-160)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-161)return ret[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-162)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-163)sections = compressArray(sections)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-164)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-165)if not sections[1] then-- empty section parameter makes {{[Section link](https://freemwiki.com/wiki/Template:Section_link "Template:Section link")}} meaningless[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-166)return ('{{[[Template:Section link|Section link]]}}: required section parameter(s) missing');-- emit error message and abandon[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-167)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-168)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-169)return p._main(page, sections, options)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-170)end[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-171)[](https://freemwiki.com/index.php?oldid=579850&title=Module%3ASection_link#L-172)return p