Skip to content

Instantly share code, notes, and snippets.

@cadars
Last active October 12, 2022 14:14
Show Gist options
  • Save cadars/78a6c96eac8faf3b11feda3d6ad033e3 to your computer and use it in GitHub Desktop.
Save cadars/78a6c96eac8faf3b11feda3d6ad033e3 to your computer and use it in GitHub Desktop.
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