tiptap/packages/extension-code/src/index.ts
Philipp Kühn 0c9ce26c02 Revert "use global namespace"
This reverts commit 24c3a9abd3.

# Conflicts:
#	packages/core/src/Editor.ts
2020-11-16 16:58:30 +01:00

73 lines
1.1 KiB
TypeScript

import {
Command,
Mark,
markInputRule,
markPasteRule,
} from '@tiptap/core'
export interface CodeOptions {
HTMLAttributes: {
[key: string]: any
},
}
export const inputRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))$/gm
export const pasteRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))/gm
const Code = Mark.create({
name: 'code',
defaultOptions: <CodeOptions>{
HTMLAttributes: {},
},
excludes: '_',
parseHTML() {
return [
{ tag: 'code' },
]
},
renderHTML({ HTMLAttributes }) {
return ['code', HTMLAttributes, 0]
},
addCommands() {
return {
/**
* Toggle inline code
*/
code: (): Command => ({ commands }) => {
return commands.toggleMark('code')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-`': () => this.editor.commands.code(),
}
},
addInputRules() {
return [
markInputRule(inputRegex, this.type),
]
},
addPasteRules() {
return [
markPasteRule(inputRegex, this.type),
]
},
})
export default Code
declare module '@tiptap/core' {
interface AllExtensions {
Code: typeof Code,
}
}