File size: 4,097 Bytes
2daf8de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import type { InputRuleMatch, PasteRuleMatch } from '@tiptap/core'
import { Mark, markInputRule, markPasteRule, mergeAttributes } from '@tiptap/core'
export interface CodeOptions {
/**
* The HTML attributes applied to the code element.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
code: {
/**
* Set a code mark
*/
setCode: () => ReturnType
/**
* Toggle inline code
*/
toggleCode: () => ReturnType
/**
* Unset a code mark
*/
unsetCode: () => ReturnType
}
}
}
/**
* The regular expression used for the inline code input rule.
* @deprecated The extension now uses a function-based finder internally.
* This regex is kept for backward compatibility.
*/
export const inputRegex = /(^|[^`])`([^`]+)`(?!`)$/
/**
* The regular expression used for the inline code paste rule.
* @deprecated The extension now uses a function-based finder internally.
* This regex is kept for backward compatibility.
*/
export const pasteRegex = /(^|[^`])`([^`]+)`(?!`)/g
/**
* A function-based finder for the inline code input rule.
* Used internally by the extension to ensure the preceding character
* is not consumed as part of the match, preventing it from being deleted.
*/
export const inputRegexMatch = (text: string): InputRuleMatch | null => {
const match = /`([^`]+)`(?!`)$/.exec(text)
if (!match) {
return null
}
// Ensure the opening backtick isn't preceded by another backtick
if (match.index > 0 && text[match.index - 1] === '`') {
return null
}
return {
index: match.index,
text: match[0],
replaceWith: match[1],
}
}
/**
* A function-based finder for the inline code paste rule.
* Used internally by the extension to avoid consuming the preceding
* character as part of the match.
*/
export const pasteRegexMatch = (text: string): PasteRuleMatch[] => {
const regex = /`([^`]+)`(?!`)/g
const matches: PasteRuleMatch[] = []
let match: RegExpExecArray | null
while ((match = regex.exec(text)) !== null) {
// Ensure the opening backtick isn't preceded by another backtick
if (match.index > 0 && text[match.index - 1] === '`') {
continue
}
matches.push({
index: match.index,
text: match[0],
replaceWith: match[1],
})
}
return matches
}
/**
* This extension allows you to mark text as inline code.
* @see https://tiptap.dev/api/marks/code
*/
export const Code = Mark.create<CodeOptions>({
name: 'code',
addOptions() {
return {
HTMLAttributes: {},
}
},
excludes: '_',
code: true,
exitable: true,
parseHTML() {
return [{ tag: 'code' }]
},
renderHTML({ HTMLAttributes }) {
return ['code', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
markdownTokenName: 'codespan',
parseMarkdown: (token, helpers) => {
// Convert 'codespan' token to code mark
// For codespan tokens, we use the raw text content, not tokens
return helpers.applyMark('code', [{ type: 'text', text: token.text || '' }])
},
renderMarkdown: (node, h) => {
if (!node.content) {
return ''
}
return `\`${h.renderChildren(node.content)}\``
},
addCommands() {
return {
setCode:
() =>
({ commands }) => {
return commands.setMark(this.name)
},
toggleCode:
() =>
({ commands }) => {
return commands.toggleMark(this.name)
},
unsetCode:
() =>
({ commands }) => {
return commands.unsetMark(this.name)
},
}
},
addKeyboardShortcuts() {
return {
'Mod-e': () => this.editor.commands.toggleCode(),
}
},
addInputRules() {
return [
markInputRule({
find: inputRegexMatch,
type: this.type,
}),
]
},
addPasteRules() {
return [
markPasteRule({
find: pasteRegexMatch,
type: this.type,
}),
]
},
})
|