How to use Parsoid to convert wikitext to html (instead of a full html page with extra info)

501 Views Asked by At

Both parsoid and parsoid-jsapi give you a .parse(... function to parse wikitext to html, but I'm having trouble getting a clean html string.

Say I want to parse This is [[it]] I do this:

var parsoid = require('parsoid-jsapi') || require('parsoid');
parsoid.parse('This is [[it]]', {}).then(function(data) {
    console.log(data.out)
})

But what I get from data.out is:

<!DOCTYPE html>
<html prefix="dc: http://purl.org/dc/terms/ mw: http://mediawiki.org/rdf/"><head prefix="mwr: http://en.wikipedia.org/wiki/Special:Redirect/"><meta charset="utf-8"/><meta property="mw:pageNamespace" content="0"/><meta property="isMainPage" content="true"/><meta property="mw:html:version" content="1.3.0"/><link rel="dc:isVersionOf" href="//en.wikipedia.org/wiki/Main%20Page"/><title></title><base href="//en.wikipedia.org/wiki/"/><link rel="stylesheet" href="//en.wikipedia.org/w/load.php?modules=mediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.skinning.content.parsoid%7Cmediawiki.skinning.interface%7Cskins.vector.styles%7Csite.styles%7Cext.cite.style%7Cmediawiki.page.gallery.styles&amp;only=styles&amp;skin=vector"/></head><body data-parsoid='{"dsr":[0,14,0,0]}' lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body mw-body-content mediawiki" dir="ltr"><p data-parsoid='{"dsr":[0,14,0,0]}'>This is <a rel="mw:WikiLink" href="./It" title="It" data-parsoid='{"stx":"simple","a":{"href":"./It"},"sa":{"href":"it"},"dsr":[8,14,2,2]}'>it</a></p></body></html>

With the only relevant part to me being:

<p data-parsoid='{"dsr":[0,14,0,0]}'>This is <a rel="mw:WikiLink" href="./It" title="It" data-parsoid='{"stx":"simple","a":{"href":"./It"},"sa":{"href":"it"},"dsr":[8,14,2,2]}'>it</a></p>

Or the wikitext to html instead of adding a head, body, etc. And even then it got lots of extra data that I don't need, what I really should be getting is something like:

<p>This is <a href="./It" title="It" >it</a></p>

As would be the case if I were using something like markdown with markdown-it for example.

Can parsoid generate a plain html string? And if so how?

0

There are 0 best solutions below