Skip to content

Commit

Permalink
尝试增加搜索引擎优化的 meta 字段
Browse files Browse the repository at this point in the history
  • Loading branch information
windy committed Apr 2, 2014
1 parent 0727c94 commit 69bc42c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ def visited
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 = '<div>' + html + '</div>'
Nokogiri.parse(html).text()
end

def labels_content( need_blank=false )
Expand Down
3 changes: 3 additions & 0 deletions app/views/blogs/index.html.slim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/views/blogs/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- content_for(:meta) do
meta name="description" content="#{@post.meta_content}"
meta name="keywords" content[email protected]_content

- content_for(:title) do
| #{@post.title}
.row
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 69bc42c

Please sign in to comment.