mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-04-14 11:36:45 +00:00
Merge pull request #12140 from benkelaar/regex-escape
Issue 12138 - Fix chat support for regex chars in code blocks
This commit is contained in:
commit
6ffc28f04e
2 changed files with 19 additions and 3 deletions
|
|
@ -119,4 +119,18 @@ describe('highlightMentions', () => {
|
||||||
|
|
||||||
expect(err).to.be.undefined;
|
expect(err).to.be.undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('github issue 12138, method crashes when regex chars are used in code block', async () => {
|
||||||
|
const text = '`[test]`';
|
||||||
|
|
||||||
|
let err;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await highlightMentions(text);
|
||||||
|
} catch (e) {
|
||||||
|
err = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(err).to.be.undefined;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import escapeRegExp from 'lodash/escapeRegExp';
|
||||||
import habiticaMarkdown from 'habitica-markdown';
|
import habiticaMarkdown from 'habitica-markdown';
|
||||||
|
|
||||||
import { model as User } from '../models/user';
|
import { model as User } from '../models/user';
|
||||||
|
|
@ -57,14 +58,15 @@ function withOptionalIndentation (content) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function createCodeBlockRegex ({ content, type, markup }) {
|
function createCodeBlockRegex ({ content, type, markup }) {
|
||||||
|
const contentRegex = escapeRegExp(content);
|
||||||
let regexStr = '';
|
let regexStr = '';
|
||||||
|
|
||||||
if (type === 'code_block') {
|
if (type === 'code_block') {
|
||||||
regexStr = withOptionalIndentation(content);
|
regexStr = withOptionalIndentation(contentRegex);
|
||||||
} else if (type === 'fence') {
|
} else if (type === 'fence') {
|
||||||
regexStr = `\\s*${markup}.*\n${withOptionalIndentation(content)}\\s*${markup}`;
|
regexStr = `\\s*${markup}.*\n${withOptionalIndentation(contentRegex)}\\s*${markup}`;
|
||||||
} else { // type === code_inline
|
} else { // type === code_inline
|
||||||
regexStr = `${markup} ?${content} ?${markup}`;
|
regexStr = `${markup} ?${contentRegex} ?${markup}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RegExp(regexStr);
|
return new RegExp(regexStr);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue