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

Move getUsernameByPath to page-path-util

Shun Miyazawa 2 лет назад
Родитель
Сommit
55cd4ae9f5

+ 3 - 1
apps/app/src/server/models/user.js

@@ -1,4 +1,6 @@
 /* eslint-disable no-use-before-define */
+import { pagePathUtils } from '@growi/core/dist/utils';
+
 import { i18n } from '^/config/next-i18next.config';
 
 import { generateGravatarSrc } from '~/utils/gravatar';
@@ -711,7 +713,7 @@ module.exports = function(crowi) {
   };
 
   userSchema.statics.isExistUserByUserPagePath = async function(path) {
-    const username = this.getUsernameByPath(path);
+    const username = pagePathUtils.getUsernameByPath(path);
 
     if (username == null) {
       return false;

+ 16 - 0
packages/core/src/utils/page-path-utils/index.ts

@@ -287,5 +287,21 @@ export const generateChildrenRegExp = (path: string): RegExp => {
   return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
 };
 
+/**
+ * Get username from user page path
+ * @param path string
+ * @returns string | null
+ */
+export const getUsernameByPath = (path: string): string | null => {
+  let username: string | null = null;
+  const match = path.match(/^\/user\/([^/]+)\/?/);
+  if (match) {
+    username = match[1];
+  }
+
+  return username;
+};
+
+
 export * from './is-top-page';
 export * from './collect-ancestor-paths';