Module:Section link: Difference between revisions

From NvWiki
Jump to navigation Jump to search
m 1 revision imported: Import modules used with Template:Infobox software
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{central|text=all talk subpages of [[Template talk:Section link]] and [[Module talk:Section link]] redirect here.}}
-- This module implements {{section link}}.
{{copied|from1=Template:Section_link|from_oldid1=760447925|to1=:incubator:Template:Wp/nod/sectionlink|to_diff1=4320681
require('strict');
|from2=Module:Section_link|from_oldid2=836236090|to2=:incubator:Module:Wp/nod/Section_link|to_diff2=4320680}}
{{User:MiszaBot/config
|archiveheader = {{talkarchivenav}}
|maxarchivesize = 90K
|counter = 1
|minthreadsleft = 3
|minthreadstoarchive = 1
|algo = old(730d)
|archive = Template talk:Section link/Archive %(counter)d
}}
{{Archives |age=730}}


==Bugs==
local checkType = require('libraryUtil').checkType
<code><nowiki>{{see section||Soviet Union|Armenian SSR (Armenia)|label2=Armenian SSR}}</nowiki></code> displays {{see section||Soviet Union|Armenian SSR (Armenia)|label2=Armenian SSR}}. There are at least two bugs here. The multiple "§§" is one, and the lack of functionality for {{para|label2}} is another. I'd appreciate if this could be fixed for use at [[World War II by country]]. Thanks! <span style="color:#AAA"><small>&#123;{u&#124;</small><span style="border-radius:9em;padding:0 5px;background:#088">[[User:Sdkb|<span style="color:#FFF">'''Sdkb'''</span>]]</span><small>}&#125;</small></span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 20:21, 4 July 2022 (UTC)


:Do we agree that the output '''should''' be § [[Template talk:Section link#Soviet%20Union|Soviet Union]]​ and § [[Template talk:Section link#Armenian%20SSR%20(Armenia)|Armenian SSR (Armenia)]]? – i.e. with a single section sign per link  — [[User:GhostInTheMachine|GhostInTheMachine]] <sup>[[User talk:GhostInTheMachine|talk to me]]</sup> 11:26, 10 October 2022 (UTC)
local p = {}
::I believe we do. The disambiguation should be fixed by {{para|label2}}, as well. <span style="color:#AAA"><small>&#123;{u&#124;</small><span style="border-radius:9em;padding:0 5px;background:#088">[[User:Sdkb|<span style="color:#FFF">'''Sdkb'''</span>]]</span><small>}&#125;</small></span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 14:21, 11 October 2022 (UTC)
:[[File:Pictogram voting wait.svg|14px|link=|alt=]] <small>'''Bumping thread'''.  <span style="border:3px outset;border-radius:8pt 0;padding:1px 5px;background:linear-gradient(6rad,#86c,#2b9)">[[User:Sdkb|<span style="color:#FFF;text-decoration:inherit;font:1em Lucida Sans">Sdkb</span>]]</span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 22:10, 28 May 2024 (UTC)</small><!-- [[User:DoNotArchiveUntil]] 22:10, 27 June 2024 (UTC) -->{{User:ClueBot III/DoNotArchiveUntil|1719526212}}


==Add a wikilink to the § symbol==
local function makeSectionLink(page, section, display)
is there anyway a wikilink could be added to the § symbol pointing to [[Section_sign]]. A lot of people don't know what that symbol means, myself included until very recently. [[User:Jake-jakubowski|<span style="color:#146c43;">Jake Jakubowski</span>]] [[User talk:Jake-jakubowski|<span style="color:#146c43;">(Talk)</span>]] 05:13, 14 June 2023 (UTC)
display = display or section
page = page or ''
-- MediaWiki doesn't allow these in 'page', so only need to do for 'section'
if type(section) == 'string' then
section = string.gsub(section, "{", "&#x7B;")
section = string.gsub(section, "}", "&#x7D;")
end
return string.format('[[%s#%s|%s]]', page, section, display)
end


:A wikilink wouldn't be appropriate I don't think, but we could maybe do hover text, like this: {{tqbm|{{tlx|tooltip|&sect;|Section}}}} which renders as: {{tqbm|{{tooltip|§|Section}}}} However, this isn't accessible on mobile and I'm not sure about screen readers. — [[User:W.andrea|W.andrea]] ([[User talk:W.andrea|talk]]) 14:26, 17 April 2025 (UTC)
local function normalizeTitle(title)
title = mw.ustring.gsub(mw.ustring.gsub(title, "'", ""), '"', '')
title = mw.ustring.gsub(title, "%b<>", "")
return mw.title.new(title).prefixedText
end


:A current ongoing debate at {{section link|Talk:Saint Valentine's Day Massacre|See also - List of organized crime killings in Illinois}} suggests to me that there would be merit in a tool tip for the § symbol. Currently the text displayed by {{tl|section link}} appears as a single link, but it might be feasible to have two or three links:
function p._main(page, sections, options, title)
:# to the article
-- Validate input.
:# to [[§]]
checkType('_main', 1, page, 'string', true)
:# to the section
checkType('_main', 3, options, 'table', true)
:Eg: [[Albert Einstein]] [[Section mark|§]] [[Albert Einstein#Scientific career|Scientific career]]
if sections == nil then
:(Perhaps with a more explanatory tool tip than "section mark", if possible.)
sections = {}
:This is contrary to [[WP:SEAOFBLUE]], but might be justified in contexts where the template is used. (Also see [https://en.wikipedia.org/wiki/Talk:Saint_Valentine%27s_Day_Massacre#c-Mitch_Ames-20250906035200-See_also_-_List_of_organized_crime_killings_in_Illinois] for a comment on "where the template should be used".) Removing the first link (to the article, [[Albert Einstein]]) would reduce the impact of SEAOFBLUE. [[User:Mitch Ames|Mitch Ames]] ([[User talk:Mitch Ames|talk]]) 04:53, 6 September 2025 (UTC)
elseif type(sections) == 'string' then
sections = {sections}
elseif type(sections) ~= 'table' then
error(string.format(
"type error in argument #2 to '_main' " ..
"(string, table or nil expected, got %s)",
type(sections)
), 2)
end
options = options or {}
title = title or mw.title.getCurrentTitle()


==Bug: any link without a section adds #Notes==
-- Deal with blank page names elegantly
None of the following have "Notes" as a template parameter. It must be coming from [[Module:Section link]]
if page and not page:find('%S') then
page = nil
options.nopage = true
end


* {{slink|Wikipedia:Miscellany for deletion/User:Adoring nanny/Essays/Lab Leak Likely (2nd nomination)}}
-- Make the link(s).
* {{slink|Wikipedia:Miscellany}}
local isShowingPage = not options.nopage
* {{slink|Miscellany}}
if #sections <= 1 then
local linkPage = page or ''
if options.permalink then -- when oldid provided
linkPage = 'Special:Permalink/' .. options.permalink; -- make Special:Permalink/oldid wikilink
end
local section = sections[1]; -- sections[1] must have a value
local display = '§&nbsp;' .. section
if isShowingPage then
page = page or title.prefixedText
if options.display and options.display ~= '' then
if normalizeTitle(options.display) == normalizeTitle(page) then
display = options.display .. ' ' .. display
else
error(string.format(
'Display title "%s" was ignored since it is ' ..
"not equivalent to the page's actual title",
options.display
), 0)
end
else
display = page .. ' ' .. display
end
end
return makeSectionLink(linkPage, section, display)
else
-- Multiple sections. First, make a list of the links to display.
local ret = {}
for i, section in ipairs(sections) do
ret[i] = makeSectionLink(page, section)
end


I played around with fixing this in [https://en.wikipedia.org/w/index.php?title=Module:Section_link/sandbox&diff=prev&oldid=1163701314 this edit], but it still leaves an errant " § " when testing it in [[Template:Section link/sandbox|the sandbox]]. Anyone want to take this to the finish line and submit a {{t|TPER}}? Thanks. –[[User:Novem Linguae|<span style="color:blue">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 04:47, 6 July 2023 (UTC)
-- Assemble the list of links into a string with mw.text.listToText.
:If the purpose of the {{tq|module [is to create] links to sections, nicely formatted with the "§" symbol instead of the default "#"}}, is there ever a legitimate reason for it to make up a section name that may or may not exist in the target article?  Is there ever a legitimate reason to omit the section name from a template named 'section link'?  I don't know of any such reasons so, for me, I would have the module emit a {{color|#d33|section name missing}} (or some such) error message (perhaps with an accompanying hidden category).
-- We use the default separator for mw.text.listToText, but a custom
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 13:21, 6 July 2023 (UTC)
-- conjunction. There is also a special case conjunction if we only
::That sounds good to me. –[[User:Novem Linguae|<span style="color:blue">'''Novem Linguae'''</span>]] <small>([[User talk:Novem Linguae|talk]])</small> 09:42, 13 July 2023 (UTC)
-- have two links.
:Using the sandbox and the example from the OP:
local conjunction
::<syntaxhighlight lang="wikitext" inline="1">{{Section link/sandbox|Miscellany}}</syntaxhighlight> → {{Section link/sandbox|Miscellany}}
if #sections == 2 then
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 15:54, 1 June 2024 (UTC)
conjunction = '&#8203; and '
else
conjunction = ', and '
end
ret = mw.text.listToText(ret, nil, conjunction)


==Permalink option==
-- Add the intro text.
This template could be enhanced by adding an optional parameter for linking to a permanent revision of the section on the page. <span style="border:3px outset;border-radius:8pt 0;padding:1px 5px;background:linear-gradient(6rad,#86c,#2b9)">[[User:Sdkb|<span style="color:#FFF;text-decoration:inherit;font:1em Lucida Sans">Sdkb</span>]]</span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 00:25, 17 March 2024 (UTC)
local intro = '§§&nbsp;'
if isShowingPage then
intro = (page or title.prefixedText) .. ' ' .. intro
end
ret = intro .. ret


:@[[User:Novem Linguae|Novem Linguae]] and @[[User:Trappist the monk|Trappist the monk]], I see you've both been active in editing this template recently. Since I don't know how to code Lua, wondering if you might be interested in helping with this?
return ret
:To flesh out the proposal a bit, I envision a new {{para|permalink}} parameter that would be used to provide a revision number, and if so provided the link would go to that revision. <span style="border:3px outset;border-radius:8pt 0;padding:1px 5px;background:linear-gradient(6rad,#86c,#2b9)">[[User:Sdkb|<span style="color:#FFF;text-decoration:inherit;font:1em Lucida Sans">Sdkb</span>]]</span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 02:34, 29 May 2024 (UTC)
end
:
end
:Using the sandbox: links to the first post in this discussion:
::<syntaxhighlight lang="wikitext" inline="1">[[Special:Permalink/1214107719#Permalink option]]</syntaxhighlight> → [[Special:Permalink/1214107719#Permalink option]]
::<syntaxhighlight lang="wikitext" inline="1">{{section link/sandbox|Template talk:Section link|Permalink option|permalink=1214107719}}</syntaxhighlight> → {{section link/sandbox|Template talk:Section link|Permalink option|permalink=1214107719}}
:Only supported for uses with one section parameter; with multiple section parameters {{para|permalink}} is ignored.
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 15:52, 1 June 2024 (UTC)
::Looks good to me! Thanks for coding; I'd say feel free to implement. <span style="border:3px outset;border-radius:8pt 0;padding:1px 5px;background:linear-gradient(6rad,#86c,#2b9)">[[User:Sdkb|<span style="color:#FFF;text-decoration:inherit;font:1em Lucida Sans">Sdkb</span>]]</span> <sup>[[User talk:Sdkb|'''talk''']]</sup> 05:16, 2 June 2024 (UTC)
:::Done.  I leave it to you to update the documentation.
:::—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 14:39, 2 June 2024 (UTC)


==Wikipiping==
function p.main(frame)
I rune into the necessity for wikipiping, e.g., [[Moscow|the capital of Russia]] (hypothetical example). Can it be implemented as a parameter, e.g., <nowiki>{{section link|Stars in fiction|Neutron stars|pipe=life on the surface of neutron stars}}</nowiki>- [[user:Altenmann|Altenmann]] [[user talk:Altenmann|>talk]] 18:47, 18 August 2024 (UTC)
local yesno = require('Module:Yesno')
:Most often, hypothetical examples are meaningless. Is your other example a real-life example, or just another hypothetical?
local args = require('Module:Arguments').getArgs(frame, {
:
wrappers = 'Template:Section link',
:As currently written, this template and its module do not support piping.  Likely, to do so would seem to conflict with the purpose of this template which is to make a pretty rendering of an unpretty wikilink where all parts of the wikilink are visible to the reader:
valueFunc = function (key, value)
::<syntaxhighlight lang="wikitext" inline="1">[[Stars in fiction#Neutron stars]]</syntaxhighlight> → [[Stars in fiction#Neutron stars]]
value = value:match('^%s*(.-)%s*$') -- Trim whitespace
::<syntaxhighlight lang="wikitext" inline="1">{{slink|Stars in fiction|Neutron stars}}</syntaxhighlight> → {{slink|Stars in fiction|Neutron stars}}
-- Allow blank first parameters, as the wikitext template does this.
:If you want a piped link, make a piped link:
if value ~= '' or key == 1 then
::<syntaxhighlight lang="wikitext" inline="1">[[Stars in fiction#Neutron stars|life on the surface of neutron stars]]</syntaxhighlight> → [[Stars in fiction#Neutron stars|life on the surface of neutron stars]]
return value
:But, such links might run afoul of [[WP:EASTEREGG]].
end
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 19:42, 18 August 2024 (UTC)
end
::It is noit hypothetiical, and that's exactly what did in [[Life origination beyond planets]] three times, see the text "[[Aphotic zone#Life in the aphotic zone|life with no sunlight]]" there and two more downtext. After second thought I nolonger see the advantage of using sectionlink template in such cases. Yes, I am aware of rotten Easter eggs, and I always fix them when I see them. --[[user:Altenmann|Altenmann]] [[user talk:Altenmann|>talk]] 23:32, 18 August 2024 (UTC)
})
:::I sometimes use the template to link to related sections in the same article. This means that I want to flag to readers they're staying in the same article (with the section symbol), and need to make sure the text fits within the sentence, often requiring piping. [[User:Femke|—Femke 🐦]] ([[User talk:Femke|talk]]) 07:32, 12 July 2025 (UTC)


==Template-protected edit request on 17 April 2025==
for k, v in pairs(args) do -- replace underscores in the positional parameter values
{{edit template-protected|Template:Section link|answered=yes}}
if 'number' == type(k) then
Is it possible to add an example usage in the template itself? I'm not super familiar with <syntaxhighlight lang=wikitext inline>{{#invoke}}</syntaxhighlight>, but I know in other templates you can use {{tag|noinclude|o}}, like <syntaxhighlight lang=wikitext>{{slink|{{{article|}}}<noinclude>Example article</noinclude>|{{{section|}}}<noinclude>Example section</noinclude>}}</syntaxhighlight> — [[User:W.andrea|W.andrea]] ([[User talk:W.andrea|talk]]) 14:20, 17 April 2025 (UTC) '''edited 17:27, 10 May'''
if not yesno (args['keep-underscores']) then -- unless |keep-underscores=yes
: {{not done}} There are plenty of example on the doc page, and I don't see a good reason to deviate from the default behavior of the template displaying itself with no parameters. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 20:13, 22 April 2025 (UTC)
args[k] = mw.uri.decode (v, 'WIKI'); -- percent-decode; replace underscores with space characters
::{{tqbm|I don't see a good reason to deviate from the default behavior of the template displaying itself with no parameters.}}
else
::People first learning about the template would be better served seeing an example of the template's intended usage rather than an error stemming from unintended usage. — [[User:W.andrea|W.andrea]] ([[User talk:W.andrea|talk]]) 17:24, 10 May 2025 (UTC)
args[k] = mw.uri.decode (v, 'PATH'); -- percent-decode; retain underscores
:::Cf. {{tlx|Nothanks-web}} which noincludes "Page" — [[User:W.andrea|W.andrea]] ([[User talk:W.andrea|talk]]) 17:58, 10 May 2025 (UTC)
end
end
end


=={Section link} suggests a nicer default wikilink behavior==
-- Sort the arguments.
{{edit template-protected|Template:Section link|answered=yes}}
local page
Changes to this template+module cannot accomplish this, but: Does the default wikilink ''really'' have to make ugly links like this?
local sections, options = {}, {}
:<syntaxhighlight lang="wikitext" inline>[[Albert Einstein#Scientific career]]</syntaxhighlight> &rarr; [[Albert Einstein#Scientific career]]
for k, v in pairs(args) do
if k == 1 then
-- Doing this in the loop because of a bug in [[Module:Arguments]]
-- when using pairs with deleted arguments.
page = mw.text.decode(v, true)
elseif type(k) == 'number' then
sections[k] = v
else
options[k] = v
end
end


... when it could instead make nicely formatted links like this template+module does?
options.nopage = yesno (options.nopage); -- make boolean
-- Extract section from page, if present
if page then
local p, s = page:match('^(.-)#(.*)$')
if p then page, sections[1] = p, s end
end


:<syntaxhighlight lang="wikitext" inline>{{section link|Albert Einstein#Scientific career}}</syntaxhighlight> &rarr; {{section link|Albert Einstein#Scientific career}}
-- Compress the sections array.
local function compressArray(t)
local nums, ret = {}, {}
for num in pairs(t) do
nums[#nums + 1] = num
end
table.sort(nums)
for i, num in ipairs(nums) do
ret[i] = t[num]
end
return ret
end
sections = compressArray(sections)


[[Help:Cheatsheet]] lists both ways to "Link to a section".
if not sections[1] then -- empty section parameter makes {{Section link}} meaningless
return ('<span style="color:#d33"><span style="font-family:monospace">{{[[Template:Section link|Section link]]}}</span>: required section parameter(s) missing</span>'); -- emit error message and abandon
end


I can't imagine where the link with the {{blue|Article title § Section title}} section-link format would NOT suit better than the {{blue|Article title#Section title}} format (though one might contemplate alternative glyphs and alternative spacings).-[[User:A876|A876]] ([[User talk:A876|talk]]) 23:18, 27 August 2025 (UTC)
return p._main(page, sections, options)
:{{not done}} – no '''complete and specific description''' of the request.  Generally such requests are in the form: change X to Y.
end
:
:Not clear what you mean by {{tq|Changes to this template+module cannot accomplish this}}.  Accomplish what, specifically?
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 23:31, 27 August 2025 (UTC)
::I regret making such a vague comment in this context. I withdraw it embarrassed.
::(I would delete this whole section<!--("{Section link} suggests a nicer default wikilink behavior")-->. If you would too, please do so.) (A new attempt follows.) -[[User:A876|A876]] ([[User talk:A876|talk]]) 18:09, 28 August 2025 (UTC)


==Make <nowiki>[[ ]]</nowiki> (wikilink) do the same as {section link|}!==
return p
{{edit template-protected|Template:Section link|answered=yes}}
Pre-apologies:
* I know I'm [mis]using {edit template-protected} to suggest changes to ''some other'' template. I do not request any edit here; I only hope for an opinion and maybe an idea of where to [re]post.
* I have held this idea for a while, and sadly I must let it out. Now.
* I ''would'' comment in the place where the code for <syntaxhighlight lang="wikitext" inline>[[ ]]</syntaxhighlight> is maintained, but <syntaxhighlight lang="wikitext" inline>[[ ]]</syntaxhighlight> is not a template. I don't know where to look. It might be a fundamental non-template hard-coded in [[MediaWiki]]. For lack of a better immediate place, I <s>boldly</s> rudely cram it in here to introduce it.
 
'''This template-and-module ({section link|})''' makes "nice" section links, like this:
 
:<syntaxhighlight lang="wikitext" inline>{{section link|Albert Einstein#Wormholes}}</syntaxhighlight> &rarr; {{section link|Albert Einstein#Wormholes}}
 
'''The standard [[ &nbsp; ]] (wikilink)''' makes the same target into a ugly (or less-nice) section link, like this:
:<syntaxhighlight lang="wikitext" inline>[[Albert Einstein#Wormholes]]</syntaxhighlight> &rarr; [[Albert Einstein#Wormholes]]
 
[[Help:Cheatsheet]] lists both ways to add a "Link to a section". If someone wants a "nice" link, they have to code for this template instead of using the default wikilink syntax.
 
Maybe it's time for '''[[ &nbsp; ]] (wikilink)''' to do the same thing (in the above case) that '''{section link|}''' does! Because:
* Editors who want to add a "nice" link-to-a-section (without pipe) won't have to add the code for his template.
* Every existing link-to-a-section (without pipe) would immediately become "nice".
* I cannot imagine where a link like {{blue|Article § Section}} would NOT suit better than a link like {{blue|Article#Section}} (though one might contemplate using an alternate glyph and/or different spacing). Wouldn't ''everyone'' prefer it? -[[User:A876|A876]] ([[User talk:A876|talk]]) 18:09, 28 August 2025 (UTC)
:{{not done}}.
:
:The <syntaxhighlight lang="wikitext" inline="1">[[ ]]</syntaxhighlight> markup is a MediaWiki primitive.  If you think that MediaWiki should render a formatted unpiped section link in a way that is different from the way that unpiped section links are currently rendered, you will need to take that up with MediaWiki.  I suppose that the place to do that might be at [[WP:PHAB|phabricator]]. Making that suggestion here will surely result in disappointment.
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 18:33, 28 August 2025 (UTC)
 
== Template-protected edit request on 2 September 2025: Don't render on the template page if it's going to show an error ==
{{edit template-protected|Template:Section link|answered=yes}}
 
Currently the template page shows:
{{tqb|1=<span style="color:#d33"><span style="font-family:monospace"><nowiki>{{Section link}}</nowiki></span>: required section parameter(s) missing</span>}}
 
This isn't a good example, so it would be better to just remove it using {{tag|includeonly}}, like {{tl|Multi-section link}} does.
 
[[Special:diff/1309175741|I put this change on the sandbox.]]
 
Previously, [[#Template-protected edit request on 17 April 2025|I requested to add a good example, which was declined]].
 
— [[User:W.andrea|W.andrea]] ([[User talk:W.andrea|talk]]) 18:38, 2 September 2025 (UTC)
 
: {{re|W.andrea}} {{done}}. [[User:Est. 2021|Est. 2021]] ([[User talk:Est. 2021|talk]]&nbsp;<b>&middot;</b>&#32;[[User:Est. 2021/contribs|contribs]]) 10:06, 7 September 2025 (UTC)
 
== Sections links in "See also" ==
 
Editors are invited to comment at {{section link|Talk:Saint Valentine's Day Massacre|See also - List of organized crime killings in Illinois}} on the relative merits of {{tl|section link}} vs piped links in the "See also" section of an article. [[User:Mitch Ames|Mitch Ames]] ([[User talk:Mitch Ames|talk]]) 11:42, 5 September 2025 (UTC)
 
== Custom conjunction word option ==
{{edit template-protected|Module:Section link|answered=y}}
I'd like a {{para|conjunction}} parameter, primarily for lists with the word "or". I have tested at the module sandbox with {{diff|Module:Section link/sandbox|prev|1315650082|this edit}}, which works as expected as can be seen in "Case 18: custom conjunction word" at {{section link|Template:Section link/testcases|Multiple sections}}. This permits constructions like {{xt|Article §§ Section 1​ or Section 2}}. <span title="Signature of Dan Leonard">[[User:Dan Leonard|Dan Leonard]] ([[User talk:Dan Leonard|talk]]&nbsp;• [[Special:Contributions/Dan Leonard|contribs]])</span> 21:49, 7 October 2025 (UTC)
:Not addressing the issue directly but I am curious: why this code:
::<syntaxhighlight lang="lua" inline="1">conjunction = '&#8203; and '</syntaxhighlight> – the original
::<syntaxhighlight lang="lua" inline="1">conjunction_string = '&#8203; ' .. conjunction_word .. ' '</syntaxhighlight> – the proposal
:What purpose does <syntaxhighlight lang="wikitext" inline="1">&#8203;</syntaxhighlight> ([[Zero-width space]]) serve?  Lines will break at the immediately adjacent 'space' character (<syntaxhighlight lang="wikitext" inline="1">&#32;</syntaxhighlight>) so why do we need the zero-width space?
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 22:24, 7 October 2025 (UTC)
::I can't see a good reason for this. It seems to have existed since the {{oldid|Module:Section link|637287647|first version of the module}}, so maybe [[User:Mr. Stradivarius|Mr. Stradivarius]] might remember? {{oldid|Template:Section link|452846230|The original template version}} used regular spaces ({{diff|Template:Section link|prev|622853255|and briefly, <syntaxhighlight inline lang="wikitext">&thinsp;</syntaxhighlight>}}) after the section signs, which might have had odd breaking behavior that the zero-width space was working around. Probably unnecessary though. <span title="Signature of Dan Leonard">[[User:Dan Leonard|Dan Leonard]] ([[User talk:Dan Leonard|talk]]&nbsp;• [[Special:Contributions/Dan Leonard|contribs]])</span> 23:06, 7 October 2025 (UTC)
:::{{Ping|Trappist the monk}} I {{diff|Module:Section link/sandbox|prev|1315761600|removed}} the zero-width space from the sandbox and see no difference in the line break behavior at [[Template:Section link/testcases|the testcases page]], running Chromium version 141.0.7390.55. <span title="Signature of Dan Leonard">[[User:Dan Leonard|Dan Leonard]] ([[User talk:Dan Leonard|talk]]&nbsp;• [[Special:Contributions/Dan Leonard|contribs]])</span> 14:09, 8 October 2025 (UTC)
:::{{ping|Dan Leonard|Trappist the monk}} The <code>&amp;#8203;</code> was in the [https://en.wikipedia.org/w/index.php?title=Template:Section_link&action=edit&oldid=631281028 version of the template] that I converted. It was added by [[User:Czarkoff]] [https://en.wikipedia.org/w/index.php?title=Template%3ASection_link&diff=next&oldid=587933224 here]. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr.&nbsp;Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪&nbsp;talk&nbsp;♪]]</sup> 14:27, 8 October 2025 (UTC)
:::<small>Pinging [[User:Trappist the monk|Trappist the monk]] again because I messed up the first one. — '''''[[User:Mr. Stradivarius|<span style="color: #194D00; font-family: Palatino, Times, serif">Mr.&nbsp;Stradivarius</span>]]''''' <sup>[[User talk:Mr. Stradivarius|♪&nbsp;talk&nbsp;♪]]</sup> 14:31, 8 October 2025 (UTC)</small>
 
{{not done for now}}—Feel free to reactivate when discussion is resolved. <span class="vcard"><span class="fn">[[User:Pigsonthewing|Andy Mabbett]]</span> (<span class="nickname">Pigsonthewing</span>); [[User talk:Pigsonthewing|Talk to Andy]]; [[Special:Contributions/Pigsonthewing|Andy's edits]]</span> 17:18, 8 December 2025 (UTC)

Latest revision as of 16:57, 26 December 2025

Template:Used in system Template:Module rating This module creates links to sections, nicely formatted with the "§" symbol instead of the default "#".

Usage

From wikitext

From wikitext, this module should be used via the template {{section link}}. Please see the template page for documentation.

From Lua

First, load the module:

local mSectionLink = require('Module:Section link')

You can then make section links via the _main function.

mSectionLink._main(page, sections, options, title)

Parameters:

  • page - the page name to link to. Defaults to the full page name of title, or the current title if that is not specified.
  • sections - an array of section names to link to, or a string to link to just one section name.
  • options - a table of options. Accepts the following fields:
    • nopage - set this to true to avoid displaying the base page name in links.
  • title - a default mw.title object to use instead of the current title. Intended for testing purposes.

All parameters are optional.

Examples

Lua code Wikitext code Result
Template:Code {{section link|Paris}} {{Section link}}: required section parameter(s) missing
Template:Code {{section link|Paris|Architecture}} Paris § Architecture
Template:Code {{section link|Paris|Architecture|Culture}} Paris §§ Architecture​ and Culture
Template:Code {{section link|Paris|Architecture|Culture|Sport}} Paris §§ Architecture, Culture, and Sport
Template:Code {{section link|Paris|Architecture|Culture|Sport|nopage=yes}} §§ Architecture, Culture, and Sport

See also


-- This module implements {{section link}}.
require('strict');

local checkType = require('libraryUtil').checkType

local p = {}

local function makeSectionLink(page, section, display)
	display = display or section
	page = page or ''
	-- MediaWiki doesn't allow these in 'page', so only need to do for 'section'
	if type(section) == 'string' then
		section = string.gsub(section, "{", "&#x7B;")
		section = string.gsub(section, "}", "&#x7D;")
	end
	return string.format('[[%s#%s|%s]]', page, section, display)
end

local function normalizeTitle(title)
	title = mw.ustring.gsub(mw.ustring.gsub(title, "'", ""), '"', '')
	title = mw.ustring.gsub(title, "%b<>", "")
	return mw.title.new(title).prefixedText
end

function p._main(page, sections, options, title)
	-- Validate input.
	checkType('_main', 1, page, 'string', true)
	checkType('_main', 3, options, 'table', true)
	if sections == nil then
		sections = {}
	elseif type(sections) == 'string' then
		sections = {sections}
	elseif type(sections) ~= 'table' then
		error(string.format(
			"type error in argument #2 to '_main' " ..
			"(string, table or nil expected, got %s)",
			type(sections)
		), 2)
	end
	options = options or {}
	title = title or mw.title.getCurrentTitle()

	-- Deal with blank page names elegantly
	if page and not page:find('%S') then
		page = nil
		options.nopage = true
	end

	-- Make the link(s).
	local isShowingPage = not options.nopage
	if #sections <= 1 then
		local linkPage = page or ''
		if options.permalink then												-- when oldid provided
			linkPage = 'Special:Permalink/' .. options.permalink;				-- make Special:Permalink/oldid wikilink
		end
		local section = sections[1];											-- sections[1] must have a value
		local display = '§&nbsp;' .. section
		if isShowingPage then
			page = page or title.prefixedText
			if options.display and options.display ~= '' then
				if normalizeTitle(options.display) == normalizeTitle(page) then
					display = options.display .. ' ' .. display
				else
					error(string.format(
						'Display title "%s" was ignored since it is ' ..
						"not equivalent to the page's actual title",
						options.display
					), 0)
				end
			else
				display = page .. ' ' .. display
			end
		end
		return makeSectionLink(linkPage, section, display)
	else
		-- Multiple sections. First, make a list of the links to display.
		local ret = {}
		for i, section in ipairs(sections) do
			ret[i] = makeSectionLink(page, section)
		end

		-- Assemble the list of links into a string with mw.text.listToText.
		-- We use the default separator for mw.text.listToText, but a custom
		-- conjunction. There is also a special case conjunction if we only
		-- have two links.
		local conjunction
		if #sections == 2 then
			conjunction = '&#8203; and '
		else
			conjunction = ', and '
		end
		ret = mw.text.listToText(ret, nil, conjunction)

		-- Add the intro text.
		local intro = '§§&nbsp;'
		if isShowingPage then
			intro = (page or title.prefixedText) .. ' ' .. intro
		end
		ret = intro .. ret

		return ret
	end
end

function p.main(frame)
	local yesno = require('Module:Yesno')
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Section link',
		valueFunc = function (key, value)
			value = value:match('^%s*(.-)%s*$') -- Trim whitespace
			-- Allow blank first parameters, as the wikitext template does this.
			if value ~= '' or key == 1 then
				return value
			end
		end
	})

	for k, v in pairs(args) do													-- replace underscores in the positional parameter values
		if 'number' == type(k) then
			if not yesno (args['keep-underscores']) then						-- unless |keep-underscores=yes
				args[k] = mw.uri.decode (v, 'WIKI');							-- percent-decode; replace underscores with space characters
			else
				args[k] = mw.uri.decode (v, 'PATH');							-- percent-decode; retain underscores
			end
		end
	end

	-- Sort the arguments.
	local page
	local sections, options = {}, {}
	for k, v in pairs(args) do
		if k == 1 then
			-- Doing this in the loop because of a bug in [[Module:Arguments]]
			-- when using pairs with deleted arguments.
			page = mw.text.decode(v, true)
		elseif type(k) == 'number' then
			sections[k] = v
		else
			options[k] = v
		end
	end

	options.nopage = yesno (options.nopage);									-- make boolean
	
	-- Extract section from page, if present
	if page then
		local p, s = page:match('^(.-)#(.*)$')
		if p then page, sections[1] = p, s end
	end

	-- Compress the sections array.
	local function compressArray(t)
		local nums, ret = {}, {}
		for num in pairs(t) do
			nums[#nums + 1] = num
		end
		table.sort(nums)
		for i, num in ipairs(nums) do
			ret[i] = t[num]
		end
		return ret
	end
	sections = compressArray(sections)

	if not sections[1] then														-- empty section parameter makes {{Section link}} meaningless
		return ('<span style="color:#d33"><span style="font-family:monospace">{{[[Template:Section link|Section link]]}}</span>: required section parameter(s) missing</span>');	-- emit error message and abandon
	end

	return p._main(page, sections, options)
end

return p