<p>You can convert your Markdown text to HTML using <a href="https://www.yiiframework.com/doc/api/2.0/yii-helpers-markdown" rel="nofollow noreferrer">yii\helpers\Markdown</a>, which is what the <code>|md</code> Twig filter hooks into:</p>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; function(\craft\elements\Entry $entry) {
    return [
        'body' =&gt; \yii\helpers\Markdown::process((string)$entry-&gt;yourTextFieldHandle),
    ];
},
</code></pre>

<p>If you want the equivalent of <code>|md('text', inlineOnly=true)</code> (where only inline elements are parsed – no <code>&lt;p&gt;</code> tags), call <code>processParagraph()</code> instead of <code>process()</code>.</p>

<p>If you want to parse the copy as <a href="https://github.github.com/gfm/" rel="nofollow noreferrer">GitHub Flavored Markdown</a>, pass <code>'gfm'</code> as a second argument to <code>process()</code>:</p>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; function(\craft\elements\Entry $entry) {
    return [
        'body' =&gt; \yii\helpers\Markdown::process((string)$entry-&gt;yourTextFieldHandle, 'gfm'),
    ];
},
</code></pre>

CC BY-SA 4.0
2019-08-21T23:18:32.897Z
Thanks so much, this worked perfectly. I got something close to this but was looking for helpers within Craft. Didn't think to use Yii helpers. Thanks again!
Moucky
CC BY-SA 4.0
2019-08-21T22:56:01.267Z
2019-08-21T22:56:01.267Z
Brandon Kelly
<p>I'm a little bit stuck using the Element API. I managed to get my JSON file going pretty easily which was great but I've reached a point where I want to include some formatted copy as it would be output using markdown in a .twig template, is this possible?</p>

<craft3><plugin-elementapi>
Element API - Markdown in JSON
# Element API - Markdown in JSON

(Asked by: username_0 on 2019-08-21)

I'm a little bit stuck using the Element API. I managed to get my JSON file going pretty easily which was great but I've reached a point where I want to include some formatted copy as it would be output using markdown in a .twig template, is this possible?

**Tags:** craft3, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2019-08-21. Score: 2 | Accepted answer

You can convert your Markdown text to HTML using yii\helpers\Markdown, which is what the |md Twig filter hooks into:
'transformer' => function(\craft\elements\Entry $entry) {
    return [
        'body' => \yii\helpers\Markdown::process((string)$entry->yourTextFieldHandle),
    ];
},

If you want the equivalent of |md('text', inlineOnly=true) (where only inline elements are parsed – no  tags), call processParagraph() instead of process().
If you want to parse the copy as GitHub Flavored Markdown, pass 'gfm' as a second argument to process():
'transformer' => function(\craft\elements\Entry $entry) {
    return [
        'body' => \yii\helpers\Markdown::process((string)$entry->yourTextFieldHandle, 'gfm'),
    ];
},

> **Comments:**
> **username_0**: Thanks so much, this worked perfectly. I got something close to this but was looking for helpers within Craft. Didn't think to use Yii helpers. Thanks again!  

---

