# This sample template explores how we can turn the Medium post on the left into a Telegram Instant View page as shown on the right — in several simple steps. If you're unsure what some of the elements used here do, check out the full documentation here: https://instantview.telegram.org/docs
### STEP 1: Define which pages get Instant View and which don't
# We only want to generate Instant View pages for articles. Other things, like lists of articles, profiles, the about section, etc. should be ignored.
# Conveniently, all article pages on Medium seem to have a <meta property="article:published_time"> tag.
# If this tag is not present, we'll assume that the page is not an article and does not require an Instant View page.
# This *condition* does the trick:
### STEP 2: Define the essential elements
# The 'body' and 'title' *properties* are required for an Instant View page to work.
# 'Subtitle' is optional for IV pages, but Medium posts can have subtitles, so it is essential that we reflect this in our template.
### Now we'll set a cover image. It's also not required, but we need one if we want our Instant view page to look cool.
# Some Medium posts have a cover image in the header background, we can use that.
# First, let's assign the background node to the '$bg_section' *variable* for subsequent use.
# Call the @background_to_image *function* to convert the background image to an <img>
# Replace the //div tag with <figure> tags.
# Set the figure as the value of the 'cover' *property*.
# If we didn't find a cover image, check a few other blocks that may contain it.
# Note that 'cover:' is a *property*, so by default it will not be overwritten once we've assigned a value for the first time. This means that each of the following rules will only be applied if we don't have a cover yet.
### Now to find an image for link previews. Links shared via Telegram will show an extended preview with a small picture in the chat. Let's try to find something for this image.
# If we've already got a cover, we'll use it for the link preview image too.
# If we didn't find a cover, we'll take a picture from the meta tags.
# Otherwise, the link preview will just have text in it, which is also OK.
### STEP 3: Further refinements
# Pullquote in Medium contains a 'strong' tag, but this shouldn't make the whole quote bold.
# Now we'll inline embeds that are wrapped in additional frames using the '@inline' *function*.
@inline: $body//iframe[starts-with(@src, "/media/")]
# Medium shows preformatted blocks/blockquotes together if they are next to each other, we'll combine them using two breaks as a separator.
# Medium articles can contain embedded link previews, we'll show such previews as blockquotes.
## STEP 4: Cleanup
# Remove excessive nodes, we don't need them on the IV page.
# Remove the first divider, it's invisible on Medium pages anyway.
# Some pages use thin images as a divider. Telegram doesn't like thin images much, so we'll replace them with a simple divider.
@before(<hr>): $body//figure[.//img[number(@data-height) < 30][(number(@data-width) > number(@data-height) * 30)]]
### And that's it, we're done. Now the rules from the '..after' block will be applied and the Instant View page is ready. Feel free to click on the panel below to see what exactly is done in the '..after' section.