Explorar el Código

improve regular expression with constant

kosei-n hace 2 años
padre
commit
558d179d86

+ 6 - 1
apps/app/src/components/PageHeader/PageTitleHeader.tsx

@@ -9,11 +9,13 @@ import { pathUtils } from '@growi/core/dist/utils';
 import { useTranslation } from 'next-i18next';
 import { useTranslation } from 'next-i18next';
 
 
 import { ValidationTarget } from '~/client/util/input-validator';
 import { ValidationTarget } from '~/client/util/input-validator';
+import { basePathname } from '~/server/routes/apiv3/page/create-page';
 
 
 import ClosableTextInput from '../Common/ClosableTextInput';
 import ClosableTextInput from '../Common/ClosableTextInput';
 import { CopyDropdown } from '../Common/CopyDropdown';
 import { CopyDropdown } from '../Common/CopyDropdown';
 import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
 import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
 
 
+
 import styles from './PageTitleHeader.module.scss';
 import styles from './PageTitleHeader.module.scss';
 
 
 const moduleClass = styles['page-title-header'];
 const moduleClass = styles['page-title-header'];
@@ -40,7 +42,10 @@ export const PageTitleHeader: FC<Props> = (props) => {
   const editedPageTitle = nodePath.basename(editedPagePath);
   const editedPageTitle = nodePath.basename(editedPagePath);
 
 
   // https://regex101.com/r/Wg2Hh6/1
   // https://regex101.com/r/Wg2Hh6/1
-  const untitledPageRegex = /^Untitled-\d+$/;
+  // const untitledPageRegex = /^Untitled-\d+$/;
+
+  const untitledPageRegex = new RegExp(`/^${basePathname}-\d+$/`);
+
   const isNewlyCreatedPage = (currentPage.wip && currentPage.latestRevision == null && untitledPageRegex.test(editedPageTitle)) ?? false;
   const isNewlyCreatedPage = (currentPage.wip && currentPage.latestRevision == null && untitledPageRegex.test(editedPageTitle)) ?? false;
 
 
   const onRenameFinish = useCallback(() => {
   const onRenameFinish = useCallback(() => {

+ 3 - 2
apps/app/src/server/routes/apiv3/page/create-page.ts

@@ -41,9 +41,10 @@ async function generateUntitledPath(parentPath: string, basePathname: string, in
   return path;
   return path;
 }
 }
 
 
+// TODO: i18n
+export const basePathname = 'Untitled';
+
 async function determinePath(_parentPath?: string, _path?: string, optionalParentPath?: string): Promise<string> {
 async function determinePath(_parentPath?: string, _path?: string, optionalParentPath?: string): Promise<string> {
-  // TODO: i18n
-  const basePathname = 'Untitled';
 
 
   if (_path != null) {
   if (_path != null) {
     const path = normalizePath(_path);
     const path = normalizePath(_path);