Skip to content

Commit

Permalink
Merge pull request #5106 from nextcloud/feature/support-markdown-in-t…
Browse files Browse the repository at this point in the history
…ext-templates
  • Loading branch information
juliusknorr authored Dec 28, 2023
2 parents b04fae1 + cb4ad16 commit 114b2d4
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)
/^#{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 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 114b2d4

Please sign in to comment.