Shun Miyazawa 2 лет назад
Родитель
Сommit
c10a5daadf
1 измененных файлов с 25 добавлено и 20 удалено
  1. 25 20
      packages/editor/src/services/link-util/Linker.ts

+ 25 - 20
packages/editor/src/services/link-util/Linker.js → packages/editor/src/services/link-util/Linker.ts

@@ -1,14 +1,18 @@
-
 import { encodeSpaces } from '@growi/core/dist/utils/page-path-utils';
 
 export default class Linker {
 
+  type: string;
+
+  label: string | undefined;
+
+  link: string | undefined;
+
   constructor(
-      type = Linker.types.markdownLink,
+      type: string = Linker.types.markdownLink,
       label = '',
       link = '',
   ) {
-
     this.type = type;
     this.label = label;
     this.link = link;
@@ -33,7 +37,7 @@ export default class Linker {
     markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
   };
 
-  initWhenMarkdownLink() {
+  initWhenMarkdownLink(): void {
     // fill label with link if empty
     if (this.label === '') {
       this.label = this.link;
@@ -42,7 +46,7 @@ export default class Linker {
     this.link = encodeSpaces(this.link);
   }
 
-  generateMarkdownText() {
+  generateMarkdownText(): string {
     if (this.type === Linker.types.pukiwikiLink) {
       if (this.label === '') return `[[${this.link}]]`;
       return `[[${this.label}>${this.link}]]`;
@@ -53,10 +57,11 @@ export default class Linker {
     if (this.type === Linker.types.markdownLink) {
       return `[${this.label}](${this.link})`;
     }
+    throw new Error('Invalid linker type');
   }
 
   // create an instance of Linker from string
-  static fromMarkdownString(str) {
+  static fromMarkdownString(str: string): Linker {
     // if str doesn't mean a linker, create a link whose label is str
     let label = str;
     let link = '';
@@ -65,23 +70,23 @@ export default class Linker {
     // pukiwiki with separator ">".
     if (str.match(this.patterns.pukiwikiLinkWithLabel)) {
       type = this.types.pukiwikiLink;
-      ({ label, link } = str.match(this.patterns.pukiwikiLinkWithLabel).groups);
+      ({ label, link } = str.match(this.patterns.pukiwikiLinkWithLabel)!.groups!);
     }
     // pukiwiki without separator ">".
     else if (str.match(this.patterns.pukiwikiLinkWithoutLabel)) {
       type = this.types.pukiwikiLink;
-      ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel).groups);
+      ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel)!.groups!);
       link = label;
     }
     // markdown
     else if (str.match(this.patterns.markdownLink)) {
       type = this.types.markdownLink;
-      ({ label, link } = str.match(this.patterns.markdownLink).groups);
+      ({ label, link } = str.match(this.patterns.markdownLink)!.groups!);
     }
     // growi
     else if (str.match(this.patterns.growiLink)) {
       type = this.types.growiLink;
-      ({ label } = str.match(this.patterns.growiLink).groups);
+      ({ label } = str.match(this.patterns.growiLink)!.groups!);
       link = label;
     }
 
@@ -93,7 +98,7 @@ export default class Linker {
   }
 
   // create an instance of Linker from text with index
-  static fromLineWithIndex(line, index) {
+  static fromLineWithIndex(line: string, index: number): Linker {
     const { beginningOfLink, endOfLink } = this.getBeginningAndEndIndexOfLink(line, index);
     // if index is in a link, extract it from line
     let linkStr = '';
@@ -103,11 +108,11 @@ export default class Linker {
     return this.fromMarkdownString(linkStr);
   }
 
-  // return beginning and end indexies of link
+  // return beginning and end indices of link
   // if index is not in a link, return { beginningOfLink: -1, endOfLink: -1 }
-  static getBeginningAndEndIndexOfLink(line, index) {
-    let beginningOfLink;
-    let endOfLink;
+  static getBeginningAndEndIndexOfLink(line: string, index: number): { beginningOfLink: number; endOfLink: number } {
+    let beginningOfLink: number;
+    let endOfLink: number;
 
     // pukiwiki link ('[[link]]')
     [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
@@ -130,13 +135,13 @@ export default class Linker {
     return { beginningOfLink, endOfLink };
   }
 
-  // return begin and end indexies as array only when index is between prefix and suffix and link contains containText.
-  static getBeginningAndEndIndexWithPrefixAndSuffix(line, index, prefix, suffix, containText = '') {
+  // return begin and end indices as an array only when index is between prefix and suffix and link contains containText.
+  static getBeginningAndEndIndexWithPrefixAndSuffix(line: string, index: number, prefix: string, suffix: string, containText = ''): [number, number] {
     const beginningIndex = line.lastIndexOf(prefix, index);
-    const IndexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
-    const endIndex = line.indexOf(suffix, IndexOfContainText + containText.length);
+    const indexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
+    const endIndex = line.indexOf(suffix, indexOfContainText + containText.length);
 
-    if (beginningIndex < 0 || IndexOfContainText < 0 || endIndex < 0) {
+    if (beginningIndex < 0 || indexOfContainText < 0 || endIndex < 0) {
       return [-1, -1];
     }
     return [beginningIndex, endIndex + suffix.length];