Pārlūkot izejas kodu

define patterns as static verience and use capturing of regex

yusuketk 5 gadi atpakaļ
vecāks
revīzija
34518d1895
1 mainītis faili ar 21 papildinājumiem un 24 dzēšanām
  1. 21 24
      src/client/js/models/Linker.js

+ 21 - 24
src/client/js/models/Linker.js

@@ -13,6 +13,13 @@ export default class Linker {
     pukiwikiLink: 'pukiwikiLink',
   }
 
+  static patterns = {
+    pukiwikiLinkWithLabel: /^\[\[(?<label>.+)>(?<link>.+)\]\]$/, // https://regex101.com/r/2fNmUN/2
+    pukiwikiLinkWithoutLabel: /^\[\[(?<label>.+)\]\]$/, // https://regex101.com/r/S7w5Xu/1
+    growiLink: /^\[(?<label>\/.+)\]$/, // https://regex101.com/r/DJfkYf/3
+    markdownLink: /^\[(?<label>.*)\]\((?<link>.*)\)$/, // https://regex101.com/r/DZCKP3/2
+  }
+
   // create an instance of Linker from string
   static fromMarkdownString(str) {
     // if str doesn't mean a linker, create a link whose label is str
@@ -20,37 +27,27 @@ export default class Linker {
     let link = '';
     let type = this.types.markdownLink;
 
-    // pukiwiki
-    // https://regex101.com/r/2fNmUN/1
-    if (str.match(/^\[\[.*\]\]$/)) {
+    // pukiwiki with separator ">".
+    if (str.match(this.patterns.pukiwikiWithLabel)) {
+      type = this.types.pukiwikiLink;
+      ({ label, link } = str.match(this.patterns.pukiwikiWithLabel).groups);
+    }
+    // pukiwiki without separator ">".
+    else if (str.match(this.patterns.pukiwikiLinkWithoutLabel)) {
       type = this.types.pukiwikiLink;
-      const value = str.slice(2, -2);
-      const indexOfSplit = value.lastIndexOf('>');
-      if (indexOfSplit < 0) {
-        label = value;
-        link = value;
-      }
-      else {
-        label = value.slice(0, indexOfSplit);
-        link = value.slice(indexOfSplit + 1);
-      }
+      ({ label } = str.match(this.patterns.pukiwikiLinkWithoutLabel).groups);
+      link = label;
     }
     // growi
-    // https://regex101.com/r/DJfkYf/1
-    else if (str.match(/^\[\/.*\]$/)) {
+    else if (str.match(this.patterns.growiLink)) {
       type = this.types.growiLink;
-      const value = str.slice(1, -1);
-      label = value;
-      link = value;
+      ({ label } = str.match(this.patterns.growiLink).groups);
+      link = label;
     }
     // markdown
-    // https://regex101.com/r/DZCKP3/1
-    else if (str.match(/^\[.*\]\(.*\)$/)) {
+    else if (str.match(this.patterns.markdownLink)) {
       type = this.types.markdownLink;
-      const value = str.slice(1, -1);
-      const indexOfSplit = value.lastIndexOf('](');
-      label = value.slice(0, indexOfSplit);
-      link = value.slice(indexOfSplit + 2);
+      ({ label, link } = str.match(this.patterns.markdownLink).groups);
     }
 
     return new Linker(type, label, link);