In building the jQuery API browser, since the source of the API comes from the wiki pages over at docs.jquery.com, I had to handle some wiki parsing and convert it to HTML.

However, the more I browsed the API, the more I realised there were aspects of wiki parsing I had missed.

So I built a (fairly) fully fledged wiki parser in JavaScript.

UK EVENTAttend ffconf.org 2024

The conference for people who are passionate about the web. 8 amazing speakers with real human interaction and content you can't just read in a blog post or watch on a tiktok!

I've not run any serious performance tests, but as simple parsing goes it does the job.

There's two different modes you can use it - either straight function or extending the String object. Just change the boolean at the top of the script to flip between modes to suit your code.

Download wiki2html.js

Try out a live demo

Example usage

// include the script
var wikiContents = document.getElementById('wiki-content');
wikiContents.innerHTML = wikiContents.innerHTML.wiki2html();

There's also a simple check method in the script that tests if the content looks like wiki text:

// include the script
var wikiContents = document.getElementById('wiki-content');
if (wikiContents.innerHTML.iswiki()) {
  wikiContents.innerHTML = wikiContents.innerHTML.wiki2html();
}

Notes

  • The wiki conversion is based on Wikipedia's cheatsheet.
  • It doesn't support images since the cheatsheet references them from an internal database.

Any feedback or bugs, let me know.