Skip to content

Commit

Permalink
Remove use of global highlight instance
Browse files Browse the repository at this point in the history
Closes GH-51.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
dschwank committed Sep 4, 2023
1 parent 5caa245 commit 8d2645d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
7 changes: 4 additions & 3 deletions lib/core.js
Expand Up @@ -20,9 +20,12 @@
* @typedef {Options & ExtraOptions} AutoOptions
*/

import high from 'highlight.js/lib/core'
import highlightCore from 'highlight.js/lib/core'
import {fault} from 'fault'

// Create an own instance to not be in conflict with the global object
const high = highlightCore.newInstance()

const own = {}.hasOwnProperty

const defaultPrefix = 'hljs-'
Expand Down Expand Up @@ -66,8 +69,6 @@ function highlight(language, value, options = {}) {
high.highlight(value, {language, ignoreIllegals: true})
)

high.configure({})

// `highlight.js` seems to use this (currently) for broken grammars, so let’s
// keep it in there just to be sure.
/* c8 ignore next 3 */
Expand Down
38 changes: 29 additions & 9 deletions test/index.js
Expand Up @@ -5,16 +5,31 @@
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import process from 'node:process'
import test from 'node:test'
import highlight from 'highlight.js'
import test, {beforeEach, mock} from 'node:test'
import highlight from 'highlight.js/lib/core'
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'
import {rehype} from 'rehype'
import {removePosition} from 'unist-util-remove-position'
import {lowlight} from '../index.js'

// Register test language which are not covered by lib/common.js
lowlight.registerLanguage('coffee', coffeescript)
lowlight.registerLanguage('haskell', haskell)
lowlight.registerLanguage('http', http)
lowlight.registerLanguage('pgsql', pgsql)

/* eslint-disable no-await-in-loop */

const fixtures = new URL('fixture/', import.meta.url)

beforeEach(() => {
mock.reset()
mock.method(highlight, 'configure')
})

test('lowlight.highlight(language, value[, options])', async (t) => {
const result = lowlight.highlight('js', '')

Expand Down Expand Up @@ -44,6 +59,13 @@ test('lowlight.highlight(language, value[, options])', async (t) => {
'should throw when given an unknown `language`'
)

assert.equal(
// @ts-expect-error mocked function
highlight.configure.mock.calls.length,
0,
'should not call the global configure method'
)

assert.equal(
result.data.relevance,
0,
Expand Down Expand Up @@ -294,22 +316,20 @@ test('fixtures', async () => {
})

test('listLanguages', () => {
const expectedLanguages = highlight.listLanguages()
const mockName = 'testtest'

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

lowlight.registerLanguage(mockName, mockSyntax)

assert.ok(
lowlight.listLanguages().includes(mockName),
'should include any additional languages that are registered'
)

assert.ok(
!highlight.listLanguages().includes(mockName),
'should *not* include any additional languages that are registered at lowlight'
)

function mockSyntax() {
return {contains: []}
}
Expand Down

0 comments on commit 8d2645d

Please sign in to comment.