Skip to content

Commit

Permalink
fix: bold & em input rules (#491)
Browse files Browse the repository at this point in the history
Co-authored-by: Sibiraj <[email protected]>
  • Loading branch information
raphael-inglin-ergon and sibiraj-s authored Aug 18, 2023
1 parent 14ee27f commit 70d5998
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 12 additions & 6 deletions projects/ngx-editor/helpers/markInputRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@ export const markInputRule = (regexp: RegExp, markType: MarkType, attrs?: Record
const from = start;
let to = end;

if (match[2]) {
const textStart = start + match[0].indexOf(match[2]);
const textEnd = textStart + match[2].length;
const [fullMatch, , content] = match;
const noOfStartSpaces = fullMatch.search(/\S/);

if (content) {
const textStart = start + fullMatch.indexOf(content);
const textEnd = textStart + content.length;

if (textEnd < end) {
tr.delete(textEnd, end);
}

if (textStart > start) {
tr.delete(start, textStart);
tr.delete(start + noOfStartSpaces, textStart);
}

to = start + match[2].length;
to = start + content.length + noOfStartSpaces;
}

return tr.addMark(from, to, markType.create(attrs));
tr.addMark(from, to, markType.create(attrs));
tr.removeStoredMark(markType);

return tr;
});
};

Expand Down
6 changes: 4 additions & 2 deletions projects/ngx-editor/src/lib/defaultPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ const headingRule = (nodeType: NodeType, maxLevel: number): InputRule => {
// : (MarkType) → InputRule
// Wraps matching text with bold mark
const boldRule = (markType: MarkType): InputRule => {
return markInputRule(/(?:^|\s)(?:(?:\*\*|__)(?:(?:[^*_]+))(?:\*\*|__))$/, markType);
// eslint-disable-next-line prefer-named-capture-group
return markInputRule(/(?:^|\s)(?:(\*\*|__)(?:([^*_]+))(\*\*|__))$/, markType);
};

// : (MarkType) → InputRule
// Wraps matching text with em mark
const emRule = (markType: MarkType): InputRule => {
return markInputRule(/(?:^|\s)(?:(?:\*|_)(?:(?:[^*_]+))(?:\*|_))$/, markType);
// eslint-disable-next-line prefer-named-capture-group
return markInputRule(/(?:^|\s)(?:(\*|_)(?:([^*_]+))(\*|_))$/, markType);
};

// : (Schema) → Plugin
Expand Down

0 comments on commit 70d5998

Please sign in to comment.