-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathhelpers.js
More file actions
71 lines (64 loc) · 1.91 KB
/
Copy pathhelpers.js
File metadata and controls
71 lines (64 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { createMarkdownSerializer } from '../extensions/Markdown.js'
import { Editor } from '@tiptap/core'
import { Document } from '@tiptap/extension-document'
import { Text } from '@tiptap/extension-text'
import Paragraph from '../nodes/Paragraph.js'
import { createRichEditor } from '../EditorFactory.js'
import markdownit from '../markdownit/index.js'
/**
*
* @param {object} options for the editor
* @param {string} options.content Content for the editor.
* @param {Array} options.extensions Tip tap extensions
*/
export function createCustomEditor({ content, extensions }) {
return new Editor({
content,
extensions: [
Document,
Paragraph,
Text,
...extensions,
],
})
}
/**
* Ease markdown through TipTap editor and return serialized markdown
*
* @param {string} markdown to process
* @return {string}
*/
export function markdownThroughEditor(markdown) {
const tiptap = createRichEditor()
tiptap.commands.setContent(markdownit.render(markdown))
const serializer = createMarkdownSerializer(tiptap.schema)
return serializer.serialize(tiptap.state.doc)
}
/**
* Ease HTML as input through the Editor and return the serialized markdown
*
* @param {string} html to process
* @return {string}
*/
export function markdownThroughEditorHtml(html) {
const tiptap = createRichEditor()
tiptap.commands.setContent(html)
const serializer = createMarkdownSerializer(tiptap.schema)
return serializer.serialize(tiptap.state.doc)
}
/**
* Paste HTML into the Editor and return the serialized markdown
*
* @param {string} html to paste
* @return {string}
*/
export function markdownFromPaste(html) {
const tiptap = createRichEditor()
tiptap.commands.insertContent(html)
const serializer = createMarkdownSerializer(tiptap.schema)
return serializer.serialize(tiptap.state.doc)
}