Last active
October 12, 2022 14:14
-
-
Save cadars/78a6c96eac8faf3b11feda3d6ad033e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'; | |
export const kbdInputRegex = /(?:^|\s)((?:<kbd>)((?:[^*]+))(?:<\/kbd>))$/; | |
export const kbdPasteRegex = /(?:^|\s)((?:<kbd>)((?:[^*]+))(?:<\/kbd>))/g; | |
export const Kbd = Mark.create({ | |
name: 'kbd', | |
excludes: '_', | |
inclusive: false, | |
parseHTML() { | |
return [ | |
{ | |
tag: 'kbd', | |
}, | |
]; | |
}, | |
renderHTML() { | |
return ['kbd']; | |
}, | |
addCommands() { | |
return { | |
setKbd: | |
() => | |
({ commands }) => { | |
return commands.setMark('kbd'); | |
}, | |
toggleKbd: | |
() => | |
({ commands }) => { | |
return commands.toggleMark('kbd'); | |
}, | |
unsetKbd: | |
() => | |
({ commands }) => { | |
return commands.unsetMark('kbd'); | |
}, | |
}; | |
}, | |
addKeyboardShortcuts() { | |
return { | |
'Mod-^': () => this.editor.commands.toggleKbd(), | |
}; | |
}, | |
addInputRules() { | |
return [ | |
markInputRule({ | |
find: kbdInputRegex, | |
type: this.type, | |
}), | |
]; | |
}, | |
addPasteRules() { | |
return [ | |
markPasteRule({ | |
find: kbdPasteRegex, | |
type: this.type, | |
}), | |
]; | |
}, | |
}); | |
export default Kbd; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment