Linker.js 4.6 KB

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