Linker.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { encodeSpaces } from '@growi/core/dist/utils/page-path-utils';
  2. export default class Linker {
  3. type: string;
  4. label: string | undefined;
  5. link: string | undefined;
  6. constructor(type = Linker.types.markdownLink, label = '', link = '') {
  7. this.type = type;
  8. this.label = label;
  9. this.link = link;
  10. if (type === Linker.types.markdownLink) {
  11. this.initWhenMarkdownLink();
  12. }
  13. this.generateMarkdownText = this.generateMarkdownText.bind(this);
  14. }
  15. static types = {
  16. markdownLink: 'mdLink',
  17. growiLink: 'growiLink',
  18. pukiwikiLink: 'pukiwikiLink',
  19. };
  20. static patterns = {
  21. pukiwikiLinkWithLabel: /^\[\[(?<label>.+)>(?<link>.+)\]\]$/, // https://regex101.com/r/2fNmUN/2
  22. pukiwikiLinkWithoutLabel: /^\[\[(?<label>.+)\]\]$/, // https://regex101.com/r/S7w5Xu/1
  23. growiLink: /^\[(?<label>\/.+)\]$/, // https://regex101.com/r/DJfkYf/3
  24. markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
  25. };
  26. initWhenMarkdownLink(): void {
  27. // fill label with link if empty
  28. if (this.label === '') {
  29. this.label = this.link;
  30. }
  31. // encode spaces
  32. this.link = encodeSpaces(this.link);
  33. }
  34. generateMarkdownText(): string | undefined {
  35. if (this.type === Linker.types.pukiwikiLink) {
  36. if (this.label === '') return `[[${this.link}]]`;
  37. return `[[${this.label}>${this.link}]]`;
  38. }
  39. if (this.type === Linker.types.growiLink) {
  40. return `[${this.link}]`;
  41. }
  42. if (this.type === Linker.types.markdownLink) {
  43. return `[${this.label}](${this.link})`;
  44. }
  45. }
  46. // create an instance of Linker from string
  47. static fromMarkdownString(str: string): Linker {
  48. // if str doesn't mean a linker, create a link whose label is str
  49. let label = str;
  50. let link = '';
  51. let type = this.types.markdownLink;
  52. // pukiwiki with separator ">".
  53. if (str.match(this.patterns.pukiwikiLinkWithLabel)) {
  54. type = this.types.pukiwikiLink;
  55. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  56. ({ label, link } = str.match(this.patterns.pukiwikiLinkWithLabel)!.groups!);
  57. }
  58. // pukiwiki without separator ">".
  59. else if (str.match(this.patterns.pukiwikiLinkWithoutLabel)) {
  60. type = this.types.pukiwikiLink;
  61. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  62. ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel)!.groups!);
  63. link = label;
  64. }
  65. // markdown
  66. else if (str.match(this.patterns.markdownLink)) {
  67. type = this.types.markdownLink;
  68. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  69. ({ label, link } = str.match(this.patterns.markdownLink)!.groups!);
  70. }
  71. // growi
  72. else if (str.match(this.patterns.growiLink)) {
  73. type = this.types.growiLink;
  74. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  75. ({ label } = str.match(this.patterns.growiLink)!.groups!);
  76. link = label;
  77. }
  78. return new Linker(
  79. type,
  80. label,
  81. link,
  82. );
  83. }
  84. // create an instance of Linker from text with index
  85. static fromLineWithIndex(line: string, index: number): Linker {
  86. const { beginningOfLink, endOfLink } = this.getBeginningAndEndIndexOfLink(line, index);
  87. // if index is in a link, extract it from line
  88. let linkStr = '';
  89. if (beginningOfLink >= 0 && endOfLink >= 0) {
  90. linkStr = line.substring(beginningOfLink, endOfLink);
  91. }
  92. return this.fromMarkdownString(linkStr);
  93. }
  94. // return beginning and end indices of link
  95. // if index is not in a link, return { beginningOfLink: -1, endOfLink: -1 }
  96. static getBeginningAndEndIndexOfLink(line: string, index: number): { beginningOfLink: number; endOfLink: number } {
  97. let beginningOfLink: number;
  98. let endOfLink: number;
  99. // pukiwiki link ('[[link]]')
  100. [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
  101. // markdown link ('[label](link)')
  102. if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index) {
  103. [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[', ')', '](');
  104. }
  105. // growi link ('[/link]')
  106. if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index) {
  107. [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[/', ']');
  108. }
  109. // return { beginningOfLink: -1, endOfLink: -1 }
  110. if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index) {
  111. [beginningOfLink, endOfLink] = [-1, -1];
  112. }
  113. return { beginningOfLink, endOfLink };
  114. }
  115. // return begin and end indices as an array only when index is between prefix and suffix and link contains containText.
  116. static getBeginningAndEndIndexWithPrefixAndSuffix(line: string, index: number, prefix: string, suffix: string, containText = ''): [number, number] {
  117. const beginningIndex = line.lastIndexOf(prefix, index);
  118. const indexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
  119. const endIndex = line.indexOf(suffix, indexOfContainText + containText.length);
  120. if (beginningIndex < 0 || indexOfContainText < 0 || endIndex < 0) {
  121. return [-1, -1];
  122. }
  123. return [beginningIndex, endIndex + suffix.length];
  124. }
  125. }