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

feat(highlight): adjust the layout of the highlight code block #132

Merged
merged 3 commits into from
Nov 17, 2019
Merged

feat(highlight): adjust the layout of the highlight code block #132

merged 3 commits into from
Nov 17, 2019

Conversation

stevenjoezhang
Copy link
Member

@stevenjoezhang stevenjoezhang commented Nov 15, 2019

Current behavior

<tr>
  <td class="gutter"><pre>
    <span class="line">1</span>
    <br>
    <span class="line">2</span>
    <br>
    <span class="line">3</span>
  </pre></td>

  <td class="code"><pre>
    <span class="line">hello, world</pre>
    <br>
    <span class="line">hello, world</span>
    <br>
    <span class="line">hello, world</span>
  </pre></td>
</tr>

New behavior

<tr>
  <td class="gutter"><pre>
    <span class="line">1</span>
  </pre></td>
  <td class="code"><pre>
    <span class="line">hello, world</pre>
  </td>
</tr>

<tr>
  <td class="gutter"><pre>
    <span class="line">2</span>
  </pre></td>
  <td class="code"><pre>
    <span class="line">hello, world</pre>
  </td>
</tr>

<tr>
  <td class="gutter"><pre>
    <span class="line">3</span>
  </pre></td>
  <td class="code"><pre>
    <span class="line">hello, world</pre>
  </td>
</tr>

@coveralls
Copy link

coveralls commented Nov 15, 2019

Coverage Status

Coverage decreased (-0.02%) to 95.364% when pulling a605d94 on stevenjoezhang:master into a1ad13b on hexojs:master.

@stevenjoezhang stevenjoezhang changed the title Adjust the layout of the highlight code block feat(highlight): adjust the layout of the highlight code block Nov 15, 2019
@SukkaW
Copy link
Member

SukkaW commented Nov 15, 2019

Currently I am working on a new Hexo theme, and highlight style problem just nearly makes me mad. I am sure this PR would help, but I am wonder if this will significantly increase output HTML size.

@SukkaW
Copy link
Member

SukkaW commented Nov 15, 2019

Also, the test case needs to be updated in order to pass the Travis CI test.

@stevenjoezhang
Copy link
Member Author

Travis CI's error report has been fixed.

The main idea of this PR is: it's a bit strange to use <br> inside the <table> element. Use more <tr> elements can make the structure of the table more logical.

Copy link
Contributor

@curbengh curbengh left a comment

Choose a reason for hiding this comment

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

Tested in local machine.

@curbengh curbengh merged commit b201c8e into hexojs:master Nov 17, 2019
@stevenjoezhang
Copy link
Member Author

@SukkaW @curbengh @jiangtj
Talking about backward compatible, I found that I have made a mistake. This PR might make the empty lines disappear in <table> if highlight.line_number is disabled in _config.yml.

function formatLine(line, lineno, marked, options) {
const useHljs = options.hljs || false;
let res = useHljs ? '' : '<span class="line';
if (marked.includes(lineno)) {
// Handle marked lines.
res += useHljs ? `<mark>${line}</mark>` : ` marked">${line}</span>`;
} else {
res += useHljs ? line : `">${line}</span>`;
}
return res;
}

And here's the solution - it's necessary to insert a <br> tag if the line is empty.

function formatLine(line, lineno, marked, options) {
+  line = line || '<br>';
  const useHljs = options.hljs || false;
  let res = useHljs ? '' : '<span class="line';
  if (marked.includes(lineno)) {
    // Handle marked lines.
    res += useHljs ? `<mark>${line}</mark>` : ` marked">${line}</span>`;
  } else {
    res += useHljs ? line : `">${line}</span>`;
  }


  return res;
}

@curbengh
Copy link
Contributor

curbengh commented Dec 9, 2019

I see, I was wondering why empty line is removed in https://deploy-preview-1241--hexo-site.netlify.com/news/2019/12/09/hexo-4-1-released/ when previewing hexojs/site#1241 . hexo website does disabled line_number.

Please submit a PR.

Need to make sure it's compatible with the following configs:

# Need a unit test for this config in that PR, the rest can test on sample blog
highlight:
  line_number: false
  wrap: true
highlight:
  line_number: false
  wrap: false
# they are decoupled after #138
highlight:
  hljs: false
  wrap: true
highlight:
  hljs: false
  wrap: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants