Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions _layouts/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,9 @@
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, width=device-width">

{%- if page.refresh -%}
{%- if page.refresh %}
<meta content="{{ page.refresh }}" http-equiv="refresh">
{%- endif -%}

{%- capture description -%}
{%- if page.description -%}
{{- page.description -}}
{%- elsif page.content -%}
{{- page.content | describe -}}
{%- else -%}
{{- site.cs50.description -}}
{%- endif -%}
{%- endcapture -%}
<meta property="og:description" content="{{ description | strip }}">
{%- endif %}

<meta property="og:image" content="{{ page.image | default: site.cs50.image }}">

Expand All @@ -34,7 +23,7 @@
{{- site.cs50.title -}}
{%- endunless -%}
{%- endif -%}
{%- endcapture -%}
{%- endcapture %}

<meta property="og:title" content="{{ title }}">

Expand Down
54 changes: 54 additions & 0 deletions lib/jekyll-theme-cs50.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,60 @@ def relative_url(input)
end
end

Jekyll::Hooks.register [:pages], :post_render do |page|

# Skip if not HTML
next if page.output_ext != ".html"

# Parse HTML
doc = Nokogiri::HTML(page.output)
main = doc.at_css("main")
next if main.nil?

# If page.description
if page.data.key?("description")
description = page.data["description"]

# Else infer
else

# Remove the page's title (i.e., first h1 tag)
main.css("h1").first&.remove

# Remove any table of contents
main.css("ul#markdown-toc").first&.remove

# Remove any spoilers
main.css("details")&.remove

# Strip tags
text = main.text.strip

# Clean up whitespace
text = text.gsub(/\s+/, " ").strip

# Truncate to max_length, breaking at word boundary
max_length = 160
if text.length > max_length
description = text[0...(max_length - 3)]
last_space = description .rindex(" ")
description = description[0...last_space] if last_space && last_space > 0
description += "..."
end
end

# Inject description
head = doc.at_css("head")
next if head.nil?
meta = Nokogiri::XML::Node.new("meta", doc)
meta["property"] = "og:description"
meta["content"] = CGI.escapeHTML(description)
head.add_child(meta)

# Update HTML
page.output = doc.to_html
end

Jekyll::Hooks.register [:site], :post_render do |site|

# Paths of pages
Expand Down