Explorar o código

refactor path-utils

Yuki Takei %!s(int64=7) %!d(string=hai) anos
pai
achega
7de8d934d4

+ 1 - 1
src/client/js/app.js

@@ -281,7 +281,7 @@ const componentMappings = {
   'bookmark-button': <BookmarkButton pageId={pageId} crowi={crowi} />,
   'bookmark-button-lg': <BookmarkButton pageId={pageId} crowi={crowi} size="lg" />,
 
-  'create-page-name-input': <PagePathAutoComplete crowi={crowi} initializedPath={pagePath} addSlashToTheEnd={true} />,
+  'create-page-name-input': <PagePathAutoComplete crowi={crowi} initializedPath={pagePath} addTrailingSlash={true} />,
   'rename-page-name-input': <PagePathAutoComplete crowi={crowi} initializedPath={pagePath} />,
   'duplicate-page-name-input': <PagePathAutoComplete crowi={crowi} initializedPath={pagePath} />,
 

+ 4 - 4
src/client/js/components/PagePathAutoComplete.jsx

@@ -34,9 +34,9 @@ export default class PagePathAutoComplete extends React.Component {
   }
 
   getKeywordOnInit(path) {
-    return this.props.addSlashToTheEnd
-      ? pathUtils.addSlashToTheEnd(path)
-      : pathUtils.removeLastSlash(path);
+    return this.props.addTrailingSlash
+      ? pathUtils.addTrailingSlash(path)
+      : pathUtils.removeTrailingSlash(path);
   }
 
   render() {
@@ -59,7 +59,7 @@ export default class PagePathAutoComplete extends React.Component {
 PagePathAutoComplete.propTypes = {
   crowi:            PropTypes.object.isRequired,
   initializedPath:  PropTypes.string,
-  addSlashToTheEnd: PropTypes.bool,
+  addTrailingSlash: PropTypes.bool,
 };
 
 PagePathAutoComplete.defaultProps = {

+ 10 - 10
src/lib/util/path-utils.js

@@ -18,40 +18,40 @@ function encodePagePath(path) {
   return paths.join('/');
 }
 
-function matchEndWithSlash(path) {
+function matchTrailingSlash(path) {
   // https://regex101.com/r/Z21fEd/1
   return path.match(/(.+?)(\/)?$/);
 }
 
-function isEndWithSlash(path) {
-  const match = matchEndWithSlash(path);
+function hasTrailingSlash(path) {
+  const match = matchTrailingSlash(path);
   return (match[2] != null);
 }
 
-function addSlashToTheEnd(path) {
+function addTrailingSlash(path) {
   if (path === '/') {
     return path;
   }
 
-  if (!isEndWithSlash(path)) {
+  if (!hasTrailingSlash(path)) {
     return `${path}/`;
   }
   return path;
 }
 
-function removeLastSlash(path) {
+function removeTrailingSlash(path) {
   if (path === '/') {
     return path;
   }
 
-  const match = matchEndWithSlash(path);
+  const match = matchTrailingSlash(path);
   return match[1];
 }
 
 module.exports = {
   encodePagePath,
   encodePagesPath,
-  isEndWithSlash,
-  addSlashToTheEnd,
-  removeLastSlash,
+  hasTrailingSlash,
+  addTrailingSlash,
+  removeTrailingSlash,
 };

+ 1 - 1
src/server/service/config-manager.js

@@ -81,7 +81,7 @@ class ConfigManager {
   getSiteUrl() {
     const siteUrl = this.getConfig('crowi', 'app:siteUrl');
     if (siteUrl != null) {
-      return pathUtils.removeLastSlash(siteUrl);
+      return pathUtils.removeTrailingSlash(siteUrl);
     }
     else {
       return '[The site URL is not set. Please set it!]';

+ 4 - 6
src/server/util/middlewares.js

@@ -1,8 +1,10 @@
 const debug = require('debug')('growi:lib:middlewares');
 const logger = require('@alias/logger')('growi:lib:middlewares');
+const pathUtils = require('@commons/util/path-utils');
 const md5 = require('md5');
 const entities = require('entities');
 
+
 exports.csrfKeyGenerator = function(crowi, app) {
   return function(req, res, next) {
     var csrfKey = (req.session && req.session.id) || 'anon';
@@ -145,12 +147,8 @@ exports.swigFilters = function(crowi, app, swig) {
         .replace(/\n/g, '<br>');
     });
 
-    swig.setFilter('removeLastSlash', function(string) {
-      if (string == '/') {
-        return string;
-      }
-
-      return string.substr(0, string.length - 1);
+    swig.setFilter('removeTrailingSlash', function(string) {
+      return pathUtils.removeTrailingSlash(string);
     });
 
     swig.setFilter('presentation', function(string) {

+ 4 - 4
src/server/views/modal/what_is_portal.html

@@ -67,12 +67,12 @@
 
         <strong>Warning!</strong><br>
 
-        <p>既に <strong><a href="{{ path|removeLastSlash }}">{{ path|removeLastSlash }}</a></strong> のページが存在します。</p>
+        <p>既に <strong><a href="{{ path|removeTrailingSlash }}">{{ path|removeTrailingSlash }}</a></strong> のページが存在します。</p>
 
         <p>
-          <a href="{{ path|removeLastSlash }}">{{ path|removeLastSlash }}</a> をポータル化するには、
-          <a href="{{ path|removeLastSlash }}">{{ path|removeLastSlash }}</a> に移動し、「ページを移動」させてください。<br>
-          <a href="{{ path|removeLastSlash }}">{{ path|removeLastSlash }}</a> とは別に、このページ(<code>{{ path }}</code>)にポータルを作成する場合、このまま編集を続けて作成してください。
+          <a href="{{ path|removeTrailingSlash }}">{{ path|removeTrailingSlash }}</a> をポータル化するには、
+          <a href="{{ path|removeTrailingSlash }}">{{ path|removeTrailingSlash }}</a> に移動し、「ページを移動」させてください。<br>
+          <a href="{{ path|removeTrailingSlash }}">{{ path|removeTrailingSlash }}</a> とは別に、このページ(<code>{{ path }}</code>)にポータルを作成する場合、このまま編集を続けて作成してください。
         </p>
 
       </div>