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

Fix: code-block-lowlight child extensions do not highlight code #1917

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
return [
...this.parent?.() || [],
LowlightPlugin({
name: 'codeBlock',
name: this.name,
lowlight: this.options.lowlight,
}),
]
Expand Down
80 changes: 80 additions & 0 deletions tests/cypress/integration/extensions/codeBlockLowlight.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// <reference types="cypress" />

import {
CodeBlockLowlight,
} from '@tiptap/extension-code-block-lowlight'
import { Text } from '@tiptap/extension-text'
import { Paragraph } from '@tiptap/extension-paragraph'
import { Document } from '@tiptap/extension-document'
import { Editor } from '@tiptap/core'
import * as lowlight from 'lowlight'

describe('code block highlight', () => {
let Frontmatter
const editorElClass = 'tiptap'
let editor: Editor

const createEditorEl = () => {
const editorEl = document.createElement('div')
editorEl.classList.add(editorElClass)

document.body.appendChild(editorEl)

return editorEl
}
const getEditorEl = () => document.querySelector(`.${editorElClass}`)

beforeEach(() => {
Frontmatter = CodeBlockLowlight
.configure({ lowlight })
.extend({
name: 'frontmatter',
})

editor = new Editor({
element: createEditorEl(),
extensions: [
Document,
Text,
Paragraph,
CodeBlockLowlight,
Frontmatter,
],
content: {
type: 'doc',
content: [
{
type: 'codeBlock',
attrs: {
language: 'javascript',
},
content: [{
type: 'text',
text: 'alert("Hello world");',
}],
},
{
type: 'frontmatter',
attrs: {
language: 'yaml',
},
content: [{
type: 'text',
text: '---\ntitle: Page title\n---',
}],
},
],
},
})
})

afterEach(() => {
editor.destroy()
getEditorEl()?.remove()
})

it('executes lowlight plugin in extensions that inherit from code-block-lowlight', () => {
expect(getEditorEl()?.querySelector('.language-javascript .hljs-string')).not.to.eq(null)
expect(getEditorEl()?.querySelector('.language-yaml .hljs-string')).not.to.eq(null)
})
})