Przeglądaj źródła

move method to utility

yohei0125 4 lat temu
rodzic
commit
3758f4195a

+ 1 - 14
packages/app/src/server/models/page.ts

@@ -13,7 +13,7 @@ import Crowi from '../crowi';
 import { IPage } from '../../interfaces/page';
 import { getPageSchema, PageQueryBuilder } from './obsolete-page';
 
-const { isTopPage } = pagePathUtils;
+const { isTopPage, collectAncestorPaths } = pagePathUtils;
 
 const logger = loggerFactory('growi:models:page');
 
@@ -95,19 +95,6 @@ const schema = new Schema<PageDocument, PageModel>({
 schema.plugin(mongoosePaginate);
 schema.plugin(uniqueValidator);
 
-
-/*
- * Methods
- */
-export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
-  if (isTopPage(path)) return ancestorPaths;
-
-  const parentPath = nodePath.dirname(path);
-  ancestorPaths.push(parentPath);
-  return collectAncestorPaths(parentPath, ancestorPaths);
-};
-
-
 const hasSlash = (str: string): boolean => {
   return str.includes('/');
 };

+ 4 - 2
packages/app/src/server/service/page.js

@@ -1,7 +1,7 @@
 import { pagePathUtils } from '@growi/core';
 
 import loggerFactory from '~/utils/logger';
-import { generateGrantCondition, collectAncestorPaths } from '~/server/models/page';
+import { generateGrantCondition } from '~/server/models/page';
 
 import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
 
@@ -17,7 +17,9 @@ const debug = require('debug')('growi:services:page');
 const { Writable } = require('stream');
 const { createBatchStream } = require('~/server/util/batch-stream');
 
-const { isCreatablePage, isDeletablePage, isTrashPage } = pagePathUtils;
+const {
+  isCreatablePage, isDeletablePage, isTrashPage, collectAncestorPaths,
+} = pagePathUtils;
 const { serializePageSecurely } = require('../models/serializers/page-serializer');
 
 const BULK_REINDEX_SIZE = 100;

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

@@ -1,3 +1,5 @@
+import nodePath from 'path';
+
 import escapeStringRegexp from 'escape-string-regexp';
 
 /**
@@ -132,3 +134,17 @@ export const generateEditorPath = (...paths: string[]): string => {
     throw new Error('Invalid path format');
   }
 };
+
+/**
+ * returns ancestors paths
+ * @param {string} path
+ * @param {string[]} ancestorPaths
+ * @returns {string[]}
+ */
+export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
+  if (isTopPage(path)) return ancestorPaths;
+
+  const parentPath = nodePath.dirname(path);
+  ancestorPaths.push(parentPath);
+  return collectAncestorPaths(parentPath, ancestorPaths);
+};