From b7d3de0a38a8e5f4e5d9aa98e74690bc893edb8e Mon Sep 17 00:00:00 2001 From: Luka Trovic Date: Fri, 8 Dec 2023 15:31:24 +0100 Subject: [PATCH] feat: check and apply markdown for smart picker output Signed-off-by: Luka Trovic --- .../Suggestion/LinkPicker/suggestions.js | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/components/Suggestion/LinkPicker/suggestions.js b/src/components/Suggestion/LinkPicker/suggestions.js index 8cf589a889a..702ea3144b5 100644 --- a/src/components/Suggestion/LinkPicker/suggestions.js +++ b/src/components/Suggestion/LinkPicker/suggestions.js @@ -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') @@ -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), @@ -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 => {