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

Remove use of global highlight instance #51

Merged
merged 1 commit into from
Sep 4, 2023

Conversation

dschwank
Copy link
Contributor

@dschwank dschwank commented Jul 5, 2023

With this PR we make use of the newInstance function of highlight.js, to not pollute the global instance anymore. As the configuration is local only now, we don't have to reset it anymore.

Closes #47

test/index.js Outdated
import {rehype} from 'rehype'
import {removePosition} from 'unist-util-remove-position'
import {lowlight} from '../index.js'
import {lowlight} from '../lib/all.js'
Copy link
Owner

Choose a reason for hiding this comment

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

Why mock? Wouldn’t it be better to actually test whether the two interfere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As the configuration can't be read, we would have to compare the results of highlight and lowlight. Would you prefer that instead of checking if configure is called?

Copy link
Contributor Author

@dschwank dschwank Jul 5, 2023

Choose a reason for hiding this comment

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

Btw. I had to switch to ../lib/all.js, as not all languages of the fixture folder are covered by the common setup. Would you rather prefer to remove the fixtures?

And another topic - locally I saw this failing check, but the message wasn't clear to me 😬

/home/runner/work/lowlight/lowlight/lib/all.d.ts:2:13: DoNotTochItRegistersLanguageFiles
1846 / 1847 99.94%
The type coverage rate(99.94%) is lower than the target(100%).

Copy link
Owner

Choose a reason for hiding this comment

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

To get the tests working, I think this is the trick:

 import highlight from 'highlight.js'
+import coffeescript from 'highlight.js/lib/languages/coffeescript'
+import haskell from 'highlight.js/lib/languages/haskell'
+import http from 'highlight.js/lib/languages/http'
+import pgsql from 'highlight.js/lib/languages/pgsql'
+lowlight.registerLanguage('coffee', coffeescript)
+lowlight.registerLanguage('haskell', haskell)
+lowlight.registerLanguage('http', http)
+lowlight.registerLanguage('pgsql', pgsql)

I also think you’d also need:

-  assert.deepEqual(
+  assert.notDeepEqual(
     lowlight.listLanguages(),
     expectedLanguages,
-    'should return the same list of languages as highlight.js'
+    'should *not* return the same list of languages as highlight.js'
   )
 

The tests failing for this actually shows that everything works, so that’s great :)

Copy link
Owner

Choose a reason for hiding this comment

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

With this, I actually think it’s fine to not add highlight.js tests. Or mocking: so these changes you made to the tests.

But I am fine with an actual test, that checks that if a language is registered with lowlight, it still crashes when trying to use it with regular highlight.js?

Choose a reason for hiding this comment

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

And at this point there's no need to stick with the current hljs-like global API.

Curious why you felt that was a requirement before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meaning I need to think about how this should look.
And I'm AFK a bit these weeks!

Thanks for the response @wooorm. Let me know if I can help out somehow ✌️

Copy link
Owner

Choose a reason for hiding this comment

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

@joshgoebel because this was created years ago, when hljs supported much less, no emitters for example. And global. So it was a subclass. Now that can change. Perhaps a similar API to https://github.com/wooorm/starry-night. The flagToScope part particularly might be nice to have, which is a bit involved, I’d need some mapping between GH linguist and HLJS grammars

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meaning I need to think about how this should look. And I'm AFK a bit these weeks!

@wooorm did you already have time to think about the options? Let me know if I can support somehow ✌️

Copy link
Owner

Choose a reason for hiding this comment

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

Haven’t found the time yet, I’m busy with major updates through the ±500 projects maintained in the unified collective.
That being said, in a couple days I’ll reach https://github.com/rehypejs/rehype-highlight, which uses this, at which point I’d like to have this change in it. So I’ll come up with something.

@wooorm wooorm changed the title Fix to not use the global highlight instance Remove use of global highlight instance Sep 4, 2023
@wooorm wooorm merged commit 8d2645d into wooorm:main Sep 4, 2023
@wooorm
Copy link
Owner

wooorm commented Sep 4, 2023

released in 3.0.0, thanks for your work and patience!

@dschwank dschwank deleted the fix/use-highlight-instance branch September 5, 2023 05:36
@dschwank
Copy link
Contributor Author

dschwank commented Sep 5, 2023

@wooorm Thanks a lot for merging and creating a new release!

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.

Running highlight.js after using lowlight results in blank output from highlight.js
3 participants