Skip to content

Commit

Permalink
feat: check and apply markdown for smart picker output
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Dec 8, 2023
1 parent f2352f5 commit b15266c
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/components/Suggestion/LinkPicker/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import LinkPickerList from './LinkPickerList.vue'
import { searchProvider, getLinkWithPicker } from '@nextcloud/vue/dist/Components/NcRichText.js'
import menuEntries from './../../Menu/entries.js'
import { getIsActive } from '../../Menu/utils.js'
import markdownit from '../../../markdownit/index.js'

const suggestGroupFormat = t('text', 'Formatting')
const suggestGroupPicker = t('text', 'Smart picker')
Expand All @@ -34,6 +35,31 @@ 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)
/^#\s.*$/, // Headings: # text
/- .*/, // Unordered list: - item
/\d\..*/, // 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 sortImportantFirst = (list) => {
return [
...list.filter(e => important.indexOf(e.key) > -1),
Expand Down Expand Up @@ -67,10 +93,16 @@ export default () => createSuggestions({
}
getLinkWithPicker(props.providerId, true)
.then(link => {
let content = link

if (hasMarkdownSyntax(content) && isValidMarkdown(content)) {
content = markdownit.render(content)
}

editor
.chain()
.focus()
.insertContentAt(range, link + ' ')
.insertContentAt(range, content + ' ')
.run()
})
.catch(error => {
Expand Down

0 comments on commit b15266c

Please sign in to comment.