jackyzha0 / quartz

🌱 a fast, batteries-included static-site generator that transforms Markdown content into fully functional websites
https://quartz.jzhao.xyz
MIT License
5.93k stars 2.37k forks source link

Allow custom sorting of FolderPage and TagPage #1250

Open cmj2002 opened 1 week ago

cmj2002 commented 1 week ago

Currently, users are unable to customize the way FolderPage and TagPage sort files, and can only sort by byDateAndAlphabetical. Users may sometimes need custom sorting methods, such as sorting by alphabetical order, custom key in frontmatter, or even manually specifying the order(#1227).

This pull request allows them to accept a sorting function to override the original sorting behavior.

Users can pass in a sorting function in quartz.config.ts. An example (sorting alphabetically) is:

import { QuartzConfig } from "./quartz/cfg"
import * as Plugin from "./quartz/plugins"
import { QuartzPluginData } from "./quartz/plugins/vfile"

const sortData: (f1: QuartzPluginData, f2: QuartzPluginData) => number = (f1, f2) => {
  const f1Title = f1.frontmatter?.title.toLowerCase() ?? ""
  const f2Title = f2.frontmatter?.title.toLowerCase() ?? ""
  return f1Title.localeCompare(f2Title)
}

const config: QuartzConfig = {
  ...
  plugins: {
    ...
    emitters: [
      ...
      Plugin.FolderPage({ sort: sortData }),
      Plugin.TagPage({ sort: sortData }),
      ...
    ],
  },
}

Full example is on example branch.