Skip to content

Commit

Permalink
let Nokogiri parse only the necessary markup
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Mar 21, 2018
1 parent a832c27 commit a24f687
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jemoji.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Gem::Specification.new do |s|
s.add_dependency "jekyll", "~> 3.0"

s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "rubocop", "~> 0.51"
s.add_development_dependency "rubocop", "~> 0.51.0"
end
11 changes: 6 additions & 5 deletions lib/jemoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Emoji
GITHUB_DOT_COM_ASSET_HOST_URL = "https://assets-cdn.github.com".freeze
ASSET_PATH = "/images/icons/".freeze
BODY_START_TAG = "<body".freeze
OPENING_BODY_TAG_REGEX = %r!<body(.*)>\s*!

class << self
def emojify(doc)
Expand Down Expand Up @@ -79,11 +80,11 @@ def default_asset_root
end

def replace_document_body(doc)
src = emoji_src(doc.site.config)
parsed_doc = Nokogiri::HTML::Document.parse(doc.output)
body = parsed_doc.at_css("body")
body.children = filter_with_emoji(src).call(body.inner_html)[:output].to_s
parsed_doc.to_html
src = emoji_src(doc.site.config)
head, opener, tail = doc.output.partition(OPENING_BODY_TAG_REGEX)
body_content, *rest = tail.partition("</body>")
processed_markup = filter_with_emoji(src).call(body_content)[:output].to_s
String.new(head) << opener << processed_markup << rest.join
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>Jemoji</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
Expand Down

0 comments on commit a24f687

Please sign in to comment.