Skip to content

Commit

Permalink
Remove trailing newline from code blocks
Browse files Browse the repository at this point in the history
Markdown-it serialized code blocks to `<pre><code>...\n</pre></code>`,
which leads to trailing newlines in the code blocks as rendered by
 Tiptap.

Let the Tiptap CodeBlock node remove the trailing newline in parseHTML
in order to avoid this.

There's another issue with trailing newlines that got added to code
blocks on purpose being removed by prosemirror-markdown, but that's not
affected by this change.

Fixes: #2344

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Nov 2, 2022
1 parent ded6b61 commit e78f78f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/nodes/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import TiptapCodeBlock from '@tiptap/extension-code-block'
import { defaultMarkdownSerializer } from 'prosemirror-markdown'

const CodeBlock = TiptapCodeBlock.extend({
parseHTML() {
return [
{
tag: 'pre',
preserveWhitespace: 'full',
// Remove trailing newline from code blocks (Github issue #2344)
getContent: (node, schema) => {
return schema.nodes.codeBlock.create(null, [schema.text(node.textContent.replace(/\n$/, ''))])
},
},
]
},

toMarkdown(state, node, parent, index) {
// prosemirror-markdown uses `params` instead of `language` attribute
Expand Down

0 comments on commit e78f78f

Please sign in to comment.