formattedTextRenderer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import * as DOM from './dom.js';
  6. export function renderText(text, options = {}) {
  7. const element = createElement(options);
  8. element.textContent = text;
  9. return element;
  10. }
  11. export function renderFormattedText(formattedText, options = {}) {
  12. const element = createElement(options);
  13. _renderFormattedText(element, parseFormattedText(formattedText, !!options.renderCodeSegments), options.actionHandler, options.renderCodeSegments);
  14. return element;
  15. }
  16. export function createElement(options) {
  17. const tagName = options.inline ? 'span' : 'div';
  18. const element = document.createElement(tagName);
  19. if (options.className) {
  20. element.className = options.className;
  21. }
  22. return element;
  23. }
  24. class StringStream {
  25. constructor(source) {
  26. this.source = source;
  27. this.index = 0;
  28. }
  29. eos() {
  30. return this.index >= this.source.length;
  31. }
  32. next() {
  33. const next = this.peek();
  34. this.advance();
  35. return next;
  36. }
  37. peek() {
  38. return this.source[this.index];
  39. }
  40. advance() {
  41. this.index++;
  42. }
  43. }
  44. function _renderFormattedText(element, treeNode, actionHandler, renderCodeSegments) {
  45. let child;
  46. if (treeNode.type === 2 /* FormatType.Text */) {
  47. child = document.createTextNode(treeNode.content || '');
  48. }
  49. else if (treeNode.type === 3 /* FormatType.Bold */) {
  50. child = document.createElement('b');
  51. }
  52. else if (treeNode.type === 4 /* FormatType.Italics */) {
  53. child = document.createElement('i');
  54. }
  55. else if (treeNode.type === 7 /* FormatType.Code */ && renderCodeSegments) {
  56. child = document.createElement('code');
  57. }
  58. else if (treeNode.type === 5 /* FormatType.Action */ && actionHandler) {
  59. const a = document.createElement('a');
  60. actionHandler.disposables.add(DOM.addStandardDisposableListener(a, 'click', (event) => {
  61. actionHandler.callback(String(treeNode.index), event);
  62. }));
  63. child = a;
  64. }
  65. else if (treeNode.type === 8 /* FormatType.NewLine */) {
  66. child = document.createElement('br');
  67. }
  68. else if (treeNode.type === 1 /* FormatType.Root */) {
  69. child = element;
  70. }
  71. if (child && element !== child) {
  72. element.appendChild(child);
  73. }
  74. if (child && Array.isArray(treeNode.children)) {
  75. treeNode.children.forEach((nodeChild) => {
  76. _renderFormattedText(child, nodeChild, actionHandler, renderCodeSegments);
  77. });
  78. }
  79. }
  80. function parseFormattedText(content, parseCodeSegments) {
  81. const root = {
  82. type: 1 /* FormatType.Root */,
  83. children: []
  84. };
  85. let actionViewItemIndex = 0;
  86. let current = root;
  87. const stack = [];
  88. const stream = new StringStream(content);
  89. while (!stream.eos()) {
  90. let next = stream.next();
  91. const isEscapedFormatType = (next === '\\' && formatTagType(stream.peek(), parseCodeSegments) !== 0 /* FormatType.Invalid */);
  92. if (isEscapedFormatType) {
  93. next = stream.next(); // unread the backslash if it escapes a format tag type
  94. }
  95. if (!isEscapedFormatType && isFormatTag(next, parseCodeSegments) && next === stream.peek()) {
  96. stream.advance();
  97. if (current.type === 2 /* FormatType.Text */) {
  98. current = stack.pop();
  99. }
  100. const type = formatTagType(next, parseCodeSegments);
  101. if (current.type === type || (current.type === 5 /* FormatType.Action */ && type === 6 /* FormatType.ActionClose */)) {
  102. current = stack.pop();
  103. }
  104. else {
  105. const newCurrent = {
  106. type: type,
  107. children: []
  108. };
  109. if (type === 5 /* FormatType.Action */) {
  110. newCurrent.index = actionViewItemIndex;
  111. actionViewItemIndex++;
  112. }
  113. current.children.push(newCurrent);
  114. stack.push(current);
  115. current = newCurrent;
  116. }
  117. }
  118. else if (next === '\n') {
  119. if (current.type === 2 /* FormatType.Text */) {
  120. current = stack.pop();
  121. }
  122. current.children.push({
  123. type: 8 /* FormatType.NewLine */
  124. });
  125. }
  126. else {
  127. if (current.type !== 2 /* FormatType.Text */) {
  128. const textCurrent = {
  129. type: 2 /* FormatType.Text */,
  130. content: next
  131. };
  132. current.children.push(textCurrent);
  133. stack.push(current);
  134. current = textCurrent;
  135. }
  136. else {
  137. current.content += next;
  138. }
  139. }
  140. }
  141. if (current.type === 2 /* FormatType.Text */) {
  142. current = stack.pop();
  143. }
  144. if (stack.length) {
  145. // incorrectly formatted string literal
  146. }
  147. return root;
  148. }
  149. function isFormatTag(char, supportCodeSegments) {
  150. return formatTagType(char, supportCodeSegments) !== 0 /* FormatType.Invalid */;
  151. }
  152. function formatTagType(char, supportCodeSegments) {
  153. switch (char) {
  154. case '*':
  155. return 3 /* FormatType.Bold */;
  156. case '_':
  157. return 4 /* FormatType.Italics */;
  158. case '[':
  159. return 5 /* FormatType.Action */;
  160. case ']':
  161. return 6 /* FormatType.ActionClose */;
  162. case '`':
  163. return supportCodeSegments ? 7 /* FormatType.Code */ : 0 /* FormatType.Invalid */;
  164. default:
  165. return 0 /* FormatType.Invalid */;
  166. }
  167. }