msgFormatter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* jshint sub: true */
  2. var formatText = (function() {
  3. /** @type {string} */
  4. var text;
  5. /** @type {MsgBranch} */
  6. var root;
  7. var opts = {
  8. /** @type {Array<string>} */
  9. highlights: [],
  10. /** @type {function(string):string} */
  11. emojiFormatFunction: identity,
  12. /** @type {function(string):string} */
  13. textFilter: identity,
  14. /** @type {function(string):{link: string, text: string}} */
  15. linkFilter: linkidentity
  16. };
  17. /**
  18. * @constructor
  19. * @param {MsgBranch!} _parent
  20. **/
  21. function MsgTextLeaf(_parent) {
  22. /** @type {string} */
  23. this.text = "";
  24. /** @const @type {MsgBranch} */
  25. this._parent = _parent;
  26. }
  27. /**
  28. * @constructor
  29. * @param {MsgBranch} _parent
  30. * @param {number} triggerIndex
  31. * @param {string=} trigger
  32. */
  33. function MsgBranch(_parent, triggerIndex, trigger) {
  34. /** @const @type {number} */
  35. this.triggerIndex = triggerIndex;
  36. /** @type {MsgBranch|MsgTextLeaf|null} */
  37. this.lastNode = null;
  38. /** @type {Array<MsgBranch|MsgTextLeaf>} */
  39. this.subNodes = [ ];
  40. /** @const @type {string} */
  41. this.trigger = trigger || '';
  42. /** @type {boolean} */
  43. this.isLink = this.trigger === '<';
  44. /** @type {boolean} */
  45. this.isBold = this.trigger === '*';
  46. /** @type {boolean} */
  47. this.isItalic = this.trigger === '_';
  48. /** @type {boolean} */
  49. this.isStrike = this.trigger === '~' || this.trigger === '-';
  50. /** @type {boolean} */
  51. this.isQuote = this.trigger === '>' || this.trigger === "&gt;";
  52. /** @type {boolean} */
  53. this.isEmoji = this.trigger === ':';
  54. /** @type {boolean} */
  55. this.isCode = this.trigger === '`';
  56. /** @type {boolean} */
  57. this.isCodeBlock = this.trigger === '```';
  58. /** @type {boolean} */
  59. this.isEol = this.trigger === '\n';
  60. /** @type {boolean} */
  61. this.isHighlight = trigger !== undefined && opts.highlights.indexOf(trigger) !== -1;
  62. /** @const @type {MsgBranch} */
  63. this._parent = _parent;
  64. /** @type {MsgBranch|null} */
  65. this.prevTwin = null;
  66. /** @type {boolean|number} */
  67. this.terminated = this.isEol || this.isHighlight ? (triggerIndex +trigger.length -1) : false;
  68. if (this.isHighlight) {
  69. this.lastNode = new MsgTextLeaf(this);
  70. this.subNodes.push(this.lastNode);
  71. this.lastNode.text = /** @type {string} */ (trigger);
  72. }
  73. }
  74. /** @return {boolean} */
  75. MsgBranch.prototype.checkIsBold = function() {
  76. return (this.isBold && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsBold());
  77. };
  78. /** @return {boolean} */
  79. MsgBranch.prototype.checkIsItalic = function() {
  80. return (this.isItalic && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsItalic());
  81. };
  82. /** @return {boolean} */
  83. MsgBranch.prototype.checkIsStrike = function() {
  84. return (this.isStrike && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsStrike());
  85. };
  86. /** @return {boolean} */
  87. MsgBranch.prototype.checkIsQuote = function() {
  88. return (this.isQuote && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsQuote());
  89. };
  90. /** @return {boolean} */
  91. MsgBranch.prototype.checkIsEmoji = function() {
  92. return (this.isEmoji && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsEmoji());
  93. };
  94. /** @return {boolean} */
  95. MsgBranch.prototype.checkIsHighlight = function() {
  96. return (this.isHighlight && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsHighlight());
  97. };
  98. /** @return {boolean} */
  99. MsgBranch.prototype.checkIsCode = function() {
  100. return (this.isCode && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsCode());
  101. };
  102. /** @return {boolean} */
  103. MsgBranch.prototype.checkIsCodeBlock = function() {
  104. return (this.isCodeBlock && !!this.terminated) || (this._parent instanceof MsgBranch && this._parent.checkIsCodeBlock());
  105. };
  106. MsgBranch.prototype.containsUnterminatedBranch = function() {
  107. for (var i =0, nbBranches = this.subNodes.length; i < nbBranches; i++) {
  108. if (this.subNodes[i] instanceof MsgBranch && (
  109. !this.subNodes[i].terminated || this.subNodes[i].containsUnterminatedBranch()))
  110. return true;
  111. }
  112. return false;
  113. };
  114. /**
  115. * Check if this token closes the branch
  116. * In this case, finishWith should return the new leave
  117. * Exemple: `_text *bold_ bold continue*` should be
  118. * + root
  119. * | italic
  120. * | "text"
  121. * | bold
  122. * | "bold"
  123. * | bold
  124. * | "bold continue"
  125. * and this function will return a "bold" branch
  126. *
  127. * @param {number} i
  128. * @return {boolean|MsgBranch}
  129. **/
  130. MsgBranch.prototype.finishWith = function(i) {
  131. if (this.trigger === '<' && text[i] === '>')
  132. return true;
  133. var prevIsAlphadec = isAlphadec(text[i -1]);
  134. if (!this.isQuote && text.substr(i, this.trigger.length) === this.trigger) {
  135. if (!prevIsAlphadec && (this.isBold || this.isItalic || this.isStrike)) {
  136. // previous char is not compatible with finishing now
  137. return false;
  138. }
  139. if (this.lastNode && this.containsUnterminatedBranch()) // sub nodes contains at least one branch wich should be replicated
  140. return this.lastNode.makeNewBranchFromThis();
  141. else if (this.hasContent()) // self or any prevTwin has Content
  142. return true;
  143. // else no content, should not be finished now
  144. }
  145. if (text[i] === '\n' && this.isQuote)
  146. return true;
  147. return false;
  148. };
  149. /**
  150. * @return {boolean}
  151. **/
  152. MsgBranch.prototype.hasContent = function() {
  153. var _this = this;
  154. while (_this) {
  155. for (var i = 0, nbNodes = _this.subNodes.length; i < nbNodes; i++)
  156. if (_this.subNodes[i] instanceof MsgBranch || _this.subNodes[i].text.length)
  157. return true;
  158. // self or any prevTwin has Content
  159. _this = _this.prevTwin;
  160. }
  161. return false;
  162. };
  163. /**
  164. * @return {MsgBranch}
  165. **/
  166. MsgBranch.prototype.makeNewBranchFromThis = function() {
  167. var other = new MsgBranch(this._parent, this.triggerIndex, this.trigger);
  168. other.prevTwin = this;
  169. if (this.lastNode && this.lastNode instanceof MsgBranch) {
  170. other.lastNode = this.lastNode.makeNewBranchFromThis();
  171. other.subNodes = [ other.lastNode ];
  172. }
  173. return other;
  174. };
  175. /**
  176. * Check if this token is compatible with this branch
  177. * @param {number} i
  178. * @return {boolean}
  179. **/
  180. MsgBranch.prototype.isAcceptable = function(i) {
  181. if (this.isEmoji && (text[i] === ' ' || text[i] === '\t'))
  182. return false;
  183. if ((this.isEmoji || this.isLink || this.isBold || this.isItalic || this.isStrike || this.isCode) && text[i] === '\n')
  184. return false;
  185. return true;
  186. };
  187. /**
  188. * Check if str[i] is a trigger for a new node
  189. * if true, return the trigger
  190. * @param {number} i
  191. * @return {string|null}
  192. **/
  193. MsgBranch.prototype.isNewToken = function(i) {
  194. if (this.isCode || this.isEmoji || this.isCodeBlock || this.isLink)
  195. return null;
  196. if (!this.lastNode || this.lastNode.terminated || this.lastNode instanceof MsgTextLeaf) {
  197. var prevIsAlphadec = isAlphadec(text[i -1]),
  198. nextIsAlphadec = isAlphadec(text[i +1]);
  199. if (text.substr(i, 3) === '```')
  200. return '```';
  201. if (isBeginingOfLine()) {
  202. if (text.substr(i, 4) === "&gt;")
  203. return "&gt;";
  204. if (text[i] === '>')
  205. return text[i];
  206. }
  207. if ('`' === text[i] && !prevIsAlphadec)
  208. return text[i];
  209. if ('\n' === text[i])
  210. return text[i];
  211. if (['*', '~', '-', '_' ].indexOf(text[i]) !== -1 &&
  212. (nextIsAlphadec || text[i +1] === undefined || ['*', '~', '-', '_', '<', '&'].indexOf(text[i+1]) !== -1) &&
  213. (!prevIsAlphadec || text[i -1] === undefined || ['*', '~', '-', '_', '<', '&'].indexOf(text[i-1]) !== -1))
  214. return text[i];
  215. if ([':'].indexOf(text[i]) !== -1 && nextIsAlphadec)
  216. return text[i];
  217. if (['<'].indexOf(text[i]) !== -1)
  218. return text[i];
  219. for (var highlightIndex =0, nbHighlight =opts.highlights.length; highlightIndex < nbHighlight; highlightIndex++) {
  220. var highlight = opts.highlights[highlightIndex];
  221. // TODO compare with collators
  222. if (text.substr(i, highlight.length) === highlight)
  223. return highlight;
  224. }
  225. }
  226. return null;
  227. };
  228. /** @return {boolean|undefined} */
  229. MsgTextLeaf.prototype.isBeginingOfLine = function() {
  230. if (this.text.trim() !== '')
  231. return false;
  232. return undefined;
  233. };
  234. /** @return {boolean|undefined} */
  235. MsgBranch.prototype.isBeginingOfLine = function() {
  236. for (var i = this.subNodes.length -1; i >= 0; i--) {
  237. var isNewLine = this.subNodes[i].isBeginingOfLine();
  238. if (isNewLine !== undefined)
  239. return isNewLine;
  240. }
  241. if (this.isEol || this.isQuote)
  242. return true;
  243. return undefined;
  244. };
  245. /**
  246. * @param {string} c
  247. * @return {boolean}
  248. **/
  249. function isAlphadec(c) {
  250. return ((c >= 'A' && c <= 'Z') ||
  251. (c >= 'a' && c <= 'z') ||
  252. (c >= '0' && c <= '9') ||
  253. "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ".indexOf(c) !== -1);
  254. }
  255. /** @return {boolean} */
  256. function isBeginingOfLine() {
  257. var isNewLine = root.isBeginingOfLine();
  258. return isNewLine === undefined ? true : isNewLine;
  259. }
  260. /** @return {number} */
  261. MsgTextLeaf.prototype.addChar = function(i) {
  262. this.text += text[i];
  263. return 1;
  264. };
  265. /**
  266. * Parse next char
  267. * @param {number} i
  268. * @return {number}
  269. **/
  270. MsgBranch.prototype.addChar = function(i) {
  271. var isFinished = (this.lastNode && !this.lastNode.terminated && this.lastNode.finishWith) ? this.lastNode.finishWith(i) : null;
  272. if (isFinished) {
  273. var lastTriggerLen = this.lastNode.trigger.length;
  274. this.lastNode.terminate(i);
  275. if (isFinished instanceof MsgBranch) {
  276. this.lastNode = isFinished;
  277. this.subNodes.push(isFinished);
  278. }
  279. return lastTriggerLen;
  280. } else {
  281. if (!this.lastNode || this.lastNode.terminated || this.lastNode instanceof MsgTextLeaf || this.lastNode.isAcceptable(i)) {
  282. var isNewToken = this.isNewToken(i);
  283. if (isNewToken) {
  284. // FIXME if this is a quote and newToken is also a quote, trim lastnode (and remove it if necessary)
  285. this.lastNode = new MsgBranch(this, i, isNewToken);
  286. this.subNodes.push(this.lastNode);
  287. return this.lastNode.trigger.length;
  288. } else {
  289. if (!this.lastNode || this.lastNode.terminated) {
  290. this.lastNode = new MsgTextLeaf(this);
  291. this.subNodes.push(this.lastNode);
  292. }
  293. return this.lastNode.addChar(i);
  294. }
  295. } else {
  296. // last branch child is not compatible with this token.
  297. // So, lastBranch is not a branch
  298. var revertTo = this.lastNode.triggerIndex +1;
  299. root.cancelTerminate(this.lastNode.triggerIndex);
  300. // Add a new "escaped" node to replace trigger
  301. this.lastNode = new MsgTextLeaf(this);
  302. this.lastNode.addChar(revertTo -1); // Add "escaped" trigger
  303. this.subNodes.pop();
  304. this.subNodes.push(this.lastNode);
  305. // return negative value to start parsing again
  306. return revertTo -i;
  307. }
  308. }
  309. };
  310. MsgBranch.prototype.terminate = function(terminateAtIndex) {
  311. var _this = this;
  312. while (_this) {
  313. _this.terminated = terminateAtIndex;
  314. _this = _this.prevTwin;
  315. }
  316. };
  317. /**
  318. * @param {number} unTerminatedIndex
  319. **/
  320. MsgBranch.prototype.cancelTerminate = function(unTerminatedIndex) {
  321. if (this.terminated && this.terminated >= unTerminatedIndex)
  322. this.terminated = false;
  323. this.subNodes.forEach(function(node) {
  324. if (node instanceof MsgBranch) {
  325. node.cancelTerminate(unTerminatedIndex);
  326. }
  327. });
  328. };
  329. /**
  330. * @return {string}
  331. **/
  332. MsgTextLeaf.prototype.innerHTML = function() {
  333. if (this._parent.checkIsEmoji()) {
  334. var _parent = this._parent;
  335. while (_parent && !_parent.isEmoji)
  336. _parent = _parent._parent;
  337. if (_parent) {
  338. var fallback = _parent.trigger +this.text +_parent.trigger;
  339. var formatted = opts.emojiFormatFunction(fallback);
  340. return formatted ? formatted : fallback;
  341. }
  342. var _formatted = opts.emojiFormatFunction(this.text);
  343. return _formatted ? _formatted : this.text;
  344. }
  345. if (this._parent.checkIsCodeBlock()) {
  346. if (typeof hljs !== "undefined") {
  347. try {
  348. var lang = this.text.match(/^\w+/);
  349. hljs.configure({
  350. "useBR": true,
  351. "tabReplace": "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
  352. });
  353. if (lang && hljs.getLanguage(lang[0]))
  354. return hljs.fixMarkup(hljs.highlight(lang[0], this.text.substr(lang[0].length)).value);
  355. return hljs.fixMarkup(hljs.highlightAuto(this.text).value);
  356. } catch (e) {
  357. console.error(e);
  358. }
  359. }
  360. return this.text.replace(/\n/g, '<br/>');
  361. }
  362. return opts.textFilter(this.text);
  363. };
  364. /**
  365. * @return {string}
  366. **/
  367. MsgTextLeaf.prototype.outerHTML = function() {
  368. var tagName = 'span',
  369. classList = [],
  370. innerHTML,
  371. params = '';
  372. if (this._parent.checkIsCodeBlock()) {
  373. tagName = "pre";
  374. classList.push('codeblock');
  375. innerHTML = this.innerHTML();
  376. } else if (this._parent.checkIsCode()) {
  377. classList.push('code');
  378. innerHTML = this.innerHTML();
  379. } else {
  380. var link;
  381. if (this._parent.isLink && (link = opts.linkFilter(this.text))) {
  382. tagName = 'a';
  383. params = ' href="' +link.link +'"';
  384. if (!link.isInternal) {
  385. params += ' target="_blank"';
  386. }
  387. innerHTML = opts.textFilter(link.text);
  388. } else {
  389. innerHTML = this.innerHTML();
  390. }
  391. if (this._parent.checkIsBold())
  392. classList.push('bold');
  393. if (this._parent.checkIsItalic())
  394. classList.push('italic');
  395. if (this._parent.checkIsStrike())
  396. classList.push('strike');
  397. if (this._parent.checkIsEmoji())
  398. classList.push('emoji');
  399. if (this._parent.checkIsHighlight())
  400. classList.push('highlight');
  401. }
  402. return '<' +tagName +params +(classList.length ? ' class="' +classList.join(' ') +'"' : '') +'>' +innerHTML +'</' +tagName +'>';
  403. };
  404. MsgBranch.prototype.outerHTML = function() {
  405. var html = "";
  406. if (this.isQuote)
  407. html += '<span class="quote">';
  408. if (this.isEol)
  409. html += '<br/>';
  410. this.subNodes.forEach(function(node) {
  411. html += node.outerHTML();
  412. });
  413. if (this.isQuote)
  414. html += '</span>';
  415. return html;
  416. };
  417. /** @param {MsgBranch=} _root */
  418. function getFirstUnterminated(_root) {
  419. _root = _root || root;
  420. for (var i =0, nbBranches = _root.subNodes.length; i < nbBranches; i++) {
  421. var branch = _root.subNodes[i];
  422. if (branch instanceof MsgBranch) {
  423. if (!branch.terminated) {
  424. return branch;
  425. } else {
  426. var unTerminatedChild = getFirstUnterminated(branch);
  427. if (unTerminatedChild)
  428. return unTerminatedChild;
  429. }
  430. }
  431. }
  432. return null;
  433. }
  434. /**
  435. * @param {MsgBranch} branch
  436. * @param {boolean} next kill this branch or all the next ones ?
  437. **/
  438. function revertTree(branch, next) {
  439. if (branch._parent instanceof MsgBranch) {
  440. branch._parent.subNodes.splice(branch._parent.subNodes.indexOf(branch) +(next ? 1 : 0));
  441. branch._parent.lastNode = branch._parent.subNodes[branch._parent.subNodes.length -1];
  442. revertTree(branch._parent, true);
  443. }
  444. }
  445. MsgBranch.prototype.implicitClose = function(i) {
  446. if (this.isQuote && !this.terminated) {
  447. this.terminate(i);
  448. }
  449. this.subNodes.forEach(function(node) {
  450. if (node instanceof MsgBranch)
  451. node.implicitClose(i);
  452. });
  453. };
  454. /**
  455. * Try to close the tree.
  456. * If a problem is found, return its index after reverting tree at this position
  457. * @return {number|undefined}
  458. **/
  459. function eof() {
  460. root.implicitClose(text.length);
  461. var unterminated = getFirstUnterminated();
  462. if (unterminated) {
  463. // We have a first token, but never closed
  464. // kill that branch
  465. revertTree(unterminated, false);
  466. // "unterminate" all branch that will be closed in the future
  467. root.cancelTerminate(unterminated.triggerIndex);
  468. // Add a new text leaf containing trigger
  469. var textNode = new MsgTextLeaf(unterminated._parent);
  470. textNode.addChar(unterminated.triggerIndex);
  471. unterminated._parent.subNodes.push(textNode);
  472. unterminated._parent.lastNode = textNode;
  473. // Restart parsing
  474. return unterminated.triggerIndex +1;
  475. } else {
  476. // FIXME merge of same block-branches (opti)
  477. }
  478. }
  479. function identity(a) { return a; }
  480. function linkidentity(str) {
  481. return {
  482. link: str,
  483. text: str,
  484. isInternal: false
  485. };
  486. }
  487. /**
  488. * @param {string} _text
  489. * @param {({
  490. * highlights: (Array<string>|undefined),
  491. * emojiFormatFunction: (function(string):string|undefined),
  492. * textFilter: (function(string):string|undefined)
  493. * })=} _opts
  494. * @return {string}
  495. **/
  496. function _parse(_text, _opts) {
  497. if (!_opts)
  498. _opts = {};
  499. opts.highlights = _opts.highlights || [];
  500. opts.emojiFormatFunction = _opts.emojiFormatFunction || identity;
  501. opts.textFilter = _opts.textFilter || identity;
  502. opts.linkFilter = _opts.linkFilter || linkidentity;
  503. text = _text;
  504. root = new MsgBranch(this, 0);
  505. var i =0,
  506. textLen = text.length;
  507. do {
  508. while (i < textLen)
  509. i += root.addChar(i);
  510. i = eof();
  511. } while (i !== undefined);
  512. return root.outerHTML();
  513. }
  514. return _parse;
  515. })();
  516. if (typeof module !== "undefined") module.exports.formatText = formatText;