I'm trying to create a Scribunto module which, among other things, can create a section header in its output.
If the returned string contains, for example, == Hello World ==
, the resulting page correctly displays the section, and even includes the section in the TOC. However, the section has no editsection link.
To some extent, this is understandable; the section doesn't actually exist in the page's source. But I would like to be able to put an edit link to where the content of the section is coming from. I have tried two different versions of a buildHeader
function:
-- version 1:
function p.buildHeader(level, title, page)
local open = '<span class="mw-editsection-bracket">[</span>'
local close = '<span class="mw-editsection-bracket">]</span>'
local link = '<a href="/w/index.php?title='..p.urlsafe(page)..'&action=edit" title="Edit section: '..title..'">edit</a>'
local edit = '<span class="mw-editsection">'..open..link..close..'</span>'
local text = '<span id="'..p.urlsafe(title)..'" class="mw-headline">'..title..'</span>'
return '<h'..level..'>'..title..edit..'</h'..level..'>'
end
-- version 2:
function p.buildHeader(level, title, page)
local result = mw.html.create('h'..level)
result:tag('span')
:attr({id=p.urlsafe(title), class='mw-headline'})
:wikitext(title)
:done()
:tag('span')
:attr('class', 'mw-editsection'):tag('span')
:attr('class', 'mw-editsection-bracket')
:wikitext('[')
:done()
:tag('a')
:attr({href='/w/index.php?title='..p.urlsafe(page)..'&action=edit', title='Edit section: '..title})
:wikitext('edit')
:done()
:tag('span')
:attr('class', 'mw-editsection-bracket')
:wikitext(']')
:allDone()
return tostring(result)
end
In both cases, the HTML of the anchor tag was escaped (eg, <span class="mw-editsection">...<a href="..." title="...">edit</a></span>
), and the whole mw-editsection span was included in the TOC text.
Is there any way for me to get my arbitrary edit link in there, or do I have to live with the editsection-less Scribunto sections?
My working solution (but not my preferred solution) is to insert the link with JavaScript. The buildHeader
function becomes:
function p.buildHeader(level, title, page)
local result = mw.html.create('h'..level)
result:attr('data-source', page):wikitext(title)
return tostring(result)
end
Then, in MediaWiki:Common.js, I add:
$('h1[data-source],h2[data-source],h3[data-source],h4[data-source],h5[data-source],h6[data-source]').append(function() {
var source = $(this).data('source'),
title = $(this).text(),
$editsection = $('<span>').attr('class', 'mw-editsection'),
$open = $('<span>').attr('class', 'mw-editsection-bracket').text('['),
$close = $('<span>').attr('class', 'mw-editsection-bracket').text(']'),
$link = $('<a>').attr('title', 'Edit section: ' + title)
.attr('href', '/w/index.php?title=' + source + '&action=edit')
.text('edit');
$editsection.append($open).append($link).append($close);
return $editsection;
});