Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(#6040) Line code is not working properly when we use backticks ar… #6119

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions packages/core/src/inputRules/markInputRule.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,77 @@
import { MarkType } from '@tiptap/pm/model'
import { MarkType } from "@tiptap/pm/model";

import { getMarksBetween } from '../helpers/getMarksBetween.js'
import { InputRule, InputRuleFinder } from '../InputRule.js'
import { ExtendedRegExpMatchArray } from '../types.js'
import { callOrReturn } from '../utilities/callOrReturn.js'
import { getMarksBetween } from "../helpers/getMarksBetween.js";
import { InputRule, InputRuleFinder } from "../InputRule.js";
import { ExtendedRegExpMatchArray } from "../types.js";
import { callOrReturn } from "../utilities/callOrReturn.js";

/**
* Build an input rule that adds a mark when the
* matched text is typed into it.
* @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
*/
export function markInputRule(config: {
find: InputRuleFinder
type: MarkType
find: InputRuleFinder;
type: MarkType;
getAttributes?:
| Record<string, any>
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
| false
| null
| null;
}) {
return new InputRule({
find: config.find,
handler: ({ state, range, match }) => {
const attributes = callOrReturn(config.getAttributes, undefined, match)
const attributes = callOrReturn(config.getAttributes, undefined, match);

if (attributes === false || attributes === null) {
return null
return null;
}

const { tr } = state
const captureGroup = match[match.length - 1]
const fullMatch = match[0]
const { tr } = state;
const captureGroup = match[match.length - 1];
const fullMatch = match[0];

if (captureGroup) {
const startSpaces = fullMatch.search(/\S/)
const textStart = range.from + fullMatch.indexOf(captureGroup)
const textEnd = textStart + captureGroup.length

const startSpaces = fullMatch.search(/\S/);
const textStart = range.from + fullMatch.indexOf(captureGroup);
const textEnd = textStart + captureGroup.length;
const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
.filter(item => {
.filter((item) => {
// @ts-ignore
const excluded = item.mark.type.excluded as MarkType[]
const excluded = item.mark.type.excluded as MarkType[];

return excluded.find(type => type === config.type && type !== item.mark.type)
return excluded.find((type) => type === config.type && type !== item.mark.type);
})
.filter(item => item.to > textStart)
.filter((item) => item.to > textStart);

if (excludedMarks.length) {
return null
return null;
}

if (textEnd < range.to) {
tr.delete(textEnd, range.to)
if (textEnd < range.to && !config.type.name === "code") {
tr.delete(textEnd, range.to);
}

if (textStart > range.from) {
tr.delete(range.from + startSpaces, textStart)
if (textStart > range.from && !config.type.name === "code") {
tr.delete(range.from + startSpaces, textStart);
}
if (config.type.name === "code") {

const markEnd = range.from + startSpaces + captureGroup.length

tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}))

tr.removeStoredMark(config.type)
/* Adjusting the range to handle cases with no spaces before the code
Example: `123` - if there are no spaces before the code, the range starts from 123`
When there are spaces, it starts from `123` */
const start = range.from - (startSpaces ? 0 : 1);
const end = range.to - 1;
tr.delete(end, range.to);
tr.delete(start, start + 1);
tr.addMark(start, end, config.type.create(attributes || {}));
}
if (!config.type.name === "code") {
const markEnd = range.from + startSpaces + captureGroup.length;
tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}));
}
tr.removeStoredMark(config.type);
}
},
})
}
});
}