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

refactor(meta_generator): update regex and avoid edge case #3697

merged 2 commits into from Sep 2, 2019


Copy link

SukkaW commented Aug 31, 2019

What does it do?

After #3671 & #3696 , meta_generator filter will replace </title> with <meta name="generator" content="Hexo ${hexo.version}"></title>.

@curbengh has mentioned some edge cases will cause unwanted behavior:

<!-- A inline svg in css might include <title> tag -->
  .foo {
    background: url(data:image/svg+xml;utf8,<svg width="500" height="300" xmlns=""><g><title>svg</title></g></svg>);

This PR introduce replacing </head> with <meta name="generator" content="Hexo ${hexo.version}"></head> with a pattern that ignore empty <head> tag.

How to test

git clone -b meta-generator
cd hexo
npm install
npm test

A test case about ignore empty head tag has been added in this PR as well.


According to benchmark, use regex to match </head> is about 1% slower than replace </title> directly.

NodeJS 8 NodeJS 10 NodeJS 12
Disable meta_generator 6.15s 5.18s 5.85s
Replace </title> directly (current master branch) 6.25s 5.25s 5.94s
This PR 6.3s 5.30s 5.94s

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.
@SukkaW SukkaW requested a review from curbengh Aug 31, 2019

This comment has been minimized.

Copy link

coveralls commented Aug 31, 2019

Coverage Status

Coverage remained the same at 97.225% when pulling ce86ca9 on SukkaW:meta-generator into eca3cf2 on hexojs:master.

@SukkaW SukkaW requested review from segayuu and YoshinoriN Aug 31, 2019
test/scripts/filters/meta_generator.js Show resolved Hide resolved
test/scripts/filters/meta_generator.js Outdated Show resolved Hide resolved
Apply suggestions from code review by @curbengh:

- update other test cases as the filter no longer seeks <title>
- seperate 'apply to first non-empty head tag only' from 'ignore empty head tag'
@SukkaW SukkaW requested a review from curbengh Sep 1, 2019
@curbengh curbengh merged commit 8d0a581 into hexojs:master Sep 2, 2019
4 checks passed
4 checks passed
Travis CI - Pull Request Build Passed
codeclimate All good!
continuous-integration/appveyor/pr AppVeyor build succeeded
coverage/coveralls Coverage remained the same at 97.225%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.