Просмотр исходного кода

Merge pull request #2503 from weseek/feat/refactor-permalink-in-linker

Feat/refactor permalink in linker
Yuki Takei 5 лет назад
Родитель
Сommit
d8b14f4837
2 измененных файлов с 20 добавлено и 21 удалено
  1. 16 5
      src/client/js/components/PageEditor/LinkEditModal.jsx
  2. 4 16
      src/client/js/models/Linker.js

+ 16 - 5
src/client/js/components/PageEditor/LinkEditModal.jsx

@@ -8,6 +8,7 @@ import {
   ModalFooter,
   ModalFooter,
 } from 'reactstrap';
 } from 'reactstrap';
 
 
+import path from 'path';
 import Preview from './Preview';
 import Preview from './Preview';
 
 
 import AppContainer from '../../services/AppContainer';
 import AppContainer from '../../services/AppContainer';
@@ -62,14 +63,20 @@ class LinkEditModal extends React.PureComponent {
   // defaultMarkdownLink is an instance of Linker
   // defaultMarkdownLink is an instance of Linker
   show(defaultMarkdownLink = null) {
   show(defaultMarkdownLink = null) {
     // if defaultMarkdownLink is null, set default value in inputs.
     // if defaultMarkdownLink is null, set default value in inputs.
-    const { label = '', link = '' } = defaultMarkdownLink;
-    let { type = Linker.types.markdownLink } = defaultMarkdownLink;
+    const { pageContainer } = this.props;
+    const { label = '' } = defaultMarkdownLink;
+    let { link = '', type = Linker.types.markdownLink } = defaultMarkdownLink;
 
 
     // if type of defaultMarkdownLink is pukiwikiLink when pukiwikiLikeLinker plugin is disable, change type(not change label and link)
     // if type of defaultMarkdownLink is pukiwikiLink when pukiwikiLikeLinker plugin is disable, change type(not change label and link)
     if (type === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
     if (type === Linker.types.pukiwikiLink && !this.isApplyPukiwikiLikeLinkerPlugin) {
       type = Linker.types.markdownLink;
       type = Linker.types.markdownLink;
     }
     }
 
 
+    const isUseRelativePath = link.startsWith('.');
+    if (isUseRelativePath) {
+      link = path.resolve(pageContainer.state.path, link);
+    }
+
     this.setState({
     this.setState({
       show: true,
       show: true,
       labelInputValue: label,
       labelInputValue: label,
@@ -77,6 +84,7 @@ class LinkEditModal extends React.PureComponent {
       isUsePermanentLink: false,
       isUsePermanentLink: false,
       permalink: '',
       permalink: '',
       linkerType: type,
       linkerType: type,
+      isUseRelativePath,
     });
     });
   }
   }
 
 
@@ -177,12 +185,15 @@ class LinkEditModal extends React.PureComponent {
       permalink,
       permalink,
     } = this.state;
     } = this.state;
 
 
+    let reshapedLink = linkInputValue;
+    if (isUseRelativePath && linkInputValue.match(/^\//)) {
+      reshapedLink = path.relative(pageContainer.state.path, linkInputValue);
+    }
+
     return new Linker(
     return new Linker(
       linkerType,
       linkerType,
       labelInputValue,
       labelInputValue,
-      linkInputValue,
-      isUseRelativePath,
-      pageContainer.state.path,
+      reshapedLink,
       isUsePermanentLink,
       isUsePermanentLink,
       permalink,
       permalink,
     );
     );

+ 4 - 16
src/client/js/models/Linker.js

@@ -1,21 +1,15 @@
-import path from 'path';
-
 export default class Linker {
 export default class Linker {
 
 
   constructor(
   constructor(
       type,
       type,
       label,
       label,
       link,
       link,
-      isUseRelativePath = false,
-      rootPath = '',
       isUsePermanentLink = false,
       isUsePermanentLink = false,
       permalink = '',
       permalink = '',
   ) {
   ) {
     this.type = type;
     this.type = type;
     this.label = label;
     this.label = label;
     this.link = link;
     this.link = link;
-    this.isUseRelativePath = isUseRelativePath;
-    this.rootPath = rootPath;
     this.isUsePermanentLink = isUsePermanentLink;
     this.isUsePermanentLink = isUsePermanentLink;
     this.permalink = permalink;
     this.permalink = permalink;
 
 
@@ -38,9 +32,6 @@ export default class Linker {
   generateMarkdownText() {
   generateMarkdownText() {
     let reshapedLink = this.link;
     let reshapedLink = this.link;
 
 
-    if (this.isUseRelativePath && this.link.match(/^\//)) {
-      reshapedLink = path.relative(this.rootPath, this.link);
-    }
     if (this.isUsePermanentLink && this.permalink != null) {
     if (this.isUsePermanentLink && this.permalink != null) {
       reshapedLink = this.permalink;
       reshapedLink = this.permalink;
     }
     }
@@ -87,18 +78,15 @@ export default class Linker {
       ({ label, link } = str.match(this.patterns.markdownLink).groups);
       ({ label, link } = str.match(this.patterns.markdownLink).groups);
     }
     }
 
 
-    // TODO GW-3074 相対パスを利用しているかテキストから判定し以下の値に反映する
-    const isUseRelativePath = false;
-    const rootPath = '';
+    const isUsePermanentLink = false;
+    const permalink = '';
 
 
     return new Linker(
     return new Linker(
       type,
       type,
       label,
       label,
       link,
       link,
-      isUseRelativePath,
-      rootPath,
-      false,
-      '',
+      isUsePermanentLink,
+      permalink,
     );
     );
   }
   }