Skip to content

highlight() is inconsistent when code is illegal syntax #164

@seaoak

Description

@seaoak

Generally the result of highlight() contains lauguage name as HTML class name.

If code is illegal syntax for the specified language,
the HTML class name is added or not (inconsistent).

option lang illegal syntax? option hljs class name of outer <figure> class name of inner <code>
java no false highlight java N/A (none)
java no true highlight java hljs java
java yes false highlight N/A (none)
java yes true highlight hljs java
none - false highlight plain N/A (none)
none - true highlight plain hljs plain

I think we have three options:

  1. always add laugage name even if illegal syntax.
  2. never add language name if illegal syntax.
  3. add the language name plain if illegal syntax.

I think Option 1 might be best because users will intend to be.

Please advice!


I think this inconsistency is caused by undocumented behavior of highlight() of highlightjs package.

The API document of highlightjs package is:
https://highlightjs.readthedocs.io/en/latest/api.html#highlight-languagename-code-ignore-illegals-continuation

According to above API document, highlight() throws an exception in case of detecting illegal syntax for the language.
But actually not throw any exception (regardless ignore_illegals parameter).
Instead, illegal property of the return value is set to true and
language property of the return value does not exist.

Current code of lib/highlight.js considers the exception halfway.
And the lack of lanaguage property is not expected.

Anyway the code should be modified to keep consistency.

p.s. This issue is derived from hexojs/hexo#3984

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions