linkedText.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. import { memoize } from './decorators.js';
  12. export class LinkedText {
  13. constructor(nodes) {
  14. this.nodes = nodes;
  15. }
  16. toString() {
  17. return this.nodes.map(node => typeof node === 'string' ? node : node.label).join('');
  18. }
  19. }
  20. __decorate([
  21. memoize
  22. ], LinkedText.prototype, "toString", null);
  23. const LINK_REGEX = /\[([^\]]+)\]\(((?:https?:\/\/|command:|file:)[^\)\s]+)(?: (["'])(.+?)(\3))?\)/gi;
  24. export function parseLinkedText(text) {
  25. const result = [];
  26. let index = 0;
  27. let match;
  28. while (match = LINK_REGEX.exec(text)) {
  29. if (match.index - index > 0) {
  30. result.push(text.substring(index, match.index));
  31. }
  32. const [, label, href, , title] = match;
  33. if (title) {
  34. result.push({ label, href, title });
  35. }
  36. else {
  37. result.push({ label, href });
  38. }
  39. index = match.index + match[0].length;
  40. }
  41. if (index < text.length) {
  42. result.push(text.substring(index));
  43. }
  44. return new LinkedText(result);
  45. }