fix(code-block): respect defaultLanguage on code-block-lowlight add option to code-block (#5406)

This commit is contained in:
Nick Perez 2024-08-06 12:25:01 +02:00 committed by GitHub
parent 174aefe0f1
commit 4b215f794e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 9 deletions

View File

@ -0,0 +1,6 @@
---
"@tiptap/extension-code-block": minor
"@tiptap/extension-code-block-lowlight": patch
---
`defaultLanguage` on Code Block Lowlight was not being respected properly, to address this we added `defaultLanguage` as an option to the code-block extension.

View File

@ -7,13 +7,6 @@ export interface CodeBlockLowlightOptions extends CodeBlockOptions {
* The lowlight instance.
*/
lowlight: any,
/**
* The default language.
* @default null
* @example 'javascript'
*/
defaultLanguage: string | null | undefined,
}
/**
@ -25,7 +18,6 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
return {
...this.parent?.(),
lowlight: {},
defaultLanguage: null,
}
},

View File

@ -22,6 +22,12 @@ export interface CodeBlockOptions {
* @default true
*/
exitOnArrowDown: boolean
/**
* The default language.
* @default null
* @example 'js'
*/
defaultLanguage: string | null | undefined
/**
* Custom HTML attributes that should be added to the rendered HTML tag.
* @default {}
@ -71,6 +77,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
languageClassPrefix: 'language-',
exitOnTripleEnter: true,
exitOnArrowDown: true,
defaultLanguage: null,
HTMLAttributes: {},
}
},
@ -88,7 +95,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
addAttributes() {
return {
language: {
default: null,
default: this.options.defaultLanguage,
parseHTML: element => {
const { languageClassPrefix } = this.options
const classNames = [...(element.firstElementChild?.classList || [])]