Deactivate specific tag (or plugin) on AMP version

Hi,
I am currently building a optimized AMP version of my blogposts. I figured out the routing with help of another thread on this board but struggling with a plugin (inserting inline styles, amp doesn’t like that).
Is there a way to override / deactivate a tag behaviour? In my case its oembed.

What I want:
Instead of parsing (oembed: url/to/video) it should do nothing (or just insert the url).

I experimentet a little with AMP before, what I did was: creating my own custom field method:

$kirby->set('field::method', 'amp', function ($field) {
    return amp($field->value);
});

//...

function amp($content)
{
    $snippets = c::get('ampMarkdownReplacements', [
        '(image:' => '(amp-image:',
        '(youtube:' => '(amp-youtube:',
    ]);
    $values = array_values($snippets);
    $keys = array_keys($snippets);
    return str_replace($keys, $values, $content);
}

(you need to change the amp-Function to remove the oembed code…)
Then use it like this in your templates: <?php $maincontent = kirbytext(amp($page->text())) ?>

What I did was creating “amp markdown code” which I transformed to AMP-HTML using custom tags.

2 Likes

Thanks! This is working just fine!

Hi, i’m newbie on this , where I should put this code ?
I just want to generate amp version of a microweb

thanks

site/tags/amp-image.php

this needs to be added, as @Pascalmh didn’t include this custom tag

<?php

kirbytext::$tags['amp-image'] = array(
  'attr' => array(
    'alt'
  ),
  'html' => function($tag) {
    $alt = $tag->attr('alt');
    if($file = $tag->file($tag->attr('amp-image'))){
      $width = $file->width();
      $height = $file->height();
    }
    return '<div><amp-img src="'. $tag->attr('amp-image') . '" alt="'.$alt.'" height="'.$height.'" width="'.$width.'"  layout="responsive"></amp-img></div>';
  }
);

like above but with amp-youtube.php

<?php

kirbytext::$tags['amp-youtube'] = array(
  'html' => function($tag) {
    return '<div><amp-youtube data-videoid="'.$tag->attr('amp-youtube').'"
    layout="responsive"
    width="480" height="270"></amp-youtube></div>';
  }
);

custom field method goes towards site/plugins/page-methods.php

1 Like