htmlContent.js 3.7 KB

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