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: check and apply markdown for smart picker output #5106

Merged
merged 1 commit into from
Dec 28, 2023
Merged
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
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