|
@@ -1,5 +1,9 @@
|
|
|
import type { MarkdownInstance } from "astro"
|
|
|
-import { compareByHas } from "src/utils/compare-by-has"
|
|
|
+import { readFileSync } from "node:fs"
|
|
|
+import { dirname, join } from "node:path"
|
|
|
+import { fileURLToPath } from "node:url"
|
|
|
+
|
|
|
+const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
|
interface Image {
|
|
|
src: string
|
|
@@ -29,22 +33,27 @@ export type PageProject = MarkdownInstance<
|
|
|
|
|
|
export type Project = BaseProject | PageProject
|
|
|
|
|
|
-export const sortProjects = (files: any[]): Project[] =>
|
|
|
- files.sort(({ frontmatter: a }, { frontmatter: b }) => {
|
|
|
- if (compareByHas(a, b, "index") !== 0)
|
|
|
- return compareByHas(a, b, "index")
|
|
|
-
|
|
|
- if (compareByHas(a, b, "date") !== 0) return compareByHas(a, b, "date")
|
|
|
- if (compareByHas(a, b, "time") !== 0) return compareByHas(a, b, "time")
|
|
|
-
|
|
|
- if (a.date && b.date) {
|
|
|
- const aDate = new Date(a.date)
|
|
|
- const bDate = new Date(b.date)
|
|
|
- return bDate.getMilliseconds() - aDate.getMilliseconds()
|
|
|
- }
|
|
|
-
|
|
|
- return 0
|
|
|
+const getId = ({ file }: MarkdownInstance<any>) =>
|
|
|
+ file.split("/").pop()?.slice(0, -3)
|
|
|
+
|
|
|
+export const sortProjects = (files: any[]): Project[] => {
|
|
|
+ const result = new Array(files.length)
|
|
|
+ const sorting = readFileSync(
|
|
|
+ join(dirname(__dirname), "content", "projects", "projects.txt"),
|
|
|
+ { encoding: "utf-8" }
|
|
|
+ )
|
|
|
+ .split("\n")
|
|
|
+ .map((line) => line.trim())
|
|
|
+
|
|
|
+ files.forEach((file) => {
|
|
|
+ const id = getId(file)
|
|
|
+ if (!id) return
|
|
|
+ const index = sorting.indexOf(id)
|
|
|
+ if (index < 0) return result.push(file)
|
|
|
+ result[index] = file
|
|
|
})
|
|
|
+ return result
|
|
|
+}
|
|
|
|
|
|
export const isProjectLink = (href: string) => href.startsWith("/")
|
|
|
|