Skip to content

Commit

Permalink
Merge pull request #6694 from nextcloud/render-text-inserted-from-ass…
Browse files Browse the repository at this point in the history
…istant

feat: render text inserted from assistant
  • Loading branch information
juliusknorr authored Nov 27, 2024
2 parents dd91caa + 7a4ab6f commit 3d43f81
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/components/Assistant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ import {
} from './Editor.provider.js'
import { FloatingMenu } from '@tiptap/vue-2'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import markdownit from '../markdownit/index.js'
import shouldInterpretAsMarkdown from '../markdownit/shouldInterpretAsMarkdown.js'
const limitInRange = (num, min, max) => {
return Math.min(Math.max(parseInt(num), parseInt(min)), parseInt(max))
Expand Down Expand Up @@ -302,7 +304,9 @@ export default {
})
},
async insertResult(task) {
this.$editor.commands.insertContent(task.output.output)
const isMarkdown = shouldInterpretAsMarkdown(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
28 changes: 2 additions & 26 deletions src/components/Suggestion/LinkPicker/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 shouldInterpretAsMarkdown from '../../../markdownit/shouldInterpretAsMarkdown.js'

const suggestGroupFormat = t('text', 'Formatting')
const suggestGroupPicker = t('text', 'Smart picker')
Expand All @@ -19,31 +20,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 Expand Up @@ -88,7 +64,7 @@ export default () => createSuggestions({
.then(link => {
const isUrl = isValidUrl(link)
if (!isUrl) {
const isMarkdown = hasMarkdownSyntax(link) && isValidMarkdown(link)
const isMarkdown = shouldInterpretAsMarkdown(link)
// Insert markdown content (e.g. from `text_templates` app)
const content = isMarkdown ? markdownit.render(link) : link
editor
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
}
}
16 changes: 16 additions & 0 deletions src/markdownit/shouldInterpretAsMarkdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import hasMarkdownSyntax from './hasMarkdownSyntax.js'
import isValidMarkdown from './isValidMarkdown.js'

/**
* Check if the content has Markdown syntax
*
* @param {string} content Markdown object
*/
export default function shouldInterpretAsMarkdown(content) {
return hasMarkdownSyntax(content) && isValidMarkdown(content)
}

0 comments on commit 3d43f81

Please sign in to comment.