Fix code inline space handling

This commit is contained in:
Jon Staab
2025-05-12 11:14:54 -07:00
parent 1a2625845c
commit ba48789a89
2 changed files with 11 additions and 7 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@welshman/editor", "name": "@welshman/editor",
"version": "0.2.3", "version": "0.2.4",
"author": "hodlbod", "author": "hodlbod",
"license": "MIT", "license": "MIT",
"description": "A batteries-included nostr editor.", "description": "A batteries-included nostr editor.",
+10 -6
View File
@@ -1,9 +1,9 @@
import {InputRule, mergeAttributes, Node, PasteRule} from "@tiptap/core" import {InputRule, mergeAttributes, Node, PasteRule} from "@tiptap/core"
import type {CodeOptions} from "@tiptap/extension-code" import type {CodeOptions} from "@tiptap/extension-code"
const inputRegex = /(?:^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))$/ const inputRegex = /(^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))$/
const pasteRegex = /(?:^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))/g const pasteRegex = /(^|\s)(`(?!\s+`)((?:[^`]+))`(?!\s+`))/g
export type CodeInlineOptions = object export type CodeInlineOptions = object
@@ -53,9 +53,11 @@ export const CodeInline = Node.create<CodeOptions>({
new InputRule({ new InputRule({
find: inputRegex, find: inputRegex,
handler: ({state, range, match}) => { handler: ({state, range, match}) => {
const textNode = state.schema.text(match[2]) const textNode = state.schema.text(match[3])
const codeNode = this.type.create(null, textNode) const codeNode = this.type.create(null, textNode)
state.tr.replaceWith(range.from, range.to, codeNode).insertText(" ") // Preserve any leading space by adjusting the range start
const spaceAdjustment = match[1] === " " ? 1 : 0
state.tr.replaceWith(range.from + spaceAdjustment, range.to, codeNode).insertText(" ")
}, },
}), }),
] ]
@@ -65,9 +67,11 @@ export const CodeInline = Node.create<CodeOptions>({
new PasteRule({ new PasteRule({
find: pasteRegex, find: pasteRegex,
handler: ({state, range, match}) => { handler: ({state, range, match}) => {
const textNode = state.schema.text(match[2]) const textNode = state.schema.text(match[3])
const codeNode = this.type.create(null, textNode) const codeNode = this.type.create(null, textNode)
state.tr.replaceWith(range.from, range.to, codeNode).insertText(" ") // Preserve any leading space by adjusting the range start
const spaceAdjustment = match[1] === " " ? 1 : 0
state.tr.replaceWith(range.from + spaceAdjustment, range.to, codeNode).insertText(" ")
}, },
}), }),
] ]