defmodule Blogex.SEO do
@moduledoc """
SEO helpers for generating meta tags and sitemaps.
"""
@doc """
Returns a map of meta tag attributes for a blog listing page.
Useful for setting OpenGraph and Twitter card tags on blog index pages.
"""
def meta_tags_for_blog(blog_module, base_url) do
url = "#{base_url}#{blog_module.base_path()}"
%{
title: blog_module.title(),
description: blog_module.description(),
og_title: blog_module.title(),
og_description: blog_module.description(),
og_type: "website",
og_url: url,
og_image: nil,
twitter_card: "summary"
}
end
@doc """
Returns a map of meta tag attributes for a post.
Useful for setting OpenGraph and Twitter card tags in your layout.
"""
def meta_tags(post, base_url, blog_module) do
url = "#{base_url}#{blog_module.base_path()}/#{post.id}"
%{
title: post.title,
description: post.description,
og_title: post.title,
og_description: post.description,
og_type: "article",
og_url: url,
og_image: if(post.image, do: "#{base_url}#{post.image}", else: nil),
article_published_time: Date.to_iso8601(post.date),
article_author: post.author,
article_tags: post.tags,
twitter_card: "summary_large_image"
}
end
@doc """
Generates a sitemap.xml string for the given blog modules.
xml = Blogex.SEO.sitemap([MyApp.EngineeringBlog, MyApp.ReleaseNotes], "https://myapp.com")
"""
def sitemap(blog_modules, base_url) when is_list(blog_modules) do
urls =
blog_modules
|> Enum.flat_map(fn mod ->
mod.all_posts()
|> Enum.map(fn post ->
url = "#{base_url}#{mod.base_path()}/#{post.id}"
lastmod = Date.to_iso8601(post.date)
"""
#{url}
#{lastmod}
monthly
0.7
"""
end)
end)
|> Enum.join()
"""
#{urls}
"""
|> String.trim()
end
end