htmlContent.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { illegalArgument } from './errors.js';
  6. import { escapeIcons } from './iconLabels.js';
  7. export class MarkdownString {
  8. constructor(value = '', isTrustedOrOptions = false) {
  9. var _a, _b;
  10. this.value = value;
  11. if (typeof this.value !== 'string') {
  12. throw illegalArgument('value');
  13. }
  14. if (typeof isTrustedOrOptions === 'boolean') {
  15. this.isTrusted = isTrustedOrOptions;
  16. this.supportThemeIcons = false;
  17. }
  18. else {
  19. this.isTrusted = (_a = isTrustedOrOptions.isTrusted) !== null && _a !== void 0 ? _a : undefined;
  20. this.supportThemeIcons = (_b = isTrustedOrOptions.supportThemeIcons) !== null && _b !== void 0 ? _b : false;
  21. }
  22. }
  23. appendText(value, newlineStyle = 0 /* Paragraph */) {
  24. this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? escapeIcons(value) : value)
  25. .replace(/([ \t]+)/g, (_match, g1) => ' '.repeat(g1.length))
  26. .replace(/^>/gm, '\\>')
  27. .replace(/\n/g, newlineStyle === 1 /* Break */ ? '\\\n' : '\n\n');
  28. return this;
  29. }
  30. appendMarkdown(value) {
  31. this.value += value;
  32. return this;
  33. }
  34. appendCodeblock(langId, code) {
  35. this.value += '\n```';
  36. this.value += langId;
  37. this.value += '\n';
  38. this.value += code;
  39. this.value += '\n```\n';
  40. return this;
  41. }
  42. }
  43. export function isEmptyMarkdownString(oneOrMany) {
  44. if (isMarkdownString(oneOrMany)) {
  45. return !oneOrMany.value;
  46. }
  47. else if (Array.isArray(oneOrMany)) {
  48. return oneOrMany.every(isEmptyMarkdownString);
  49. }
  50. else {
  51. return true;
  52. }
  53. }
  54. export function isMarkdownString(thing) {
  55. if (thing instanceof MarkdownString) {
  56. return true;
  57. }
  58. else if (thing && typeof thing === 'object') {
  59. return typeof thing.value === 'string'
  60. && (typeof thing.isTrusted === 'boolean' || thing.isTrusted === undefined)
  61. && (typeof thing.supportThemeIcons === 'boolean' || thing.supportThemeIcons === undefined);
  62. }
  63. return false;
  64. }
  65. export function escapeMarkdownSyntaxTokens(text) {
  66. // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
  67. return text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
  68. }
  69. export function removeMarkdownEscapes(text) {
  70. if (!text) {
  71. return text;
  72. }
  73. return text.replace(/\\([\\`*_{}[\]()#+\-.!])/g, '$1');
  74. }
  75. export function parseHrefAndDimensions(href) {
  76. const dimensions = [];
  77. const splitted = href.split('|').map(s => s.trim());
  78. href = splitted[0];
  79. const parameters = splitted[1];
  80. if (parameters) {
  81. const heightFromParams = /height=(\d+)/.exec(parameters);
  82. const widthFromParams = /width=(\d+)/.exec(parameters);
  83. const height = heightFromParams ? heightFromParams[1] : '';
  84. const width = widthFromParams ? widthFromParams[1] : '';
  85. const widthIsFinite = isFinite(parseInt(width));
  86. const heightIsFinite = isFinite(parseInt(height));
  87. if (widthIsFinite) {
  88. dimensions.push(`width="${width}"`);
  89. }
  90. if (heightIsFinite) {
  91. dimensions.push(`height="${height}"`);
  92. }
  93. }
  94. return { href, dimensions };
  95. }