-
Notifications
You must be signed in to change notification settings - Fork 25
Generate og:description from page content when not explicitly set #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
_layouts/page.html
Outdated
| {%- endif -%} | ||
|
|
||
| <meta property="og:description" content="{{ page.description | default: site.cs50.description }}"> | ||
| {%- capture dynamic_description -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change dynamic_description to description.
_layouts/page.html
Outdated
| {%- if page.description -%} | ||
| {{- page.description -}} | ||
| {%- elsif page.content -%} | ||
| {{- page.content | generate_description -}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change generate_description to describe.
lib/jekyll-theme-cs50.rb
Outdated
| def generate_description(input, max_length = 160) | ||
| return "" if input.nil? || input.to_s.strip.empty? | ||
|
|
||
| # Convert markdown to HTML first if possible |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change "markdown" to "Markdown"
lib/jekyll-theme-cs50.rb
Outdated
| html = input.to_s | ||
| end | ||
| rescue StandardError => e | ||
| Jekyll.logger.debug "CS50: Could not convert markdown to HTML: #{e.message}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Markdown
lib/jekyll-theme-cs50.rb
Outdated
| end | ||
|
|
||
| # Clean up whitespace | ||
| text = text.gsub(/\s+/, ' ').strip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use double quotes.
lib/jekyll-theme-cs50.rb
Outdated
| # Truncate to max_length, breaking at word boundary | ||
| if text.length > max_length | ||
| text = text[0...(max_length - 3)] | ||
| last_space = text.rindex(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use double quotes.
lib/jekyll-theme-cs50.rb
Outdated
| text = text[0...(max_length - 3)] | ||
| last_space = text.rindex(' ') | ||
| text = text[0...last_space] if last_space && last_space > 0 | ||
| text += '...' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double quotes.
lib/jekyll-theme-cs50.rb
Outdated
| # Convert markdown to HTML first if possible | ||
| begin | ||
| if defined?($site) && $site | ||
| html = $site.find_converter_instance(::Jekyll::Converters::Markdown).convert(input.to_s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace lines 108–117 with just line 110, since we know it'll exist.
lib/jekyll-theme-cs50.rb
Outdated
|
|
||
| # Parse HTML and extract text | ||
| begin | ||
| doc = Nokogiri::HTML5.fragment(html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace lines 119–133 with just line 121 since we know it'll be installed.
|
@copilot Make my changes. |
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.gitignore
Outdated
|
|
||
| node_modules/ | ||
|
|
||
| test_site/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line and whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 3875fd3. Removed the blank line and test_site/ entry.
| html = $site.find_converter_instance(::Jekyll::Converters::Markdown).convert(input.to_s) | ||
|
|
||
| # Parse HTML and extract text | ||
| doc = Nokogiri::HTML5.fragment(html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After converting to HTML, remove the page's title, which is the first h1 tag, and remove any table of contents, which would be the first ul with an id of #markdown-toc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 3875fd3. The describe filter now removes the first h1 tag and first ul#markdown-toc before extracting text for the description.
Co-authored-by: dmalan <788678+dmalan@users.noreply.github.com>
Pages without explicit
descriptionin front matter currently fall back to the site-wide default, which may not be relevant. This PR generates descriptions by extracting and formatting text from page content.Changes
New Liquid filter (
lib/jekyll-theme-cs50.rb)describe: Converts Markdown to HTML, extracts plain text, normalizes whitespace, truncates to ~160 chars at word boundariesh1tag) and table of contents (ul#markdown-toc) from the generated descriptionUpdated layout (
_layouts/page.html)page.description→ generated frompage.content→site.cs50.descriptionExample
Before:
After:
{%- 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 }}">Page with content but no explicit description now generates:
Note: The generated description excludes the page title and table of contents, ensuring only the actual content is used.
Security: Passes CodeQL with 0 alerts.
og:descriptionfrom page contents #204Original prompt
og:descriptionfrom page contents #204💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.