diff --git a/app/models/post.rb b/app/models/post.rb index 7a88fbe..a3a9006 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -44,8 +44,17 @@ class Post self.visited_count end + # 显示给首页截断数据 def sub_content - HTML_Truncator.truncate(content_html,30) + HTML_Truncator.truncate(content_html, 300, length_in_chars: true) + end + + # 显示给 meta description + def meta_content + html = HTML_Truncator.truncate(content_html, 100, :length_in_chars => true, ellipsis: '') + # 加上 div 以方便 Nokogiri 获取 text() + html = '
' + html + '
' + Nokogiri.parse(html).text() end def labels_content( need_blank=false ) diff --git a/app/views/blogs/index.html.slim b/app/views/blogs/index.html.slim index f5f0d72..71c1a77 100644 --- a/app/views/blogs/index.html.slim +++ b/app/views/blogs/index.html.slim @@ -1,3 +1,6 @@ +- if ENV['INTRODUCE'].present? + - content_for(:meta) do + meta name="description" content="#{ENV['INTRODUCE'].dup.force_encoding('UTF-8')}" - content_for(:title) do | 首页 .row diff --git a/app/views/blogs/show.html.slim b/app/views/blogs/show.html.slim index 8769baf..5174221 100644 --- a/app/views/blogs/show.html.slim +++ b/app/views/blogs/show.html.slim @@ -1,3 +1,7 @@ +- content_for(:meta) do + meta name="description" content="#{@post.meta_content}" + meta name="keywords" content=@post.labels_content + - content_for(:title) do | #{@post.title} .row diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 31ade89..4b5a466 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -2,6 +2,8 @@ html head meta charset="utf-8" meta name="viewport" content="width=device-width, initial-scale=1.0" + - if content_for?(:meta) + = yield(:meta) title = content_for?(:title) ? yield(:title) + " | #{ENV['SITE_NAME']}" : ENV['SITE_NAME'] = stylesheet_link_tag "application"