PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/utils/text_formatting.jsx

https://gitlab.com/auchalet/mattermost
JSX | 445 lines | 353 code | 61 blank | 31 comment | 31 complexity | e72ffe035639e46f2acc5e38141b3db6 MD5 | raw file
  1. // Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
  2. // See License.txt for license information.
  3. import Autolinker from 'autolinker';
  4. import {browserHistory} from 'react-router';
  5. import Constants from './constants.jsx';
  6. import * as Emoticons from './emoticons.jsx';
  7. import * as Markdown from './markdown.jsx';
  8. import PreferenceStore from 'stores/preference_store.jsx';
  9. import UserStore from 'stores/user_store.jsx';
  10. import twemoji from 'twemoji';
  11. import * as Utils from './utils.jsx';
  12. // Performs formatting of user posts including highlighting mentions and search terms and converting urls, hashtags, and
  13. // @mentions to links by taking a user's message and returning a string of formatted html. Also takes a number of options
  14. // as part of the second parameter:
  15. // - searchTerm - If specified, this word is highlighted in the resulting html. Defaults to nothing.
  16. // - mentionHighlight - Specifies whether or not to highlight mentions of the current user. Defaults to true.
  17. // - singleline - Specifies whether or not to remove newlines. Defaults to false.
  18. // - emoticons - Enables emoticon parsing. Defaults to true.
  19. // - markdown - Enables markdown parsing. Defaults to true.
  20. export function formatText(text, options = {}) {
  21. let output = text;
  22. // would probably make more sense if it was on the calling components, but this option is intended primarily for debugging
  23. if (PreferenceStore.get(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, 'formatting', 'true') === 'false') {
  24. return output;
  25. }
  26. if (!('markdown' in options) || options.markdown) {
  27. // the markdown renderer will call doFormatText as necessary
  28. output = Markdown.format(output, options);
  29. } else {
  30. output = sanitizeHtml(output);
  31. output = doFormatText(output, options);
  32. }
  33. // replace newlines with spaces if necessary
  34. if (options.singleline) {
  35. output = replaceNewlines(output);
  36. }
  37. output = insertLongLinkWbr(output);
  38. return output;
  39. }
  40. // Performs most of the actual formatting work for formatText. Not intended to be called normally.
  41. export function doFormatText(text, options) {
  42. let output = text;
  43. const tokens = new Map();
  44. // replace important words and phrases with tokens
  45. output = autolinkAtMentions(output, tokens);
  46. output = autolinkEmails(output, tokens);
  47. output = autolinkHashtags(output, tokens);
  48. if (!('emoticons' in options) || options.emoticon) {
  49. output = Emoticons.handleEmoticons(output, tokens);
  50. }
  51. if (options.searchTerm) {
  52. output = highlightSearchTerm(output, tokens, options.searchTerm);
  53. }
  54. if (!('mentionHighlight' in options) || options.mentionHighlight) {
  55. output = highlightCurrentMentions(output, tokens);
  56. }
  57. if (!('emoticons' in options) || options.emoticon) {
  58. output = twemoji.parse(output, {
  59. className: 'emoticon',
  60. base: '',
  61. folder: Constants.EMOJI_PATH,
  62. callback: (icon, twemojiOptions) => {
  63. if (!Emoticons.getEmoticonsByCodePoint().has(icon)) {
  64. // just leave the unicode characters and hope the browser can handle it
  65. return null;
  66. }
  67. return ''.concat(twemojiOptions.base, twemojiOptions.size, '/', icon, twemojiOptions.ext);
  68. }
  69. });
  70. }
  71. //replace all "/" to "/<wbr />"
  72. output = output.replace(/\//g, '/<wbr />');
  73. // reinsert tokens with formatted versions of the important words and phrases
  74. output = replaceTokens(output, tokens);
  75. return output;
  76. }
  77. export function sanitizeHtml(text) {
  78. let output = text;
  79. // normal string.replace only does a single occurrance so use a regex instead
  80. output = output.replace(/&/g, '&amp;');
  81. output = output.replace(/</g, '&lt;');
  82. output = output.replace(/>/g, '&gt;');
  83. output = output.replace(/'/g, '&apos;');
  84. output = output.replace(/"/g, '&quot;');
  85. return output;
  86. }
  87. // Convert emails into tokens
  88. function autolinkEmails(text, tokens) {
  89. function replaceEmailWithToken(autolinker, match) {
  90. const linkText = match.getMatchedText();
  91. let url = linkText;
  92. if (match.getType() === 'email') {
  93. url = `mailto:${url}`;
  94. }
  95. const index = tokens.size;
  96. const alias = `MM_EMAIL${index}`;
  97. tokens.set(alias, {
  98. value: `<a class="theme" href="${url}">${linkText}</a>`,
  99. originalText: linkText
  100. });
  101. return alias;
  102. }
  103. // we can't just use a static autolinker because we need to set replaceFn
  104. const autolinker = new Autolinker({
  105. urls: false,
  106. email: true,
  107. phone: false,
  108. twitter: false,
  109. hashtag: false,
  110. replaceFn: replaceEmailWithToken
  111. });
  112. return autolinker.link(text);
  113. }
  114. function autolinkAtMentions(text, tokens) {
  115. // Return true if provided character is punctuation
  116. function isPunctuation(character) {
  117. const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
  118. return re.test(character);
  119. }
  120. // Test if provided text needs to be highlighted, special mention or current user
  121. function mentionExists(u) {
  122. return (Constants.SPECIAL_MENTIONS.indexOf(u) !== -1 || UserStore.getProfileByUsername(u));
  123. }
  124. function addToken(username, mention) {
  125. const index = tokens.size;
  126. const alias = `MM_ATMENTION${index}`;
  127. tokens.set(alias, {
  128. value: `<a class='mention-link' href='#' data-mention='${username}'>${mention}</a>`,
  129. originalText: mention
  130. });
  131. return alias;
  132. }
  133. function replaceAtMentionWithToken(fullMatch, mention, username) {
  134. let usernameLower = username.toLowerCase();
  135. if (mentionExists(usernameLower)) {
  136. // Exact match
  137. const alias = addToken(usernameLower, mention, '');
  138. return alias;
  139. }
  140. // Not an exact match, attempt to truncate any punctuation to see if we can find a user
  141. const originalUsername = usernameLower;
  142. for (let c = usernameLower.length; c > 0; c--) {
  143. if (isPunctuation(usernameLower[c - 1])) {
  144. usernameLower = usernameLower.substring(0, c - 1);
  145. if (mentionExists(usernameLower)) {
  146. const suffix = originalUsername.substr(c - 1);
  147. const alias = addToken(usernameLower, '@' + usernameLower);
  148. return alias + suffix;
  149. }
  150. } else {
  151. // If the last character is not punctuation, no point in going any further
  152. break;
  153. }
  154. }
  155. return fullMatch;
  156. }
  157. let output = text;
  158. output = output.replace(/(@([a-z0-9.\-_]*))/gi, replaceAtMentionWithToken);
  159. return output;
  160. }
  161. function escapeRegex(text) {
  162. return text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  163. }
  164. function highlightCurrentMentions(text, tokens) {
  165. let output = text;
  166. const mentionKeys = UserStore.getCurrentMentionKeys();
  167. // look for any existing tokens which are self mentions and should be highlighted
  168. var newTokens = new Map();
  169. for (const [alias, token] of tokens) {
  170. if (mentionKeys.indexOf(token.originalText) !== -1) {
  171. const index = tokens.size + newTokens.size;
  172. const newAlias = `MM_SELFMENTION${index}`;
  173. newTokens.set(newAlias, {
  174. value: `<span class='mention--highlight'>${alias}</span>`,
  175. originalText: token.originalText
  176. });
  177. output = output.replace(alias, newAlias);
  178. }
  179. }
  180. // the new tokens are stashed in a separate map since we can't add objects to a map during iteration
  181. for (const newToken of newTokens) {
  182. tokens.set(newToken[0], newToken[1]);
  183. }
  184. // look for self mentions in the text
  185. function replaceCurrentMentionWithToken(fullMatch, prefix, mention) {
  186. const index = tokens.size;
  187. const alias = `MM_SELFMENTION${index}`;
  188. tokens.set(alias, {
  189. value: `<span class='mention--highlight'>${mention}</span>`,
  190. originalText: mention
  191. });
  192. return prefix + alias;
  193. }
  194. for (const mention of UserStore.getCurrentMentionKeys()) {
  195. if (!mention) {
  196. continue;
  197. }
  198. output = output.replace(new RegExp(`(^|\\W)(${escapeRegex(mention)})\\b`, 'gi'), replaceCurrentMentionWithToken);
  199. }
  200. return output;
  201. }
  202. function autolinkHashtags(text, tokens) {
  203. let output = text;
  204. var newTokens = new Map();
  205. for (const [alias, token] of tokens) {
  206. if (token.originalText.lastIndexOf('#', 0) === 0) {
  207. const index = tokens.size + newTokens.size;
  208. const newAlias = `MM_HASHTAG${index}`;
  209. newTokens.set(newAlias, {
  210. value: `<a class='mention-link' href='#' data-hashtag='${token.originalText}'>${token.originalText}</a>`,
  211. originalText: token.originalText
  212. });
  213. output = output.replace(alias, newAlias);
  214. }
  215. }
  216. // the new tokens are stashed in a separate map since we can't add objects to a map during iteration
  217. for (const newToken of newTokens) {
  218. tokens.set(newToken[0], newToken[1]);
  219. }
  220. // look for hashtags in the text
  221. function replaceHashtagWithToken(fullMatch, prefix, hashtag) {
  222. const index = tokens.size;
  223. const alias = `MM_HASHTAG${index}`;
  224. let value = hashtag;
  225. if (hashtag.length > Constants.MIN_HASHTAG_LINK_LENGTH) {
  226. value = `<a class='mention-link' href='#' data-hashtag='${hashtag}'>${hashtag}</a>`;
  227. }
  228. tokens.set(alias, {
  229. value,
  230. originalText: hashtag
  231. });
  232. return prefix + alias;
  233. }
  234. return output.replace(/(^|\W)(#[a-zA-ZäöüÄÖÜß][a-zA-Z0-9äöüÄÖÜß.\-_]*)\b/g, replaceHashtagWithToken);
  235. }
  236. const puncStart = /^[.,()&$!\[\]{}':;\\]+/;
  237. const puncEnd = /[.,()&$#!\[\]{}':;\\]+$/;
  238. function parseSearchTerms(searchTerm) {
  239. let terms = [];
  240. let termString = searchTerm;
  241. while (termString) {
  242. let captured;
  243. // check for a quoted string
  244. captured = (/^"(.*?)"/).exec(termString);
  245. if (captured) {
  246. termString = termString.substring(captured[0].length);
  247. terms.push(captured[1]);
  248. continue;
  249. }
  250. // check for a search flag (and don't add it to terms)
  251. captured = (/^(?:in|from|channel): ?\S+/).exec(termString);
  252. if (captured) {
  253. termString = termString.substring(captured[0].length);
  254. continue;
  255. }
  256. // capture any plain text up until the next quote or search flag
  257. captured = (/^.+?(?=\bin|\bfrom|\bchannel|"|$)/).exec(termString);
  258. if (captured) {
  259. termString = termString.substring(captured[0].length);
  260. // break the text up into words based on how the server splits them in SqlPostStore.SearchPosts and then discard empty terms
  261. terms.push(...captured[0].split(/[ <>+\-\(\)~@]/).filter((term) => !!term));
  262. continue;
  263. }
  264. // we should never reach this point since at least one of the regexes should match something in the remaining text
  265. throw new Error('Infinite loop in search term parsing: ' + termString);
  266. }
  267. // remove punctuation from each term
  268. terms = terms.map((term) => term.replace(puncStart, '').replace(puncEnd, ''));
  269. return terms;
  270. }
  271. function convertSearchTermToRegex(term) {
  272. let pattern;
  273. if (term.endsWith('*')) {
  274. pattern = '\\b' + escapeRegex(term.substring(0, term.length - 1));
  275. } else {
  276. pattern = '\\b' + escapeRegex(term) + '\\b';
  277. }
  278. return new RegExp(pattern, 'gi');
  279. }
  280. function highlightSearchTerm(text, tokens, searchTerm) {
  281. const terms = parseSearchTerms(searchTerm);
  282. if (terms.length === 0) {
  283. return text;
  284. }
  285. let output = text;
  286. function replaceSearchTermWithToken(word) {
  287. const index = tokens.size;
  288. const alias = `MM_SEARCHTERM${index}`;
  289. tokens.set(alias, {
  290. value: `<span class='search-highlight'>${word}</span>`,
  291. originalText: word
  292. });
  293. return alias;
  294. }
  295. for (const term of terms) {
  296. // highlight existing tokens matching search terms
  297. var newTokens = new Map();
  298. for (const [alias, token] of tokens) {
  299. if (token.originalText === term.replace(/\*$/, '')) {
  300. const index = tokens.size + newTokens.size;
  301. const newAlias = `MM_SEARCHTERM${index}`;
  302. newTokens.set(newAlias, {
  303. value: `<span class='search-highlight'>${alias}</span>`,
  304. originalText: token.originalText
  305. });
  306. output = output.replace(alias, newAlias);
  307. }
  308. }
  309. // the new tokens are stashed in a separate map since we can't add objects to a map during iteration
  310. for (const newToken of newTokens) {
  311. tokens.set(newToken[0], newToken[1]);
  312. }
  313. output = output.replace(convertSearchTermToRegex(term), replaceSearchTermWithToken);
  314. }
  315. return output;
  316. }
  317. function replaceTokens(text, tokens) {
  318. let output = text;
  319. // iterate backwards through the map so that we do replacement in the opposite order that we added tokens
  320. const aliases = [...tokens.keys()];
  321. for (let i = aliases.length - 1; i >= 0; i--) {
  322. const alias = aliases[i];
  323. const token = tokens.get(alias);
  324. output = output.replace(alias, token.value);
  325. }
  326. return output;
  327. }
  328. function replaceNewlines(text) {
  329. return text.replace(/\n/g, ' ');
  330. }
  331. // A click handler that can be used with the results of TextFormatting.formatText to add default functionality
  332. // to clicked hashtags and @mentions.
  333. export function handleClick(e) {
  334. const mentionAttribute = e.target.getAttributeNode('data-mention');
  335. const hashtagAttribute = e.target.getAttributeNode('data-hashtag');
  336. const linkAttribute = e.target.getAttributeNode('data-link');
  337. if (mentionAttribute) {
  338. Utils.searchForTerm(mentionAttribute.value);
  339. } else if (hashtagAttribute) {
  340. Utils.searchForTerm(hashtagAttribute.value);
  341. } else if (linkAttribute) {
  342. browserHistory.push(linkAttribute.value);
  343. }
  344. }
  345. //replace all "/" inside <a> tags to "/<wbr />"
  346. function insertLongLinkWbr(test) {
  347. return test.replace(/\//g, (match, position, string) => {
  348. return match + ((/a[^>]*>[^<]*$/).test(string.substr(0, position)) ? '<wbr />' : '');
  349. });
  350. }