Skip to content

Commit 06a08a5

Browse files
committed
Remove trailing newline from fenced code blocks
In some cases, `<pre><code>...\n</pre></code>` led to a trailing newline in the code block as rendered by Tiptap. Remove the trailing newline from HTML as serialized by markdown-it in order to avoid this. The other way round, while saving the file back, serializing to markdown removes trailing newlines from code blocks anyway, so we don't have to care about them. Fixes: #2344 Signed-off-by: Jonas <jonas@freesources.org>
1 parent 687e150 commit 06a08a5

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/markdownit/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ const markdownit = MarkdownIt('commonmark', { html: false, breaks: false })
1616
.use(keepSyntax)
1717
.use(markdownitMentions)
1818

19+
// Remove trailing newline from fenced code blocks (Github issue text#2344)
20+
const defaultFenceRule = markdownit.renderer.rules.fence
21+
markdownit.renderer.rules.fence = (...args) => {
22+
return defaultFenceRule(...args).replace(/\n<\/code><\/pre>/, '</code></pre>')
23+
}
24+
1925
export default markdownit

src/tests/markdownit.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ describe('markdownit', () => {
4747
})
4848
})
4949

50+
it('renderes fenced code blocks without trailing newline', () => {
51+
const rendered = markdownit.render('```\nsome\ncode\n```')
52+
expect(rendered).toBe('<pre><code>some\ncode</code></pre>\n')
53+
})
54+
5055
})
5156

5257
function stripIndent(content) {

0 commit comments

Comments
 (0)