Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166// 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<string, unknown>) => (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<string, unknown>) => ({ '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<string, unknown>) => ({ '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<string, unknown>) => ({ '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';