Skip to content

Commit 8391675

Browse files
feat: render text inserted from assistant
Signed-off-by: Luka Trovic <[email protected]>
1 parent 0a2b5bf commit 8391675

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/components/Assistant.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ import {
133133
} from './Editor.provider.js'
134134
import { FloatingMenu } from '@tiptap/vue-2'
135135
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
136+
import hasMarkdownSyntax from '../markdownit/hasMarkdownSyntax.js'
137+
import isValidMarkdown from '../markdownit/isValidMarkdown.js'
138+
import markdownit from '../markdownit/index.js'
136139
137140
const limitInRange = (num, min, max) => {
138141
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
@@ -302,7 +305,9 @@ export default {
302305
})
303306
},
304307
async insertResult(task) {
305-
this.$editor.commands.insertContent(task.output.output)
308+
const isMarkdown = hasMarkdownSyntax(task.output.output) && isValidMarkdown(task.output.output)
309+
const content = isMarkdown ? markdownit.render(task.output.output) : task.output.output
310+
this.$editor.commands.insertContent(content)
306311
this.showTaskList = false
307312
},
308313
async copyResult(task) {

src/components/Suggestion/LinkPicker/suggestions.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { searchProvider, getLinkWithPicker } from '@nextcloud/vue/dist/Component
99
import menuEntries from './../../Menu/entries.js'
1010
import { getIsActive } from '../../Menu/utils.js'
1111
import markdownit from '../../../markdownit/index.js'
12+
import hasMarkdownSyntax from '../../../markdownit/hasMarkdownSyntax.js'
13+
import isValidMarkdown from '../../../markdownit/isValidMarkdown.js'
1214

1315
const suggestGroupFormat = t('text', 'Formatting')
1416
const suggestGroupPicker = t('text', 'Smart picker')
@@ -19,31 +21,6 @@ const filterOut = (e) => {
1921

2022
const important = ['task-list', 'table']
2123

22-
const hasMarkdownSyntax = (content) => {
23-
// Regular expressions for common Markdown patterns
24-
const markdownPatterns = [
25-
/\*\*.*?\*\*/, // Bold: **text**
26-
/\*.*?\*/, // Italics: *text*
27-
/\[.*?\(.*?\)/, // Links: [text](url)
28-
/^#{1,6}\s.*$/, // Headings: # text
29-
/^\s*[-+*]\s.*/m, // Unordered list: - item
30-
/^\s\d\..*/m, // Ordered list: 1. item
31-
/^>+\s.*/, // Blockquote: > text
32-
/`.*?`/, // Code: `code`
33-
]
34-
35-
return markdownPatterns.some(pattern => pattern.test(content))
36-
}
37-
38-
const isValidMarkdown = (content) => {
39-
try {
40-
markdownit.parse(content)
41-
return true
42-
} catch (e) {
43-
return false
44-
}
45-
}
46-
4724
const isValidUrl = (url) => {
4825
try {
4926
return Boolean(new URL(url))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
/**
7+
* Check if the content has Markdown syntax
8+
*
9+
* @param {string} content Markdown object
10+
*/
11+
export default function hasMarkdownSyntax(content) {
12+
// Regular expressions for common Markdown patterns
13+
const markdownPatterns = [
14+
/\*\*.*?\*\*/, // Bold: **text**
15+
/\*.*?\*/, // Italics: *text*
16+
/\[.*?\(.*?\)/, // Links: [text](url)
17+
/^#{1,6}\s.*$/, // Headings: # text
18+
/^\s*[-+*]\s.*/m, // Unordered list: - item
19+
/^\s\d\..*/m, // Ordered list: 1. item
20+
/^>+\s.*/, // Blockquote: > text
21+
/`.*?`/, // Code: `code`
22+
]
23+
24+
return markdownPatterns.some(pattern => pattern.test(content))
25+
}

src/markdownit/isValidMarkdown.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import markdownit from './index.js'
6+
7+
/**
8+
* Check if the content is valid Markdown syntax
9+
*
10+
* @param {string} content Markdown object
11+
*/
12+
export default function isValidMarkdown(content) {
13+
try {
14+
markdownit.parse(content)
15+
return true
16+
} catch (e) {
17+
return false
18+
}
19+
}

0 commit comments

Comments
 (0)