Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 1.45 KB

EXAMPLES.livemd

File metadata and controls

71 lines (54 loc) · 1.45 KB

Liquor Examples

Liquor Tags. Inspired by dev.to embed tags (liquid tags), takes Open Graph data from URLs and generate output using pseudo EEx templates. Useful when creating previews of websites using Open Graph data.

Installation

Mix.install([
  {:liquor, git: "https://github.com/ElixirCL/liquor.git", branch: "main"},
  {:req, "~> 0.4.14"},
  {:kino, "~> 0.12.3"}
])
alias Liquor.Tags.Tag

content = """
{% github ElixirCL %}
{% embed https://ninjas.cl %}
"""

tags = [
  Tag.new("github", "Github: <%= og.url %>", "https://github.com"),
  Tag.new("embed", "Website: <%= og.title %>")
]

# Fetch only needs to return the HTML string
fetch = fn url -> Req.get!(url) |> then(& &1.body) end

Liquor.render(content, tags, fetch)
|> Kino.Text.new()

Renders

Github: https://github.com/ElixirCL
Website: Ninja Software Chile (Ninjas.cl)

Advanced Fetch

You can also create a fetch function that caches the html files like this:

fetch =
  fn url ->
        md5 =
          :crypto.hash(:md5, url)
          |> Base.encode16(case: :lower)

        path = Path.join([".opengraph"])
        File.mkdir_p!(path)
        file = Path.join([path, "#{md5}.html.cache"])

        case File.read(file) do
          {:error, _} ->
                html = Req.get!(url)
                |> then(& &1.body)

                File.write!(file, html, [:write])
                html

          {:ok, html} -> html
        end
  end