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

feat: allow escaped markdown within TextFormatTransformer #2964

Merged
merged 1 commit into from
Sep 8, 2022
Merged
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
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,
};
}