Skip to content

Commit

Permalink
let Nokogiri parse only the necessary markup (#74)
Browse files Browse the repository at this point in the history
Merge pull request 74
  • Loading branch information
ashmaroli authored and jekyllbot committed May 2, 2018
1 parent 017a162 commit f5e2e08
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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"
ASSET_PATH = "/images/icons/"
BODY_START_TAG = "<body"
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 f5e2e08

Please sign in to comment.