Skip to content

Commit

Permalink
allow escaped markdown within TextFormatTransformer (#2964)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Ratz <christian.ratz@b-ite.de>
  • Loading branch information
2 people authored and thegreatercurve committed Nov 25, 2022
1 parent 77166da commit 7f7fa05
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
$isParagraphNode,
$isTextNode,
} from 'lexical';
import {IS_IOS, IS_SAFARI} from 'shared/environment';

import {PUNCTUATION_OR_SPACE, transformersByType} from './utils';

Expand Down Expand Up @@ -355,22 +356,36 @@ function createTextFormatTransformersIndex(
const transformersByTag: Record<string, TextFormatTransformer> = {};
const fullMatchRegExpByTag: Record<string, RegExp> = {};
const openTagsRegExp = [];
const escapeRegExp = `(?<![\\\\])`;

for (const transformer of textTransformers) {
const {tag} = transformer;
transformersByTag[tag] = transformer;
const tagRegExp = tag.replace(/(\*|\^)/g, '\\$1');
openTagsRegExp.push(tagRegExp);
fullMatchRegExpByTag[tag] = new RegExp(
`(${tagRegExp})(?![${tagRegExp}\\s])(.*?[^${tagRegExp}\\s])${tagRegExp}(?!${tagRegExp})`,
);

if (IS_SAFARI || IS_IOS) {
fullMatchRegExpByTag[tag] = new RegExp(
`(${tagRegExp})(?![${tagRegExp}\\s])(.*?[^${tagRegExp}\\s])${tagRegExp}(?!${tagRegExp})`,
);
} else {
fullMatchRegExpByTag[tag] = new RegExp(
`(?<![\\\\${tagRegExp}])(${tagRegExp})((\\\\${tagRegExp})?.*?[^${tagRegExp}\\s](\\\\${tagRegExp})?)((?<!\\\\)|(?<=\\\\\\\\))(${tagRegExp})(?![\\\\${tagRegExp}])`,
);
}
}

return {
// Reg exp to find open tag + content + close tag
fullMatchRegExpByTag,
// Reg exp to find opening tags
openTagsRegExp: new RegExp('(' + openTagsRegExp.join('|') + ')', 'g'),
openTagsRegExp: new RegExp(
(IS_SAFARI || IS_IOS ? '' : `${escapeRegExp}`) +
'(' +
openTagsRegExp.join('|') +
')',
'g',
),
transformersByTag,
};
}

0 comments on commit 7f7fa05

Please sign in to comment.