From 4b215f794e13b81780395d500952b27d35995456 Mon Sep 17 00:00:00 2001 From: Nick Perez Date: Tue, 6 Aug 2024 12:25:01 +0200 Subject: [PATCH] fix(code-block): respect `defaultLanguage` on code-block-lowlight add option to `code-block` (#5406) --- .changeset/good-schools-pretend.md | 6 ++++++ .../src/code-block-lowlight.ts | 8 -------- packages/extension-code-block/src/code-block.ts | 9 ++++++++- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 .changeset/good-schools-pretend.md diff --git a/.changeset/good-schools-pretend.md b/.changeset/good-schools-pretend.md new file mode 100644 index 000000000..6d7244219 --- /dev/null +++ b/.changeset/good-schools-pretend.md @@ -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. diff --git a/packages/extension-code-block-lowlight/src/code-block-lowlight.ts b/packages/extension-code-block-lowlight/src/code-block-lowlight.ts index c1eb4dcbf..3b952e329 100644 --- a/packages/extension-code-block-lowlight/src/code-block-lowlight.ts +++ b/packages/extension-code-block-lowlight/src/code-block-lowlight.ts @@ -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({ return { ...this.parent?.(), lowlight: {}, - defaultLanguage: null, } }, diff --git a/packages/extension-code-block/src/code-block.ts b/packages/extension-code-block/src/code-block.ts index 8d6b22d4c..122e476b4 100644 --- a/packages/extension-code-block/src/code-block.ts +++ b/packages/extension-code-block/src/code-block.ts @@ -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({ languageClassPrefix: 'language-', exitOnTripleEnter: true, exitOnArrowDown: true, + defaultLanguage: null, HTMLAttributes: {}, } }, @@ -88,7 +95,7 @@ export const CodeBlock = Node.create({ addAttributes() { return { language: { - default: null, + default: this.options.defaultLanguage, parseHTML: element => { const { languageClassPrefix } = this.options const classNames = [...(element.firstElementChild?.classList || [])]