Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: render text inserted from assistant #6694

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading