Returning a value from omniJS

I’m mainly testing omniJS in OmniGraffle, but a quick spin of the omniJS in OO5 looks promising - fast and functioning well in the parts that I have looked at.

My main question (also for OG7 but perhaps particularly for OO5 scripting) is what the best way might be of returning a value (for example a Markdown or JSON version of all or some part of an outline) from omniJS, for example to a calling routine in JXA.

The TaskPaper JS Context has a helpful solution to this - it adds a sibling command document.evaluate(JSString, dctOptions) to the AppleScript/JXA dictionary.

In exchange for some JS Context source code, and an optional dictionary containing key value pairs to pass named values in with, it returns to JXA/AppleScript any kind of JS value returned by the JS Context script - Strings, numbers, Dictionaries, Lists etc.

An example of the kind of value which it would be good to capture from an OO outline (I personally use OO mainly when I need an outline with columns) is a nested JSON structure like this which can be parsed to an outline in any other JS context:

To paste into and run in the OmniJS console in Scripts > Show Console, to generate a JSON version of the active outline in OO5 Test

// show :: a -> String
const show = x => JSON.stringify(x, null, 2);

// jsoOutline :: OO.item -> Node {text:String, nest:[Node]}
const jsoOutline = item => ({
    text : item.topic,
    nest : item.children.map(jsoOutline)
});

show(jsoOutline(
    document.outline.rootItem
));

Generating something like:

{
  "text": "",
  "nest": [
    {
      "text": "Class Notes: CS229",
      "nest": [
        {
          "text": "Sunt autem fuci sine aculeo",
          "nest": [
            {
              "text": "Velut inperfectae apes novissimaeque a fessis aut iam emeritis inchoatae serotinus fetus et quasi servitia verarum apium, quam ob rem imperant iis primosque expellunt in opera",
              "nest": []
            },
            {
              "text": "Certe quo maior eorum fuit multitudo, hoc maior fit et examinum proventus. cum mella coeperunt maturescere, abigunt eos multaeque singulos adgressae trucidant.",
              "nest": [
                {
                  "text": "Regias imperatoribus futuris in ima parte alvi exstruunt amplas, magnificas, separatas, tuberculo eminentes",
                  "nest": []
                },
                {
                  "text": "Quod si exprimatur, non gignuntur. sexangulae omnes cellae a singulorum pedum opere.",
                  "nest": [
                    {
                      "text": "Nihil horum stato tempore, sed rapiunt diebus serenis munia. melle uno alterove summum die cellas replent.",
                      "nest": []
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "text": "Venit hoc ex aëre et maxime siderum exortu",
          "nest": [
            {
              "text": "Praecipueque ipso sirio expendescente",
              "nest": [
                {
                  "text": "Nec omnino prius vergiliarum exortu",
                  "nest": []
                }
              ]
            },
            {
              "text": "Sublucanis temporibus",
              "nest": [
                {
                  "text": "Itaque tum prima aurora folia arborum melle roscida inveniuntur ac, si qui matutino sub diu fuere",
                  "nest": []
                },
                {
                  "text": "Unctas liquore vestes capillumque concretum sentiunt",
                  "nest": []
                },
                {
                  "text": "Sive ille est caeli sudor sive quaedam siderum saliva sive purgantis se aëris sucus",
                  "nest": []
                },
                {
                  "text": "Utinamque esset purus ac liquidus et suae naturae",
                  "nest": []
                },
                {
                  "text": "Qualis defluit primo!",
                  "nest": []
                }
              ]
            },
            {
              "text": "Nunc vero e tanta cadens altitudine multumque",
              "nest": [
                {
                  "text": "Dum venit, sordescens et obvio terrae halitu infectus, praeterea e fronde ac pabulis potus et in utriculos congestus apium",
                  "nest": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

The question is, how do we best harvest this if we want to ‘export’ or make further use of it ?