yusuketk 5 лет назад
Родитель
Сommit
c8f57aaa93

+ 3 - 1
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -34,7 +34,8 @@ class LinkEditModal extends React.PureComponent {
       markdown: '',
     };
 
-    this.isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
+    // this.isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
+    this.isApplyPukiwikiLikeLinkerPlugin = true;
 
     this.show = this.show.bind(this);
     this.hide = this.hide.bind(this);
@@ -58,6 +59,7 @@ class LinkEditModal extends React.PureComponent {
   }
 
   show(defaultMarkdownLink = null) {
+    console.log(defaultMarkdownLink);
     // if defaultMarkdownLink is null, set default value in inputs.
     const {type='mdLink', label='', link=''} = defaultMarkdownLink ;
 

+ 4 - 3
src/client/js/components/PageEditor/MarkdownLinkUtil.js

@@ -1,4 +1,5 @@
-import Linker from '../models/Linker';
+import Linker from '../../models/Linker';
+import { ConnectionStates } from 'mongoose';
 
 /**
  * Utility for markdown link
@@ -17,13 +18,13 @@ class MarkdownLinkUtil {
       return Linker.fromMarkdownString(editor.getDoc().getSelection());
     }
     const curPos = editor.getCursor();
-    return Linker.fromLineContainsLink(editor.getDoc().getLine(curPos.line), curPos.ch)
+    return Linker.fromLineWithIndex(editor.getDoc().getLine(curPos.line), curPos.ch)
   }
 
   isInLink(editor) {
     const curPos = editor.getCursor();
     const { beginningOfLink, endOfLink } = Linker.getBeginningAndEndIndexOfLink(editor.getDoc().getLine(curPos.line), curPos.ch);
-    return beginningOfLink >= 0 && endOfLink >= 0 && beginningOfLink <= curPos.ch && curPos.ch <= endOfLink;
+    return beginningOfLink >= 0 && endOfLink >= 0;
   }
 
   replaceFocusedMarkdownLinkWithEditor(editor) {

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

@@ -1,10 +1,13 @@
+import { sl } from "date-fns/locale";
+
 const types = {
   markdownLink: 'mdLink',
   growiLink: 'growiLink',
   pukiwikiLink: 'pukiwikiLink',
 }
 
-const isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
+// const isApplyPukiwikiLikeLinkerPlugin = window.growiRenderer.preProcessors.some(process => process.constructor.name === 'PukiwikiLikeLinker');
+const isApplyPukiwikiLikeLinkerPlugin = true;
 
 export default class Linker {
 
@@ -46,7 +49,7 @@ export default class Linker {
     // https://regex101.com/r/DZCKP3/1
     else if (str.match(/^\[.*\]\(.*\)$/)) {
       type = types.markdownLink;
-      const value = MarkdownLink.slice(1, -1);
+      const value = str.slice(1, -1);
       const indexOfSplit = value.lastIndexOf('](');
       label = value.slice(0, indexOfSplit);
       link = value.slice(indexOfSplit + 2);
@@ -59,46 +62,52 @@ export default class Linker {
   static fromLineWithIndex(line, index) {
     const { beginningOfLink, endOfLink } = this.getBeginningAndEndIndexOfLink(line, index);
     let linkStr = '';
-    if (beginningOfLink < 0 || endOfLink < 0) {
+    if (beginningOfLink >= 0 && endOfLink >= 0) {
      linkStr = line.substring(beginningOfLink, endOfLink);
     }
+    console.log(linkStr);
+
     return this.fromMarkdownString(linkStr);
   }
 
-  // return beginning index and end index of the closest link to index
-  // if there is no link, return { beginningOfLink: -1, endOfLink: -1}
+  // return beginning and end indexies of link
+  // if index is not on link, return { beginningOfLink: -1, endOfLink: -1 }
   static getBeginningAndEndIndexOfLink(line, index) {
     let beginningOfLink, endOfLink;
 
-    // growi link ('[/link]')
-    [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[/', ']');
-
-    // markdown link ('[label](link)')
-    [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[', ')', '](');
-
     // pukiwiki link ('[[link]]')
     if (isApplyPukiwikiLikeLinkerPlugin) {
       [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[[', ']]');
     }
 
+    // growi link ('[/link]')
+    if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index){
+      [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[/', ']');
+    }
+
+    // markdown link ('[label](link)')
+    if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index){
+      [beginningOfLink, endOfLink] = this.getBeginningAndEndIndexWithPrefixAndSuffix(line, index, '[', ')', '](');
+    }
+
+    if (beginningOfLink < 0 || endOfLink < 0 || beginningOfLink > index || endOfLink < index) {
+      [beginningOfLink, endOfLink] = [-1, -1];
+    };
+
     return { beginningOfLink, endOfLink };
   }
 
   // return begin and end indexies as array only when index between prefix and suffix.
   // if line doesn't contain containText, return null.
-  static getBeginningAndEndIndexWithPrefixAndSuffix(line, index, prefix, suffix, containText=null) {
-    const beginningIndex = line.lastIndexOf(prefix, index + prefix.length);
-    let startIndex = beginningIndex;
-    if (containText != null){
-      startIndex = line.indexOf(containText, beginningOfLink);
-    }
-    const endIndex = line.indexOf(suffix, startIndex);
+  static getBeginningAndEndIndexWithPrefixAndSuffix(line, index, prefix, suffix, containText='') {
+    const beginningIndex = line.lastIndexOf(prefix, index);
+    const IndexOfContainText = line.indexOf(containText, beginningIndex + prefix.length);
+    const endIndex = line.indexOf(suffix, IndexOfContainText + containText.length);
 
-    if (beginningIndex < 0 || endIndex < 0 || startIndex < 0) {
+    if (beginningIndex < 0 || IndexOfContainText < 0 || endIndex < 0) {
       return [-1, -1];
     }
-
-    return [beginningIndex, endIndex + suffix.lengt];
+    return [beginningIndex, endIndex + suffix.length];
   }
 
 }