Skip to content

Conversation

seaoak
Copy link
Member

@seaoak seaoak commented Oct 19, 2019

Fix #111

This patch is fix the behavior of highlight() with a option wrap: false.

If hljs: false, the output of highlight() should include a HTML class name highlight.
It is consistent with the case of hljs: true.

And also, even if hljs: false, the output of highlight() should be include a PRE element and a CODE element.
It is consistent with the case of hljs: true, too.

@coveralls
Copy link

coveralls commented Oct 19, 2019

Coverage Status

Coverage increased (+0.02%) to 95.302% when pulling 62ab0f8 on seaoak:feature/make_consistency_in_output_when_wrap_false into 635161b on hexojs:master.

@seaoak
Copy link
Member Author

seaoak commented Oct 19, 2019

This PR will be used to introduce wrap option in highlight section of Hexo's _config.yml.

@seaoak seaoak requested a review from curbengh October 19, 2019 01:03
lib/highlight.js Outdated
@@ -21,13 +21,15 @@ function highlightUtil(str, options = {}) {
hljs.configure({ classPrefix: useHljs ? 'hljs-' : ''});

const data = highlight(str, options);
const lang = options.lang || ''; // Maybe "data.language" should be included
Copy link
Contributor

Choose a reason for hiding this comment

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

My understanding is that data.language would default to plain value if no lang is provided. In current behavior wrap = true, plain does get appended to class name -> "highlight plain".

So, I agree with options.lang || data.language || '';.

Copy link
Member Author

Choose a reason for hiding this comment

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

One thing I worry about is that the default behavior of Hexo with hljs: true will be changed.

Under the code options.lang || data.language || '',
when hljs:true is specified with neither lang option nor gutter option,
the output of highlight() will be <pre><code class="hljs plain">....

On the other hand, the current implementation says <pre><code class="hljs undefined">....
That is, a class name plain is not added (adding undefined is a tiny bug).

Under the code options.lang || '' (without data.language),
the default output of highlight() will be <pre><code class="hljs">....
That is, no class name is added.

So including data.language causes Hexo changing the default behavior with hljs: true, line_number: false.
Is it OK?

Copy link
Contributor

@curbengh curbengh Oct 22, 2019

Choose a reason for hiding this comment

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

the current implementation says <pre><code class="hljs undefined">

which implementation are you referring to? this PR with options.lang || data.language || ''?


The main reason of having "highlight plain" is so that "hljs: false, wrap: false" can be compatible with current theme as much as possible (which uses the default "hljs: false, wrap: true". I just worry that some theme might use "plain" class.

So including data.language causes Hexo changing the default behavior with hljs: true, line_number: false.

I don't foresee any breaking change for adding a new class (i.e. "plain"), even when it's not used in the newer syntax.


In summary, without data.language, "plain" class is removed when "hljs: false, wrap: false" which is a possible breaking change. Having an unused "plain" class in "hljs: true" is not a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

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

@curbengh Thank you for explaining. I didn't understand enough.
Now I understand that data.language is necessary to keep compatibility with current themes and adding a class plain will not break current themes.

I'll update the code soon.

@seaoak seaoak force-pushed the feature/make_consistency_in_output_when_wrap_false branch from 73ce38f to a8de9f5 Compare October 24, 2019 00:19
@seaoak
Copy link
Member Author

seaoak commented Oct 24, 2019

I updated the code of this PR (reflect @curbengh 's comments and add two test cases).
Could you review this again?

@seaoak seaoak requested a review from curbengh October 25, 2019 00:06
@curbengh
Copy link
Contributor

curbengh commented Oct 27, 2019

Since wrap: false is not compatible with gutter: true, shall we give priority to "gutter" (i.e. enable "wrap" when "gutter" is enabled)? My preference is on "gutter".

Definitely we'll have to mention in the docs about the compatibility (after wrap option is introduced in hexo).

@seaoak
Copy link
Member Author

seaoak commented Oct 27, 2019

I agree with giving the priority (gutter option is superior to wrap option).

I will update this PR to arbitrate the conflict with them.

@seaoak seaoak force-pushed the feature/make_consistency_in_output_when_wrap_false branch from a8de9f5 to 980adfd Compare October 27, 2019 09:39
…xojs#111)

This patch is fix the behavior of `highlight()` with a option `wrap: false`.

If `hljs: false`, the output of `highlight()` should include a HTML class name `highlight`.
It is consistent with the case of `hljs:true`.

And also, even if `hljs: false`, the output of `highlight()` should include a PRE element and a CODE element.
It is consistent with the case of `hljs:true`, too.
@seaoak seaoak force-pushed the feature/make_consistency_in_output_when_wrap_false branch from 980adfd to 62ab0f8 Compare October 28, 2019 00:00
@seaoak
Copy link
Member Author

seaoak commented Oct 28, 2019

@curbengh I'm sorry it was an elementary mistake.
I updated this PR (adding a test case).

@curbengh curbengh requested a review from a team October 28, 2019 03:33
@curbengh curbengh merged commit 42b7393 into hexojs:master Nov 2, 2019
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.

highlight.js generates no HTML class name when "wrap: false" and "hljs: false"
3 participants