JSFiddle - React, Tailwind, and code Playground

by dariusk

HTML

<div id="out"></div>

JavaScript

var wikipediaHTMLResult = function (data) {
    var readData = $('<div class="top">' + data.parse.text['*'] +'</div>');
    //console.log(readData.html());
    
    //console.log(readData.html());

    // handle redirects
    var redirect = readData.find('.redirectMsg a').text();
    console.log('re',redirect, redirect.length);
    if (redirect != '') {
        callWikipediaAPI(redirect);
        return;
    }
    // handle disambiguation
    var disambig = readData.find('#disambigbox').length;
    if (disambig) {
        console.log('DISAMBIG', readData.html());
        callWikipediaAPI(redirect);
        return;
    }

    var first = readData.children('p').has('a[title]');
    first = first.eq(Math.floor(Math.random()*Math.min(first.length),5));
    console.log(first.html())
    first = first.html().match(/.*?title=.*?\.(\s|\n|$|)/) ? first.html().match(/.*?title=.*?\.(\s|\n|$|)/)[0] : '';
    console.log(first);
    var links = $('<p>'+first+'</p>').find('a[title]').not('[href*=":"]').not('[href*="wiktionary"]').not('[href*="wikimedia"]').not('[class="external"]').not('[class="new"]');
    console.log(links);
    // if we still can't find a valid link, find any old valid link
    if (links.length === 0) {
     links  = readData.children('p').find('a[title]').not('[href*=":"]').not('[href*="wiktionary"]').not('[href*="wikimedia"]').not('[class="external"]').not('[class="new"]');
    }
    var link = links[Math.floor(Math.random()*links.length)];
    console.log(link)    
    var title = link.href.match(/wiki\/(.*)$/)[1].replace(/\#.*/,'');

    $('#out').append('<p>' + $('<p>'+first+'</p>').text() + '</p>');
    console.log('calling', title);
    callWikipediaAPI(title);
};

function callWikipediaAPI(wikipediaPage) {
    wikipediaPage = decodeURIComponent(wikipediaPage);
    console.log('calling', wikipediaPage);
    // http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse
   ...