Sanitize and href-ify user input

This commit is contained in:
Sinclair Chen 2022-09-12 13:32:30 -07:00
parent 9f3cbcccf6
commit 1d257c9b02
2 changed files with 11 additions and 7 deletions

View File

@ -23,8 +23,15 @@ import { Link } from '@tiptap/extension-link'
import { Mention } from '@tiptap/extension-mention' import { Mention } from '@tiptap/extension-mention'
import Iframe from './tiptap-iframe' import Iframe from './tiptap-iframe'
import TiptapTweet from './tiptap-tweet-type' import TiptapTweet from './tiptap-tweet-type'
import { find } from 'linkifyjs'
import { uniq } from 'lodash' import { uniq } from 'lodash'
/** get first url in text. like "notion.so " -> "http://notion.so"; "notion" -> null */
export function getUrl(text: string) {
const results = find(text, 'url')
return results.length ? results[0].href : null
}
export function parseTags(text: string) { export function parseTags(text: string) {
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
const matches = (text.match(regex) || []).map((match) => const matches = (text.match(regex) || []).map((match) =>

View File

@ -37,6 +37,7 @@ import { Tooltip } from './tooltip'
import BoldIcon from 'web/lib/icons/bold-icon' import BoldIcon from 'web/lib/icons/bold-icon'
import ItalicIcon from 'web/lib/icons/italic-icon' import ItalicIcon from 'web/lib/icons/italic-icon'
import LinkIcon from 'web/lib/icons/link-icon' import LinkIcon from 'web/lib/icons/link-icon'
import { getUrl } from 'common/util/parse'
const DisplayImage = Image.configure({ const DisplayImage = Image.configure({
HTMLAttributes: { HTMLAttributes: {
@ -160,13 +161,9 @@ function FloatingMenu(props: { editor: Editor | null }) {
const isLink = editor.isActive('link') const isLink = editor.isActive('link')
const setLink = () => { const setLink = () => {
if (url) { const href = url && getUrl(url)
editor if (href) {
.chain() editor.chain().focus().extendMarkRange('link').setLink({ href }).run()
.focus()
.extendMarkRange('link')
.setLink({ href: url })
.run()
} }
} }