This commit is contained in:
Jon Staab
2024-08-26 14:43:43 -07:00
parent 644c32dd09
commit 88318e9753
36 changed files with 370 additions and 311 deletions
+18 -17
View File
@@ -1,41 +1,42 @@
import { Node, nodePasteRule, type PasteRuleMatch } from '@tiptap/core'
import type { Node as ProsemirrorNode } from '@tiptap/pm/model'
import type { MarkdownSerializerState } from 'prosemirror-markdown'
import {Node, nodePasteRule, type PasteRuleMatch} from "@tiptap/core"
import type {Node as ProsemirrorNode} from "@tiptap/pm/model"
import type {MarkdownSerializerState} from "prosemirror-markdown"
export const LINK_REGEX = /^([a-z\+:]{2,30}:\/\/)?[^<>\(\)\s]+\.[a-z]{2,6}[^\s]*[^<>"'\.!?,:\s\)\(]*/gi
export const LINK_REGEX =
/^([a-z\+:]{2,30}:\/\/)?[^<>\(\)\s]+\.[a-z]{2,6}[^\s]*[^<>"'\.!?,:\s\)\(]*/gi
export const createPasteRuleMatch = <T extends Record<string, unknown>>(
match: RegExpMatchArray,
data: T,
): PasteRuleMatch => ({ index: match.index!, replaceWith: match[2], text: match[0], match, data })
): PasteRuleMatch => ({index: match.index!, replaceWith: match[2], text: match[0], match, data})
export interface LinkAttributes {
url: string
}
declare module '@tiptap/core' {
declare module "@tiptap/core" {
interface Commands<ReturnType> {
link: {
insertLink: (options: { url: string }) => ReturnType
insertLink: (options: {url: string}) => ReturnType
}
}
}
export const LinkExtension = Node.create({
atom: true,
name: 'link',
group: 'inline',
name: "link",
group: "inline",
inline: true,
selectable: true,
draggable: true,
priority: 1000,
addAttributes() {
return {
url: { default: null },
url: {default: null},
}
},
renderHTML(props) {
return ['div', { 'data-url': props.node.attrs.url }]
return ["div", {"data-url": props.node.attrs.url}]
},
renderText(props) {
return props.node.attrs.url
@@ -53,10 +54,10 @@ export const LinkExtension = Node.create({
addCommands() {
return {
insertLink:
({ url }) =>
({ commands }) => {
({url}) =>
({commands}) => {
return commands.insertContent(
{ type: this.name, attrs: { url } },
{type: this.name, attrs: {url}},
{
updateSelection: false,
},
@@ -68,13 +69,13 @@ export const LinkExtension = Node.create({
return [
nodePasteRule({
type: this.type,
getAttributes: (match) => match.data,
find: (text) => {
getAttributes: match => match.data,
find: text => {
const matches = []
for (const match of text.matchAll(LINK_REGEX)) {
try {
matches.push(createPasteRuleMatch(match, { url: match[0] }))
matches.push(createPasteRuleMatch(match, {url: match[0]}))
} catch (e) {
continue
}