// Inline semantics beyond bold/italic/code: definitions, references to // definitions (local or in an imported spec), bibliographic references, // comments, and the three marks that carry suggested changes. import { Mark, markInputRule, mergeAttributes } from '@tiptap/core'; const attr = (name: string, dom: string) => ({ default: null as string | null, parseHTML: (el: HTMLElement) => el.getAttribute(dom), renderHTML: (a: Record) => (a[name] ? { [dom]: a[name] } : {}), }); export const Dfn = Mark.create({ name: 'dfn', excludes: 'dfnRef ref', addAttributes() { return { id: attr('id', 'data-id') }; }, parseHTML() { return [{ tag: 'dfn' }]; }, renderHTML({ HTMLAttributes }) { return ['dfn', mergeAttributes(HTMLAttributes), 0]; }, addKeyboardShortcuts() { return { 'Mod-d': () => this.editor.commands.toggleMark(this.name) }; }, }); export const DfnRef = Mark.create({ name: 'dfnRef', excludes: 'dfn ref', addAttributes() { return { id: attr('id', 'data-id'), // a term from an imported reference: its citation key and the definition's URL ref: attr('ref', 'data-ref'), uri: attr('uri', 'data-uri'), }; }, parseHTML() { return [{ tag: 'a.dfn-ref' }]; }, renderHTML({ HTMLAttributes, mark }) { return ['a', mergeAttributes(HTMLAttributes, { class: mark.attrs.uri ? 'dfn-ref external' : 'dfn-ref' }), 0]; }, addKeyboardShortcuts() { return { 'Mod-Shift-d': () => this.editor.commands.toggleMark(this.name) }; }, }); export const Ref = Mark.create({ name: 'ref', excludes: 'dfn dfnRef link', addAttributes() { return { key: { default: '', parseHTML: (el: HTMLElement) => el.getAttribute('data-key') ?? el.textContent, renderHTML: (a: Record) => ({ 'data-key': a.key }) }, uri: attr('uri', 'data-uri'), title: attr('title', 'data-title'), }; }, parseHTML() { return [{ tag: 'a.ref' }]; }, renderHTML({ HTMLAttributes }) { return ['a', mergeAttributes(HTMLAttributes, { class: 'ref' }), 0]; }, addInputRules() { // [key] → a reference return [markInputRule({ find: /\[([^[\]\s]+)\]$/, type: this.type, getAttributes: (m) => ({ key: m[1] }) })]; }, }); /** A comment thread attached to a range of text. Overlaps are fine. */ export const Comment = Mark.create({ name: 'comment', excludes: '', inclusive: false, addAttributes() { return { id: attr('id', 'data-comment') }; }, parseHTML() { return [{ tag: 'mark[data-comment]' }]; }, renderHTML({ HTMLAttributes }) { return ['mark', mergeAttributes(HTMLAttributes, { class: 'comment' }), 0]; }, }); // --- suggested changes ---------------------------------------------------------- // The same shapes as @handlewithcare/prosemirror-suggest-changes' mark specs // (it finds them by name in the schema), expressed as TipTap marks so they // take part in the extension pipeline. Block-level ones wrap a block's DOM; // the stylesheet displays those as blocks. const idAttr = { id: { default: null as string | number | null, parseHTML: (el: HTMLElement) => { const raw = el.getAttribute('data-id'); if (raw === null) return null; try { return JSON.parse(raw) as string | number; } catch { return raw; } }, renderHTML: (a: Record) => ({ 'data-id': JSON.stringify(a.id) }), }, }; export const Insertion = Mark.create({ name: 'insertion', inclusive: false, excludes: 'deletion modification insertion', addAttributes() { return idAttr; }, parseHTML() { return [{ tag: 'ins[data-id]' }]; }, renderHTML({ HTMLAttributes }) { return ['ins', mergeAttributes(HTMLAttributes), 0]; }, }); export const Deletion = Mark.create({ name: 'deletion', inclusive: false, excludes: 'insertion modification deletion', addAttributes() { return idAttr; }, parseHTML() { return [{ tag: 'del[data-id]' }]; }, renderHTML({ HTMLAttributes }) { return ['del', mergeAttributes(HTMLAttributes), 0]; }, }); export const Modification = Mark.create({ name: 'modification', inclusive: false, excludes: 'deletion insertion', addAttributes() { return { ...idAttr, type: { default: 'mark', parseHTML: (el: HTMLElement) => el.getAttribute('data-mod-type') ?? 'mark', renderHTML: (a: Record) => ({ 'data-mod-type': a.type }) }, attrName: { default: null, renderHTML: () => ({}) }, previousValue: { default: null, renderHTML: () => ({}) }, newValue: { default: null, renderHTML: () => ({}) }, }; }, parseHTML() { return [{ tag: 'span[data-type="modification"]' }]; }, renderHTML({ HTMLAttributes }) { return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'modification' }), 0]; }, }); /** The marks block containers must allow on their children for block-level suggestions. */ export const BLOCK_MARKS = 'insertion deletion modification'; /** The marks that may sit on any text: formatting plus the suggestion and comment marks. */ export const SIMPLE_MARKS = 'bold italic code subscript superscript insertion deletion modification comment';