Parse entity_embed module markup I'm using Drupal 8.1.1 with Acquia's Lightning distribution. I need to be able to embed a media entity (a tweet, instagram post, youtube video, image, etc.) and then captured the resultant HTML output in a hook. However, $node->body->value just gives me a weird markup like this:

&lt;drupal-entity data-align=\"none\" data-caption=\"\" data-embed-button=\"media_library\" data-entity-embed-display=\"entity_reference:entity_reference_entity_view\" data-entity-embed-settings=\"{&amp;quot;view_mode&amp;quot;:&amp;quot;media_library&amp;quot;}\" data-entity-id=\"21\" data-entity-type=\"media\" data-entity-uuid=\"ea90ea95-6b7e-4e44-a1a6-4e7baabe3cb7\"&gt;&lt;/drupal-entity&gt;\r\n"


And rendering the entity through the renderer service gives me the entire output of the node, rather than just the html equivalent of the aforementioned markup.

Is there any way to parse this output and get the actual HTML of the embedded entity?
The body contains tokens set when you press an embed button in ckeditor.

This raw body content is processed by this filter:

http://cgit.drupalcode.org/entity_embed/tree/src/Plugin/Filter/EntityEmbedFilter.php

 public function process($text, $langcode) {
    $result = new FilterProcessResult($text);

    if (strpos($text, 'data-entity-type') !== FALSE &amp;&amp; (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) {
      $dom = Html::load($text);
      $xpath = new \DOMXPath($dom);

      foreach ($xpath-&gt;query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id) and (@data-entity-embed-display or @data-view-mode)]') as $node) {
        /** @var \DOMElement $node */
....


You would have to build a view mode in ui with only the body field or apply the filter in code.