Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2022-05-30 12:43:59 +0300
committerJulius Härtl <jus@bitgrid.net>2022-06-08 22:49:19 +0300
commit4daeb204d25163a31f53c6a5494aae4b324f7e8b (patch)
tree9424ca9f8e9cb4d332023bfbd3ae036f3d56c00d /src
parent17bf216e870280dc341869c248e49f74b8c3460b (diff)
Implement toMarkdown for hard break instead of replacing after markdown transformation
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'src')
-rw-r--r--src/EditorFactory.js3
-rw-r--r--src/extensions/HardBreak.js37
-rw-r--r--src/extensions/Markdown.js1
-rw-r--r--src/extensions/index.js2
-rw-r--r--src/tests/markdown.spec.js10
5 files changed, 50 insertions, 3 deletions
diff --git a/src/EditorFactory.js b/src/EditorFactory.js
index 0ff2b8160..e95cd4ff5 100644
--- a/src/EditorFactory.js
+++ b/src/EditorFactory.js
@@ -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'
@@ -53,7 +52,7 @@ import {
TaskItem,
Callout,
} from './nodes'
-import { Markdown, Emoji } from './extensions'
+import { HardBreak, Markdown, Emoji } from './extensions'
import { translate as t } from '@nextcloud/l10n'
import { listLanguages, registerLanguage } from 'lowlight/lib/core'
import { emojiSearch } from '@nextcloud/vue/dist/Functions/emoji'
diff --git a/src/extensions/HardBreak.js b/src/extensions/HardBreak.js
new file mode 100644
index 000000000..60c7518ee
--- /dev/null
+++ b/src/extensions/HardBreak.js
@@ -0,0 +1,37 @@
+/*
+ * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.net>
+ *
+ * @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
diff --git a/src/extensions/Markdown.js b/src/extensions/Markdown.js
index 7eae9c83a..c7faa5caf 100644
--- a/src/extensions/Markdown.js
+++ b/src/extensions/Markdown.js
@@ -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(']')
},
diff --git a/src/extensions/index.js b/src/extensions/index.js
index dfc84425f..67026b3da 100644
--- a/src/extensions/index.js
+++ b/src/extensions/index.js
@@ -21,6 +21,7 @@
*/
import Emoji from './Emoji'
+import HardBreak from './HardBreak'
import Keymap from './Keymap'
import UserColor from './UserColor'
import Collaboration from './Collaboration'
@@ -28,6 +29,7 @@ import Markdown from './Markdown'
export {
Emoji,
+ HardBreak,
Keymap,
UserColor,
Collaboration,
diff --git a/src/tests/markdown.spec.js b/src/tests/markdown.spec.js
index 2e862c13b..828bfd5c2 100644
--- a/src/tests/markdown.spec.js
+++ b/src/tests/markdown.spec.js
@@ -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__')
@@ -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)')
})