Skip to content

Commit

Permalink
feat: render text inserted from assistant
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Nov 25, 2024
1 parent 0a2b5bf commit 8391675
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ import {
} from './Editor.provider.js'
import { FloatingMenu } from '@tiptap/vue-2'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import hasMarkdownSyntax from '../markdownit/hasMarkdownSyntax.js'
import isValidMarkdown from '../markdownit/isValidMarkdown.js'
import markdownit from '../markdownit/index.js'

const limitInRange = (num, min, max) => {
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
Expand Down Expand Up @@ -302,7 +305,9 @@ export default {
})
},
async insertResult(task) {
this.$editor.commands.insertContent(task.output.output)
const isMarkdown = hasMarkdownSyntax(task.output.output) && isValidMarkdown(task.output.output)
const content = isMarkdown ? markdownit.render(task.output.output) : task.output.output
this.$editor.commands.insertContent(content)
this.showTaskList = false
},
async copyResult(task) {
Expand Down
27 changes: 2 additions & 25 deletions src/components/Suggestion/LinkPicker/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { searchProvider, getLinkWithPicker } from '@nextcloud/vue/dist/Component
import menuEntries from './../../Menu/entries.js'
import { getIsActive } from '../../Menu/utils.js'
import markdownit from '../../../markdownit/index.js'
import hasMarkdownSyntax from '../../../markdownit/hasMarkdownSyntax.js'
import isValidMarkdown from '../../../markdownit/isValidMarkdown.js'

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

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

const hasMarkdownSyntax = (content) => {
// Regular expressions for common Markdown patterns
const markdownPatterns = [
/\*\*.*?\*\*/, // Bold: **text**
/\*.*?\*/, // Italics: *text*
/\[.*?\(.*?\)/, // Links: [text](url)
/^#{1,6}\s.*$/, // Headings: # text
/^\s*[-+*]\s.*/m, // Unordered list: - item
/^\s\d\..*/m, // Ordered list: 1. item
/^>+\s.*/, // Blockquote: > text
/`.*?`/, // Code: `code`
]

return markdownPatterns.some(pattern => pattern.test(content))
}

const isValidMarkdown = (content) => {
try {
markdownit.parse(content)
return true
} catch (e) {
return false
}
}

const isValidUrl = (url) => {
try {
return Boolean(new URL(url))
Expand Down
25 changes: 25 additions & 0 deletions src/markdownit/hasMarkdownSyntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/**
* Check if the content has Markdown syntax
*
* @param {string} content Markdown object
*/
export default function hasMarkdownSyntax(content) {
// Regular expressions for common Markdown patterns
const markdownPatterns = [
/\*\*.*?\*\*/, // Bold: **text**
/\*.*?\*/, // Italics: *text*
/\[.*?\(.*?\)/, // Links: [text](url)
/^#{1,6}\s.*$/, // Headings: # text
/^\s*[-+*]\s.*/m, // Unordered list: - item
/^\s\d\..*/m, // Ordered list: 1. item
/^>+\s.*/, // Blockquote: > text
/`.*?`/, // Code: `code`
]

return markdownPatterns.some(pattern => pattern.test(content))
}
19 changes: 19 additions & 0 deletions src/markdownit/isValidMarkdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import markdownit from './index.js'

/**
* Check if the content is valid Markdown syntax
*
* @param {string} content Markdown object
*/
export default function isValidMarkdown(content) {
try {
markdownit.parse(content)
return true
} catch (e) {
return false
}
}

0 comments on commit 8391675

Please sign in to comment.