Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement toMarkdown for hard break instead of replacing after markdo…
…wn transformation

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and max-nextcloud committed Jun 7, 2022
commit efb27a52f5aed9771cb5e4a019e8a3125dfb7d59
3 changes: 1 addition & 2 deletions src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import HardBreak from '@tiptap/extension-hard-break'
import History from '@tiptap/extension-history'
import Blockquote from '@tiptap/extension-blockquote'
import Placeholder from '@tiptap/extension-placeholder'
Expand Down Expand Up @@ -53,7 +52,7 @@ import {
TaskItem,
Callout,
} from './nodes/index.js'
import { Markdown, Emoji } from './extensions/index.js'
import { HardBreak, Markdown, Emoji } from './extensions/index.js'
import { translate as t } from '@nextcloud/l10n'
import { listLanguages, registerLanguage } from 'lowlight/lib/core'
import { emojiSearch } from '@nextcloud/vue/dist/Functions/emoji'
Expand Down
37 changes: 37 additions & 0 deletions src/extensions/HardBreak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import TipTapHardBreak from '@tiptap/extension-hard-break'

const HardBreak = TipTapHardBreak.extend({

toMarkdown(state, node, parent, index) {
for (let i = index + 1; i < parent.childCount; i++) {
if (parent.child(i).type !== node.type) {
state.write(' \n')
return
}
}
},
})

export default HardBreak
1 change: 0 additions & 1 deletion src/extensions/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const createMarkdownSerializer = ({ nodes, marks }) => {
),
serialize(content, options) {
return this.serializer.serialize(content, { ...options, tightLists: true })
.split('\\\n').join(' \n')
.split('\\[').join('[')
.split('\\]').join(']')
},
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
*/

import Emoji from './Emoji.js'
import HardBreak from './HardBreak.js'
import Keymap from './Keymap.js'
import UserColor from './UserColor.js'
import Collaboration from './Collaboration.js'
import Markdown from './Markdown.js'

export {
Emoji,
HardBreak,
Keymap,
UserColor,
Collaboration,
Expand Down
10 changes: 10 additions & 0 deletions src/tests/markdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe('Markdown though editor', () => {
expect(markdownThroughEditor('#### Test')).toBe('#### Test')
expect(markdownThroughEditor('##### Test')).toBe('##### Test')
})
test('hard breaks', () => {
expect(markdownThroughEditor('hard \nbreak')).toBe('hard \nbreak')
expect(markdownThroughEditor('hard\\\nbreak')).toBe('hard \nbreak')
expect(markdownThroughEditor('no\nbreak')).toBe('no break')
})
test('inline format', () => {
expect(markdownThroughEditor('**Test**')).toBe('**Test**')
expect(markdownThroughEditor('__Test__')).toBe('__Test__')
Expand Down Expand Up @@ -131,6 +136,11 @@ describe('Markdown serializer from html', () => {
test('paragraph', () => {
expect(markdownThroughEditorHtml('<p>hello</p><p>world</p>')).toBe('hello\n\nworld')
})
test('hard line breaks', () => {
expect(markdownThroughEditorHtml('<p>hard<br />break</p>')).toBe('hard \nbreak')
expect(markdownThroughEditorHtml('<p>hard<br>break</p>')).toBe('hard \nbreak')
expect(markdownThroughEditorHtml('<p>no\nbreak</p>')).toBe('no break')
})
test('links', () => {
expect(markdownThroughEditorHtml('<a href="foo">test</a>')).toBe('[test](foo)')
})
Expand Down