Skip to content

Commit

Permalink
Merge pull request #2765 from microsoft/u/juliaroldi/fix-markdown
Browse files Browse the repository at this point in the history
Do not trigger markdown inside a word.
  • Loading branch information
juliaroldi authored Aug 12, 2024
2 parents 6370e59 + 2761b34 commit c5d5de9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ export function setFormat(
editor,
(_model, previousSegment, paragraph, markerFormat, context) => {
if (previousSegment.text[previousSegment.text.length - 1] == character) {
const textBeforeMarker = previousSegment.text.slice(0, -1);
const textSegment = previousSegment.text;
const textBeforeMarker = textSegment.slice(0, -1);
context.newPendingFormat = {
...markerFormat,
strikethrough: !!markerFormat.strikethrough,
italic: !!markerFormat.italic,
fontWeight: markerFormat?.fontWeight ? 'bold' : undefined,
};
if (textBeforeMarker.indexOf(character) > -1) {
const lastCharIndex = previousSegment.text.length;
const firstCharIndex = previousSegment.text
const lastCharIndex = textSegment.length;
const firstCharIndex = textSegment
.substring(0, lastCharIndex - 1)
.lastIndexOf(character);
if (lastCharIndex - firstCharIndex > 2) {

if (
hasSpaceBeforeFirstCharacter(textSegment, firstCharIndex) &&
lastCharIndex - firstCharIndex > 2
) {
const formattedText = splitTextSegment(
previousSegment,
paragraph,
Expand Down Expand Up @@ -61,3 +66,12 @@ export function setFormat(
}
);
}

/**
* The markdown should not be trigger inside a word, then check if exist a space before the trigger character
* Should trigger markdown example: _one two_
* Should not trigger markdown example: one_two_
*/
function hasSpaceBeforeFirstCharacter(text: string, index: number) {
return !text[index - 1] || text[index - 1].trim().length == 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,31 @@ describe('setFormat', () => {

runTest(input, '*', { fontWeight: 'bold' }, input, false);
});

it('should not set italic - one_two_', () => {
const input: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'one_two_',
format: {},
},
{
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
],
format: {},
},
],
format: {},
};

runTest(input, '_', { italic: true }, input, false);
});
});

0 comments on commit c5d5de9

Please sign in to comment.