Elad Ratson
Elad Ratson

Reputation: 724

MediaWiki template to fetch all the sections from a given {{PAGETITLE}} and display them in a markup ordered list

I am an admin user on a dedicated MediaWiki installation with no access to the server, therefore I can not install extensions.

I am trying to create a template called {{DynamicPageSections|page_title}} where, when used in any given page, it will generate a markup ordered list of all the sections contained in the specific page specified in page_title, with a result similar to this:

* [[page_title#section_1|#section_1]]
** [[page_title#section_1_1|#section_1]]
** [[page_title#section_1_2|#section_1_2]]
* [[page_title#section_2|#section_2]]
** [[page_title#section_2_1|#section_2_1]]
and so forth...

Where, of course, each item on the list is named just like the section on the desired page and links to the anchor of that specific section.

I realized i can create a dedicated JS library at Wiki:my_js.js and then import it using importScript('MediaWiki:my_js.js'); added to the MediaWiki:Common.js.

I also figured out that I can use MediaWiki API to fetch all the sections in a given page in XML format using: 'https://arcticsea.miraheze.org/w/api.php?format=xml&action=parse&prop=sections&page=' + page_title;

What I can't manage to do is how to convert the fetched XML into a well structured markup and then have it delivered to a separately constructed template.

I tried to write a separate function at the end of my_js.js like this:

function getSectionsFor(title){
    // the code for parsing the XML and rewriting it in MediaWiki markup
}

$(function () {
    var result = document.getElementById('dynamicSections');
    result.innerHTML = getSectionsFor('page_title');
}());

And then on my template I put:

<div id="dynamicSections"></div>

but no matter what I do in my principal getSectionsFor(title) function, I always get an empty string wherever I use the template.

Would appreciate help from anyone who is familiar with MediaWiki and the possibilities of executing dedicated JS for same source templating.

Upvotes: 0

Views: 51

Answers (0)

Related Questions