Help:Extension:Discourse

PD Note: When you edit this page, you agree to release your contribution under the CC0. See Public Domain Help Pages for more info. PD

The Discourse extension provides a Lua library with which to interact with an external Discourse forum. Users of the extension are expected to already be familiar with writing templates and modules.

The extension provides the following two functions that expose the basic functionality for fetching any data that is available via the Discourse API:

  • mw.ext.discourse.getData( site, urlPath ) — Get data from a Discourse site.
    • @param string site - Site short name (as defined by the wiki system administrator).
    • @param string urlPath - The URL path of a JSON endpoint. See the Discourse documentation for full details.
    • @return table - Whatever is returned by the Discourse API, or error information.
  • mw.ext.discourse.getBaseUrl( site ) — Get the base URL of a given Discourse site. Useful for constructing links in templates etc.
    • @param string site - Site name.
    • @return table - With 'result' key.

The mw.ext.discourse library also provides two 'example' functions that can be used as-is or serve as the basis for a wiki's own formatting etc.

An example of a Module:Discourse that uses these could look something like the following:

local discourse = require( 'mw.ext.discourse' )

function news( frame )
    return discourse.news( frame )
end

function events( frame )
    return discourse.events( frame )
end

return {
    news = function( frame ) return news( frame ) end;
    events = function( frame ) return events( frame ) end;
}

This could then be used within a template with the following wikitext:

<!-- List all recent news tagged with 'site-feedback' -->
{{#invoke: discourse | news | tags=site-feedback }}

The details of the news() and events() functions can be viewed in the discourse.lua source file.