Fix code inline space handling
This commit is contained in:
@@ -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.",
|
||||||
|
|||||||
@@ -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(" ")
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user