rich-text.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import tplAudio from 'templates/audio.js';
  2. import tplGif from 'templates/gif.js';
  3. import tplImage from 'templates/image.js';
  4. import tplVideo from 'templates/video.js';
  5. import { Directive, directive } from 'lit/directive.js';
  6. import { api, log } from '@converse/headless';
  7. import { getEmojiMarkup } from './chat/utils.js';
  8. import { getHyperlinkTemplate } from '../utils/html.js';
  9. import { getMediaURLs } from '@converse/headless/shared/chat/utils.js';
  10. import { getMediaURLsMetadata } from '@converse/headless/shared/parsers.js';
  11. import { html } from 'lit';
  12. import { until } from 'lit/directives/until.js';
  13. import {
  14. convertASCII2Emoji,
  15. getCodePointReferences,
  16. getShortnameReferences
  17. } from '@converse/headless/plugins/emoji/utils.js';
  18. import {
  19. filterQueryParamsFromURL,
  20. isAudioURL,
  21. isGIFURL,
  22. isImageURL,
  23. isVideoURL,
  24. } from '@converse/headless/utils/url.js';
  25. import { shouldRenderMediaFromURL } from 'utils/url.js';
  26. /**
  27. * @class RichText
  28. * A String subclass that is used to render rich text (i.e. text that contains
  29. * hyperlinks, images, mentions, styling etc.).
  30. *
  31. * The "rich" parts of the text is represented by lit TemplateResult
  32. * objects which are added via the {@link RichText.addTemplateResult}
  33. * method and saved as metadata.
  34. *
  35. * By default Converse adds TemplateResults to support emojis, hyperlinks,
  36. * images, map URIs and mentions.
  37. *
  38. * 3rd party plugins can listen for the `beforeMessageBodyTransformed`
  39. * and/or `afterMessageBodyTransformed` events and then call
  40. * `addTemplateResult` on the RichText instance in order to add their own
  41. * rich features.
  42. */
  43. export class RichText extends String {
  44. /**
  45. * Create a new {@link RichText} instance.
  46. * @param {string} text - The text to be annotated
  47. * @param {number} offset - The offset of this particular piece of text
  48. * from the start of the original message text. This is necessary because
  49. * RichText instances can be nested when templates call directives
  50. * which create new RichText instances (as happens with XEP-393 styling directives).
  51. * @param {Object} [options]
  52. * @param {string} [options.nick] - The current user's nickname (only relevant if the message is in a XEP-0045 MUC)
  53. * @param {boolean} [options.render_styling] - Whether XEP-0393 message styling should be applied to the message
  54. * @param {boolean} [options.embed_audio] - Whether audio URLs should be rendered as <audio> elements.
  55. * If set to `true`, then audio files will always be rendered with an
  56. * audio player. If set to `false`, they won't, and if not defined, then the `embed_audio` setting
  57. * is used to determine whether they should be rendered as playable audio or as hyperlinks.
  58. * @param {boolean} [options.embed_videos] - Whether video URLs should be rendered as <video> elements.
  59. * If set to `true`, then videos will always be rendered with a video
  60. * player. If set to `false`, they won't, and if not defined, then the `embed_videos` setting
  61. * is used to determine whether they should be rendered as videos or as hyperlinks.
  62. * @param {Array} [options.mentions] - An array of mention references
  63. * @param {Array} [options.media_urls] - An array of {@link MediaURLMetadata} objects,
  64. * used to render media such as images, videos and audio. It might not be
  65. * possible to have the media metadata available, so if this value is
  66. * `undefined` then the passed-in `text` will be parsed for URLs. If you
  67. * don't want this parsing to happen, pass in an empty array for this
  68. * option.
  69. * @param {boolean} [options.show_images] - Whether image URLs should be rendered as <img> elements.
  70. * @param {boolean} [options.show_me_message] - Whether /me messages should be rendered differently
  71. * @param {Function} [options.onImgClick] - Callback for when an inline rendered image has been clicked
  72. * @param {Function} [options.onImgLoad] - Callback for when an inline rendered image has been loaded
  73. * @param {boolean} [options.hide_media_urls] - Callback for when an inline rendered image has been loaded
  74. */
  75. constructor (text, offset = 0, options = {}) {
  76. super(text);
  77. this.embed_audio = options?.embed_audio;
  78. this.embed_videos = options?.embed_videos;
  79. this.mentions = options?.mentions || [];
  80. this.media_urls = options?.media_urls;
  81. this.nick = options?.nick;
  82. this.offset = offset;
  83. this.onImgClick = options?.onImgClick;
  84. this.onImgLoad = options?.onImgLoad;
  85. this.options = options;
  86. this.payload = [];
  87. this.references = [];
  88. this.render_styling = options?.render_styling;
  89. this.show_images = options?.show_images;
  90. this.hide_media_urls = options?.hide_media_urls;
  91. }
  92. shouldRenderMedia (url_text, type) {
  93. let override;
  94. if (type === 'image') {
  95. override = this.show_images;
  96. } else if (type === 'audio') {
  97. override = this.embed_audio;
  98. } else if (type === 'video') {
  99. override = this.embed_videos;
  100. }
  101. if (typeof override === 'boolean') {
  102. return override;
  103. }
  104. return shouldRenderMediaFromURL(url_text, type);
  105. }
  106. /**
  107. * Look for `http` URIs and return templates that render them as URL links
  108. * @param { String } text
  109. * @param { number } local_offset - The index of the passed in text relative to
  110. * the start of this RichText instance (which is not necessarily the same as the
  111. * offset from the start of the original message stanza's body text).
  112. */
  113. addHyperlinks (text, local_offset) {
  114. const full_offset = local_offset + this.offset;
  115. const urls_meta = this.media_urls || getMediaURLsMetadata(text, local_offset).media_urls || [];
  116. const media_urls = getMediaURLs(urls_meta, text, full_offset);
  117. media_urls.filter(o => !o.is_encrypted).forEach(url_obj => {
  118. const url_text = url_obj.url;
  119. const filtered_url = filterQueryParamsFromURL(url_text);
  120. let template;
  121. if (isGIFURL(url_text) && this.shouldRenderMedia(url_text, 'image')) {
  122. template = tplGif(filtered_url, this.hide_media_urls);
  123. } else if (isImageURL(url_text) && this.shouldRenderMedia(url_text, 'image')) {
  124. template = tplImage({
  125. 'src': filtered_url,
  126. // XXX: bit of an abuse of `hide_media_urls`, might want a dedicated option here
  127. 'href': this.hide_media_urls ? null : filtered_url,
  128. 'onClick': this.onImgClick,
  129. 'onLoad': this.onImgLoad
  130. });
  131. } else if (isVideoURL(url_text) && this.shouldRenderMedia(url_text, 'video')) {
  132. template = tplVideo(filtered_url, this.hide_media_urls);
  133. } else if (isAudioURL(url_text) && this.shouldRenderMedia(url_text, 'audio')) {
  134. template = tplAudio(filtered_url, this.hide_media_urls);
  135. } else {
  136. template = getHyperlinkTemplate(filtered_url);
  137. }
  138. this.addTemplateResult(url_obj.start + local_offset, url_obj.end + local_offset, template);
  139. });
  140. }
  141. /**
  142. * Look for `geo` URIs and return templates that render them as URL links
  143. * @param { String } text
  144. * @param { number } offset - The index of the passed in text relative to
  145. * the start of the message body text.
  146. */
  147. addMapURLs (text, offset) {
  148. const regex = /geo:([\-0-9.]+),([\-0-9.]+)(?:,([\-0-9.]+))?(?:\?(.*))?/g;
  149. const matches = text.matchAll(regex);
  150. for (const m of matches) {
  151. this.addTemplateResult(
  152. m.index + offset,
  153. m.index + m[0].length + offset,
  154. getHyperlinkTemplate(m[0].replace(regex, api.settings.get('geouri_replacement')))
  155. );
  156. }
  157. }
  158. /**
  159. * Look for emojis (shortnames or unicode) and add templates for rendering them.
  160. * @param {String} text
  161. * @param {number} offset - The index of the passed in text relative to
  162. * the start of the message body text.
  163. */
  164. addEmojis (text, offset) {
  165. const references = [...getShortnameReferences(text.toString()), ...getCodePointReferences(text.toString())];
  166. references.forEach(e => {
  167. this.addTemplateResult(
  168. e.begin + offset,
  169. e.end + offset,
  170. getEmojiMarkup(e, { add_title_wrapper: true })
  171. );
  172. });
  173. }
  174. /**
  175. * Look for mentions included as XEP-0372 references and add templates for
  176. * rendering them.
  177. * @param { String } text
  178. * @param { number } local_offset - The index of the passed in text relative to
  179. * the start of this RichText instance (which is not necessarily the same as the
  180. * offset from the start of the original message stanza's body text).
  181. */
  182. addMentions (text, local_offset) {
  183. const full_offset = local_offset + this.offset;
  184. this.mentions?.forEach(ref => {
  185. const begin = Number(ref.begin) - full_offset;
  186. if (begin < 0 || begin >= full_offset + text.length) {
  187. return;
  188. }
  189. const end = Number(ref.end) - full_offset;
  190. const mention = text.slice(begin, end);
  191. if (mention === this.nick) {
  192. this.addTemplateResult(
  193. begin + local_offset,
  194. end + local_offset,
  195. tplMentionWithNick({...ref, mention })
  196. );
  197. } else {
  198. this.addTemplateResult(begin + local_offset, end + local_offset, tplMention({...ref, mention }));
  199. }
  200. });
  201. }
  202. /**
  203. * Look for XEP-0393 styling directives and add templates for rendering them.
  204. */
  205. addStyling () {
  206. if (!containsDirectives(this)) {
  207. return;
  208. }
  209. const references = [];
  210. const mention_ranges = this.mentions.map(m =>
  211. Array.from({ 'length': Number(m.end) }, (_, i) => Number(m.begin) + i)
  212. );
  213. let i = 0;
  214. while (i < this.length) {
  215. if (mention_ranges.filter(r => r.includes(i)).length) { // eslint-disable-line no-loop-func
  216. // Don't treat potential directives if they fall within a
  217. // declared XEP-0372 reference
  218. i++;
  219. continue;
  220. }
  221. const { d, length } = getDirectiveAndLength(this, i);
  222. if (d && length) {
  223. const is_quote = isQuoteDirective(d);
  224. const end = i + length;
  225. const slice_end = is_quote ? end : end - d.length;
  226. let slice_begin = d === '```' ? i + d.length + 1 : i + d.length;
  227. if (is_quote && this[slice_begin] === ' ') {
  228. // Trim leading space inside codeblock
  229. slice_begin += 1;
  230. }
  231. const offset = slice_begin;
  232. const text = this.slice(slice_begin, slice_end);
  233. references.push({
  234. 'begin': i,
  235. 'template': getDirectiveTemplate(d, text, offset, this.options),
  236. end
  237. });
  238. i = end;
  239. }
  240. i++;
  241. }
  242. references.forEach(ref => this.addTemplateResult(ref.begin, ref.end, ref.template));
  243. }
  244. trimMeMessage () {
  245. if (this.offset === 0) {
  246. // Subtract `/me ` from 3rd person messages
  247. if (this.isMeCommand()) {
  248. this.payload[0] = this.payload[0].substring(4);
  249. }
  250. }
  251. }
  252. /**
  253. * Look for plaintext (i.e. non-templated) sections of this RichText
  254. * instance and add references via the passed in function.
  255. * @param { Function } func
  256. */
  257. addAnnotations (func) {
  258. const payload = this.marshall();
  259. let idx = 0; // The text index of the element in the payload
  260. for (const text of payload) {
  261. if (!text) {
  262. continue;
  263. } else if (isString(text)) {
  264. func.call(this, text, idx);
  265. idx += text.length;
  266. } else {
  267. idx = text.end;
  268. }
  269. }
  270. }
  271. /**
  272. * Parse the text and add template references for rendering the "rich" parts.
  273. **/
  274. async addTemplates () {
  275. /**
  276. * Synchronous event which provides a hook for transforming a chat message's body text
  277. * before the default transformations have been applied.
  278. * @event _converse#beforeMessageBodyTransformed
  279. * @param { RichText } text - A {@link RichText } instance. You
  280. * can call {@link RichText#addTemplateResult } on it in order to
  281. * add TemplateResult objects meant to render rich parts of the message.
  282. * @example _converse.api.listen.on('beforeMessageBodyTransformed', (view, text) => { ... });
  283. */
  284. await api.trigger('beforeMessageBodyTransformed', this, { 'Synchronous': true });
  285. this.render_styling && this.addStyling();
  286. this.addAnnotations(this.addMentions);
  287. this.addAnnotations(this.addHyperlinks);
  288. this.addAnnotations(this.addMapURLs);
  289. await api.emojis.initialize();
  290. this.addAnnotations(this.addEmojis);
  291. /**
  292. * Synchronous event which provides a hook for transforming a chat message's body text
  293. * after the default transformations have been applied.
  294. * @event _converse#afterMessageBodyTransformed
  295. * @param { RichText } text - A {@link RichText } instance. You
  296. * can call {@link RichText#addTemplateResult} on it in order to
  297. * add TemplateResult objects meant to render rich parts of the message.
  298. * @example _converse.api.listen.on('afterMessageBodyTransformed', (view, text) => { ... });
  299. */
  300. await api.trigger('afterMessageBodyTransformed', this, { 'Synchronous': true });
  301. this.payload = this.marshall();
  302. this.options.show_me_message && this.trimMeMessage();
  303. this.payload = this.payload.map(item => (isString(item) ? item : item.template));
  304. }
  305. /**
  306. * The "rich" markup parts of a chat message are represented by lit
  307. * TemplateResult objects.
  308. *
  309. * This method can be used to add new template results to this message's
  310. * text.
  311. *
  312. * @method RichText.addTemplateResult
  313. * @param { Number } begin - The starting index of the plain message text
  314. * which is being replaced with markup.
  315. * @param { Number } end - The ending index of the plain message text
  316. * which is being replaced with markup.
  317. * @param { Object } template - The lit TemplateResult instance
  318. */
  319. addTemplateResult (begin, end, template) {
  320. this.references.push({ begin, end, template });
  321. }
  322. isMeCommand () {
  323. const text = this.toString();
  324. if (!text) {
  325. return false;
  326. }
  327. return text.startsWith('/me ');
  328. }
  329. /**
  330. * Take the annotations and return an array of text and TemplateResult
  331. * instances to be rendered to the DOM.
  332. * @method RichText#marshall
  333. */
  334. marshall () {
  335. let list = [this.toString()];
  336. this.references
  337. .sort((a, b) => b.begin - a.begin)
  338. .forEach(ref => {
  339. const text = list.shift();
  340. list = [text.slice(0, ref.begin), ref, text.slice(ref.end), ...list];
  341. });
  342. return list.reduce(
  343. (acc, i) => (isString(i) ? [...acc, convertASCII2Emoji(collapseLineBreaks(i))] : [...acc, i]),
  344. []
  345. );
  346. }
  347. }
  348. const isString = (s) => typeof s === 'string';
  349. // We don't render more than two line-breaks, replace extra line-breaks with
  350. // the zero-width whitespace character
  351. // This takes into account other characters that may have been removed by
  352. // being replaced with a zero-width space, such as '> ' in the case of
  353. // multi-line quotes.
  354. const collapseLineBreaks = (text) => text.replace(/\n(\u200B*\n)+/g, m => `\n${'\u200B'.repeat(m.length - 2)}\n`);
  355. const tplMentionWithNick = (o) =>
  356. html`<span class="mention mention--self badge badge-info" data-uri="${o.uri}">${o.mention}</span>`;
  357. const tplMention = (o) => html`<span class="mention" data-uri="${o.uri}">${o.mention}</span>`;
  358. async function transform (t) {
  359. try {
  360. await t.addTemplates();
  361. } catch (e) {
  362. log.error(e);
  363. }
  364. return t.payload;
  365. }
  366. class StylingDirective extends Directive {
  367. render (txt, offset, options) {
  368. const t = new RichText(
  369. txt,
  370. offset,
  371. Object.assign(options, { 'show_images': false, 'embed_videos': false, 'embed_audio': false })
  372. );
  373. return html`${until(transform(t), html`${t}`)}`;
  374. }
  375. }
  376. const renderStylingDirectiveBody = directive(StylingDirective);
  377. const bracketing_directives = ['*', '_', '~', '`'];
  378. const styling_directives = [...bracketing_directives, '```', '>'];
  379. const styling_map = {
  380. '*': {'name': 'strong', 'type': 'span'},
  381. '_': {'name': 'emphasis', 'type': 'span'},
  382. '~': {'name': 'strike', 'type': 'span'},
  383. '`': {'name': 'preformatted', 'type': 'span'},
  384. '```': {'name': 'preformatted_block', 'type': 'block'},
  385. '>': {'name': 'quote', 'type': 'block'}
  386. };
  387. const dont_escape = ['_', '>', '`', '~'];
  388. // prettier-ignore
  389. /* eslint-disable max-len */
  390. const styling_templates = {
  391. // m is the chatbox model
  392. // i is the offset of this directive relative to the start of the original message
  393. 'emphasis': (txt, i, options) => html`<span class="styling-directive">_</span><i>${renderStylingDirectiveBody(txt, i, options)}</i><span class="styling-directive">_</span>`,
  394. 'preformatted': txt => html`<span class="styling-directive">\`</span><code>${txt}</code><span class="styling-directive">\`</span>`,
  395. 'preformatted_block': txt => html`<div class="styling-directive">\`\`\`</div><code class="block">${txt}</code><div class="styling-directive">\`\`\`</div>`,
  396. 'quote': (txt, i, options) => html`<blockquote>${renderStylingDirectiveBody(txt, i, options)}</blockquote>`,
  397. 'strike': (txt, i, options) => html`<span class="styling-directive">~</span><del>${renderStylingDirectiveBody(txt, i, options)}</del><span class="styling-directive">~</span>`,
  398. 'strong': (txt, i, options) => html`<span class="styling-directive">*</span><b>${renderStylingDirectiveBody(txt, i, options)}</b><span class="styling-directive">*</span>`,
  399. };
  400. /**
  401. * Checks whether a given character "d" at index "i" of "text" is a valid opening or closing directive.
  402. * @param { String } d - The potential directive
  403. * @param { String } text - The text in which the directive appears
  404. * @param { Number } i - The directive index
  405. * @param { Boolean } opening - Check for a valid opening or closing directive
  406. */
  407. function isValidDirective (d, text, i, opening) {
  408. // Ignore directives that are parts of words
  409. // More info on the Regexes used here: https://javascript.info/regexp-unicode#unicode-properties-p
  410. if (opening) {
  411. const regex = RegExp(dont_escape.includes(d) ? `^(\\p{L}|\\p{N})${d}` : `^(\\p{L}|\\p{N})\\${d}`, 'u');
  412. if (i > 1 && regex.test(text.slice(i-1))) {
  413. return false;
  414. }
  415. const is_quote = isQuoteDirective(d);
  416. if (is_quote && i > 0 && text[i-1] !== '\n') {
  417. // Quote directives must be on newlines
  418. return false;
  419. } else if (bracketing_directives.includes(d) && (text[i+1] === d)) {
  420. // Don't consider empty bracketing directives as valid (e.g. **, `` etc.)
  421. return false;
  422. }
  423. } else {
  424. const regex = RegExp(dont_escape.includes(d) ? `^${d}(\\p{L}|\\p{N})` : `^\\${d}(\\p{L}|\\p{N})`, 'u');
  425. if (i < text.length-1 && regex.test(text.slice(i))) {
  426. return false;
  427. }
  428. if (bracketing_directives.includes(d) && (text[i-1] === d)) {
  429. // Don't consider empty directives as valid (e.g. **, `` etc.)
  430. return false;
  431. }
  432. }
  433. return true;
  434. }
  435. /**
  436. * Given a specific index "i" of "text", return the directive it matches or null otherwise.
  437. * @param { String } text - The text in which the directive appears
  438. * @param { Number } i - The directive index
  439. * @param { Boolean } opening - Whether we're looking for an opening or closing directive
  440. */
  441. function getDirective (text, i, opening=true) {
  442. let d;
  443. if (
  444. (/(^```[\s,\u200B]*\n)|(^```[\s,\u200B]*$)/).test(text.slice(i)) &&
  445. (i === 0 || text[i-1] === '>' || (/\n\u200B{0,2}$/).test(text.slice(0, i)))
  446. ) {
  447. d = text.slice(i, i+3);
  448. } else if (styling_directives.includes(text.slice(i, i+1))) {
  449. d = text.slice(i, i+1);
  450. if (!isValidDirective(d, text, i, opening)) return null;
  451. } else {
  452. return null;
  453. }
  454. return d;
  455. }
  456. /**
  457. * Given a directive "d", which occurs in "text" at index "i", check that it
  458. * has a valid closing directive and return the length from start to end of the
  459. * directive.
  460. * @param { String } d -The directive
  461. * @param { Number } i - The directive index
  462. * @param { String } text -The text in which the directive appears
  463. */
  464. function getDirectiveLength (d, text, i) {
  465. if (!d) return 0;
  466. const begin = i;
  467. i += d.length;
  468. if (isQuoteDirective(d)) {
  469. i += text.slice(i).split(/\n\u200B*[^>\u200B]/).shift().length;
  470. return i-begin;
  471. } else if (styling_map[d].type === 'span') {
  472. const line = text.slice(i).split('\n').shift();
  473. let j = 0;
  474. let idx = line.indexOf(d);
  475. while (idx !== -1) {
  476. if (getDirective(text, i+idx, false) === d) {
  477. return idx+2*d.length;
  478. }
  479. idx = line.indexOf(d, j++);
  480. }
  481. return 0;
  482. } else {
  483. // block directives
  484. const substring = text.slice(i+1);
  485. let j = 0;
  486. let idx = substring.indexOf(d);
  487. while (idx !== -1) {
  488. if (getDirective(text, i+1+idx, false) === d) {
  489. return idx+1+2*d.length;
  490. }
  491. idx = substring.indexOf(d, j++);
  492. }
  493. return 0;
  494. }
  495. }
  496. function getDirectiveAndLength (text, i) {
  497. const d = getDirective(text, i);
  498. const length = d ? getDirectiveLength(d, text, i) : 0;
  499. return length > 0 ? { d, length } : {};
  500. }
  501. const isQuoteDirective = (d) => ['>', '&gt;'].includes(d);
  502. function getDirectiveTemplate (d, text, offset, options) {
  503. const template = styling_templates[styling_map[d].name];
  504. if (isQuoteDirective(d)) {
  505. const newtext = text
  506. // Don't show the directive itself
  507. // This big [] corresponds to \s without newlines, to avoid issues when the > is the last character of the line
  508. .replace(/\n\u200B*>[ \f\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]?/g, m => `\n${'\u200B'.repeat(m.length - 1)}`)
  509. .replace(/\n$/, ''); // Trim line-break at the end
  510. return template(newtext, offset, options);
  511. } else {
  512. return template(text, offset, options);
  513. }
  514. }
  515. function containsDirectives (text) {
  516. for (let i=0; i<styling_directives.length; i++) {
  517. if (text.includes(styling_directives[i])) {
  518. return true;
  519. }
  520. }
  521. }