Shun Miyazawa 4 лет назад
Родитель
Сommit
cb1528e8d7
1 измененных файлов с 29 добавлено и 0 удалено
  1. 29 0
      packages/core/src/utils/page-path-utils.ts

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

@@ -1,4 +1,5 @@
 import escapeStringRegexp from 'escape-string-regexp';
+import path from 'path';
 
 /**
  * Whether path is the top page
@@ -132,3 +133,31 @@ export const generateEditorPath = (...paths: string[]): string => {
     throw new Error('Invalid path format');
   }
 };
+
+/**
+ * Get the child path
+ * @param {string} path
+ * @returns {string}
+ */
+export const path2name = (path: string): string => {
+  const name = path;
+
+  // /.../YYYY/MM/DD
+  if (name.match(/^.*?([^/]+\/\d{4}\/\d{2}\/\d{2}\/?)$/)) {
+    return name.replace(/^.*?([^/]+\/\d{4}\/\d{2}\/\d{2}\/?)$/, '$1');
+  }
+
+  // /.../YYYY/MM
+  if (name.match(/^.*?([^/]+\/\d{4}\/\d{2}\/?)$/)) {
+    return name.replace(/^.*?([^/]+\/\d{4}\/\d{2}\/?)$/, '$1');
+  }
+
+  // /.../YYYY
+  if (name.match(/^.*?([^/]+\/\d{4}\/?)$/)) {
+    return name.replace(/^.*?([^/]+\/\d{4}\/?)$/, '$1');
+  }
+
+  // Pick up the end of the page
+  const suffix = name.replace(/.*\/(.+\/?)$/, '$1');
+  return suffix || name;
+};