Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Nokogiri parse only the necessary markup #74

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin(s) will no longer add this extra markup to our output..
The passing script/cibuild testifies this..

<meta charset="UTF-8">
<title>Jemoji</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
Expand Down