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

Exclude some languages in code highlighting #3865

Merged
merged 1 commit into from
Feb 27, 2022

Conversation

corvofeng
Copy link
Contributor

@corvofeng corvofeng commented Nov 17, 2019

Signed-off-by: corvofeng [email protected]

What does it do?

Only wrap some languages' code with <pre> and <code>, and do no render all tags(span, and br) in content, so that user could process those languages ​​freely.

How to test

git clone -b exclude_highlight_lang https:/corvofeng/hexo.git
cd hexo
npm install
npm test

How to use?

config.yaml:

highlight:
  enable: true
  line_number: true
  auto_detect: false
  exclude_languages:
    - flowchartjs-code

Blog code:

```flowchartjs-code
st=>start: Start:>http://www.bing.com[blank]
e=>end:>http://www.bing.com
op1=>operation: My Operation
sub1=>subroutine: My Subroutine
cond=>condition: Yes
or No?:>http://www.google.com
io=>inputoutput: catch something...
para=>parallel: parallel tasks

st->op1->cond
cond(yes)->io->e
cond(no)->para
para(path1, bottom)->sub1(right)->op1
para(path2, top)->op1
```

You will get:

<code class="highlight flowchartjs-code">st=&gt;start: Start:&gt;http://www.bing.com[blank]
e=&gt;end:&gt;http://www.bing.com
op1=&gt;operation: My Operation
sub1=&gt;subroutine: My Subroutine
cond=&gt;condition: Yes
or No?:&gt;http://www.google.com
io=&gt;inputoutput: catch something...
para=&gt;parallel: parallel tasks

st-&gt;op1-&gt;cond
cond(yes)-&gt;io-&gt;e
cond(no)-&gt;para
para(path1, bottom)-&gt;sub1(right)-&gt;op1
para(path2, top)-&gt;op1</code>

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@coveralls
Copy link

coveralls commented Nov 17, 2019

Coverage Status

Coverage increased (+0.002%) to 98.301% when pulling bf18425 on corvofeng:exclude_highlight_lang into 1f9755e on hexojs:master.

@curbengh
Copy link
Contributor

Requires #3827

@curbengh
Copy link
Contributor

I just started testing this,

Only wrap some languages' code with <pre> and <code>, and do no render all tags(span, and br) in content, so that user could process those languages ​​freely.

By "render", you mean html escape?

I tried

if (options.exclude_languages) {
  if (options.exclude_languages.includes(options.lang)) {
    options.wrap = false;
<br>
    options.gutter = false;
    options.autoDetect = false;
  }
}

<br> is still escaped, even when highlight.enable is disabled.

Looks like exclude_wrap: [] option is more accurate?

@corvofeng
Copy link
Contributor Author

corvofeng commented Dec 22, 2019

I just started testing this,

Only wrap some languages' code with <pre> and <code>, and do no render all tags(span, and br) in content, so that user could process those languages ​​freely.

By "render", you mean html escape?

I tried

if (options.exclude_languages) {
  if (options.exclude_languages.includes(options.lang)) {
    options.wrap = false;
<br>
    options.gutter = false;
    options.autoDetect = false;
  }
}

<br> is still escaped, even when highlight.enable is disabled.

Looks like exclude_wrap: [] option is more accurate?

Well, in my blogs, when I work with my latest changes and [email protected], I could get the proper result which does not include <br>. Would you like to use hexo clean firstly, and then check the result. This option should only work when the highlight is enabled

exclude_wrap is also fine, but I strongly recommend add languages or langs postfix because it is an array of a few languages.

@stevenjoezhang
Copy link
Member

There will be other code highlighting engines added to Hexo: #4119
I suggest you implement this feature using custom tags, e.g.

{% flowchartjscode %}
st=>start: Start:>http://www.bing.com[blank]
e=>end:>http://www.bing.com
{% endflowchartjscode %}

This looks more clear than
```flowchartjs-code
st=>start: Start:>http://www.bing.com[blank]
e=>end:>http://www.bing.com
```

@corvofeng
Copy link
Contributor Author

There will be other code highlighting engines added to Hexo: #4119
I suggest you implement this feature using custom tags, e.g.

{% flowchartjscode %}
st=>start: Start:>http://www.bing.com[blank]
e=>end:>http://www.bing.com
{% endflowchartjscode %}

This looks more clear than

st=>start: Start:>[http://www.bing.com[blank]](http://www.bing.com%5Bblank%5D)
e=>end:>http://www.bing.com

Well, I have downloaded that branch and tested it, and it may somehow achieve the function I requested. But I have a question: why not let the hexo user decide when the code should be rendered(in the hexo building or in the browser loading).

Copy link
Member

@stevenjoezhang stevenjoezhang left a comment

Choose a reason for hiding this comment

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

Please make it compatible with #4119 thanks

@SukkaW
Copy link
Member

SukkaW commented Jun 6, 2020

@stevenjoezhang

Will, we could merge #4119 first, and then I will bring up the feature in another PR.

@corvofeng
Copy link
Contributor Author

@SukkaW @stevenjoezhang @curbengh

I have rebased this PR based on the latest master branch, could anyone merge it or give me some advice? I don’t want to be forgotten after a long time, because I really need this feature.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot added the stale label Nov 30, 2021
@stevenjoezhang stevenjoezhang changed the title Exclude some languages in code highlghting. Exclude some languages in code highlighting Dec 1, 2021
@github-actions github-actions bot closed this Dec 9, 2021
@github-actions
Copy link

How to test

git clone -b exclude_highlight_lang https:/hexojs/hexo.git
cd hexo
npm install
npm test

@SukkaW
Copy link
Member

SukkaW commented Feb 25, 2022

The failed test is fixed in #4902, not caused by the PR itself. The change LGTM.

@yoshinorin yoshinorin merged commit c73dbed into hexojs:master Feb 27, 2022
yoshinorin added a commit to yoshinorin/site that referenced this pull request Mar 12, 2022
yoshinorin added a commit to hexojs/site that referenced this pull request Mar 13, 2022
@D-Sketon
Copy link
Member

D-Sketon commented Nov 22, 2022

I try to exclude java in highlight like this:

highlight:
  enable: true
  line_number: true
  auto_detect: false
  tab_replace: ''
  wrap: true
  hljs: false
  exclude_languages:
    - java 

But I find that tags still exist, is this expected behavior?🤔🤔

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.

7 participants