Markdown (or MDX) from Plone REST API

I'd like to retrieve Markdown (or, preferably, MDX) content from Plone's REST API but I cannot seem to find documentation on the process.

(.json (.get requests "http://plone.dev.kvm1.fugazi.link/journal/test/demo" :headers PloneKit.HEADERS :auth PloneKit.AUTH))

Returns

 "text" {"content-type" "text/x-web-markdown"
         "data" "<h1>This is plain pre</h1>\n<p>Pre formatted</p>"
         "encoding" "utf-8"}

But the data is not in Markdown format. Is this possible?

plone.restapi currently does not support Markdown format. Plone might have a portal_transform to transform HTML to Markdown (not sure, you would need to check) and then you could write a custom endpoint (or enhance an existing endpoint) to return Markdown instead of JSON.

I would suggest starting simple and writing a custom endpoint specific to your needs. In the long run, we might include this feature in plone.restapi core. Though, this is not on the short/mid-term roadmap so far.

1 Like

as far as i get it, the "content-type" is set correct, but the data (structure) of this json return isn't markdown. its html (with newlines)

either content-type is "text/xhtml-blabla" and data is html or content-type is text/x-web-markdown and data is markdown.

but maybe i get it wrong ...

Is your content in Plone markdown or do you want to convert html to markdown?

PS: If you convert html to markdown and back to html, it will not look the same. Especially tables are problematic in markdown, so often I need to 'keep them in html'. Note: Markdown supports html, so markdown can contain html

Thank you. I'd like to store content that can be converted by the consumer. Plone has attractive data modeling capabilities that are more important to me than its internal rendering features.

My content can be plain text. I tried that with the "text" field (plain text is fine) but that is also converted to HTML (inserting <p> tags).

Is it possible to store text in Plone as text/plain while displaying line breaks? I could store Markdown content as text/plain and default plaintext rendering would be sufficient. I want to avoid HTML in my content and still render it in a human-readable format.

Plone mostly stores text as it is input. Exception: for security reasons, HTML structures deemed dangerous are by default filtered out -- this can be customized.

Views determine how the objects are presented -- e.g. whether text is treated as HTML or plain text. You can override existing views or define your own views.

Many things can be achieved with CSS, too. You can e.g. use CSS to keep newlines in a block.

A bit off topic, but my add-on GitHub - espenmn/medialog.markdown: Markdown widget for Plone 5 saves 'both html and markdown' (it converts markdown to html and saves it in another field). (a did this because I could not get the default plone conversion to do all I wanted).

I have not touched the code in a while, but I use the markdown editor 'all the time',

The REST API returns the HTML-Version of the text.

I'd expect the stored markdown version to be returned.

We send:

"text": {
  "content-type": "text/x-web-markdown",
  "data": "xyz **bold** normal",
  "encoding": "utf8"
}

We get:

"text": {
  "content-type": "text/x-web-markdown",
  "data": "<p>xyz <strong>bold</strong> normal</p>",
  "encoding": "utf8"
}

To reproduce it:

SITE_URL=https://6-classic.demo.plone.org
USER=manager:****   # <- set manager password here

cat <<EOF | tee config_markup.json
{
  "allowed_types": [
    {"title":"text/html","token": "text/html"},
    {"title": "text/x-web-markdown","token": "text/x-web-markdown"},
    {"title": "text/x-web-textile","token": "text/x-web-textile"}
  ],
  "default_type": {"title":"text/html","token": "text/html"},
  "markdown_extensions":[
    "markdown.extensions.fenced_code",
    "markdown.extensions.footnotes",
    "markdown.extensions.tables"
  ]
}
EOF

curl -i -X PATCH "${SITE_URL}/@controlpanels/markup" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    --data-binary "@my_document.json" \
    --user ${USER}

cat <<EOF | tee my_document.json
{
  "@type": "Document",
  "title": "MyDocument",
  "text": {
    "content-type": "text/x-web-markdown",
    "data": "xyz **bold** normal",
    "encoding": "utf8"
  }
}
EOF

ID=$(
    curl -s -X POST "${SITE_URL}" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    --data-binary "@my_document.json" \
    --user ${USER} \
    | jq -r '."@id"' \
)
echo $ID
curl -s -X GET "${ID}" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    --user ${USER} \
    | jq -r '."text"'
"text": {
  "content-type": "text/x-web-markdown",
  "data": "<p>xyz <strong>bold</strong> normal</p>",
  "encoding": "utf8"
}

If the response tells us content-type": "text/x-web-markdown and the data is HTML this is a bug. Please file an issue at Issues · plone/plone.restapi · GitHub

We better discuss this in the issue.

1 Like