From 69bc42c4effe797689b4e1b4d2923ab30b20c9fb Mon Sep 17 00:00:00 2001 From: yafeilee Date: Wed, 2 Apr 2014 15:06:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E5=A2=9E=E5=8A=A0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=93=8E=E4=BC=98=E5=8C=96=E7=9A=84=20meta?= =?UTF-8?q?=20=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/post.rb | 11 ++++++++++- app/views/blogs/index.html.slim | 3 +++ app/views/blogs/show.html.slim | 4 ++++ app/views/layouts/application.html.slim | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) 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"