From f9bea10a540bb40c39f506b6904086481d7ba29c Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 Jul 2026 21:48:03 +0200 Subject: [PATCH] chore(refactor): aviod extending serializer options Signed-off-by: Max --- src/extensions/Markdown.ts | 29 ++++++++++++++++++++++++++--- src/marks/Link.ts | 21 --------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/extensions/Markdown.ts b/src/extensions/Markdown.ts index e52f96d46fe..4c676afdc31 100644 --- a/src/extensions/Markdown.ts +++ b/src/extensions/Markdown.ts @@ -30,8 +30,10 @@ import type { MarkdownSerializerState } from 'prosemirror-markdown' import { Extension, getExtensionField } from '@tiptap/core' import { DOMParser } from '@tiptap/pm/model' import { Plugin, PluginKey } from '@tiptap/pm/state' +import { normalizeReference } from 'markdown-it/lib/common/utils.mjs' import { defaultMarkdownSerializer, MarkdownSerializer } from 'prosemirror-markdown' import markdownit from '../markdownit/index.js' +import Link from '../marks/Link.ts' import transformPastedHTML from './transformPastedHTML.ts' // taken from prosemirror-markdown @@ -191,13 +193,34 @@ function createMarkdownSerializer(schema: { extractMarksToMarkdown(schema.marks), ), serialize(content: Node) { - // Extend serialize options to carry reference definitions (populated by `toMarkdown` callbacks in link mark) - const referenceDefinitions = new Map() const body = this.serializer.serialize(content, { tightLists: true, - referenceDefinitions, } as { tightLists: boolean }) + const referenceDefinitions = new Map() + content.descendants((node: Node) => { + const link = node.marks.find((m: Mark) => m.type.name === Link.name) + if (!link) { + return + } + const { href, referenceLabel: label, referenceType: type, title } = link.attrs as { + href: string + referenceLabel?: string + referenceType?: string + title?: string + } + if (label && type) { + const key = normalizeReference(label) + if (!referenceDefinitions.has(key)) { + referenceDefinitions.set(key, { + label, + href, + title: title ?? null, + }) + } + } + return false // do not descend into nodes that already have a link mark. + }) if (referenceDefinitions.size === 0) { return body } diff --git a/src/marks/Link.ts b/src/marks/Link.ts index aad930534cb..abb0b91da61 100644 --- a/src/marks/Link.ts +++ b/src/marks/Link.ts @@ -10,7 +10,6 @@ import type { MarkdownSerializerState } from 'prosemirror-markdown' import { getMarkRange, isMarkActive, markInputRule } from '@tiptap/core' import TipTapLink, { isAllowedUri } from '@tiptap/extension-link' -import { normalizeReference } from 'markdown-it/lib/common/utils.mjs' import { defaultMarkdownSerializer } from 'prosemirror-markdown' import { domHref, parseHref } from '../helpers/links.js' import { logger } from '../helpers/logger.ts' @@ -317,26 +316,6 @@ const Link = TipTapLink.extend({ | 'shortcut' | 'collapsed' | 'full' - const defs - // Cast `state.options` locally as `referenceDefinitions` doesn't exist in upstream type definition - = ( - state.options as { - referenceDefinitions?: Map< - string, - { label: string, href: string, title: string | null } - > - } - ).referenceDefinitions - if (defs) { - const key = normalizeReference(label) - if (!defs.has(key)) { - defs.set(key, { - label, - href: mark.attrs.href as string, - title: (mark.attrs.title as string) ?? null, - }) - } - } switch (type) { case 'shortcut': return ']'