編集コード

  • quartz.config.ts

タイトルが数字で始まる場合はその数字の昇順でソートする

Plugin.FolderPage({
  // Custom sort function
  // Sort by number if the title starts with a digit, otherwise sort by update date.
  sort: (a, b) => {
    const titleA = a.frontmatter?.title ?? "";
    const titleB = b.frontmatter?.title ?? "";
    let numA = -1, numB = -1
 
    const regex = /^([0-9]+)\s*[\.|-|:|_]/;
    const matchA = titleA.match(regex);
    const matchB = titleB.match(regex);
    const removeLeadingZeros = (str: string) => str.replace(/^0+/, '') || '0';
 
    if (matchA) {
      const digitA = removeLeadingZeros(matchA[1]); // Remove leading zeros
      numA = parseInt(digitA, 10);
    }
 
    if (matchB) {
      const digitB = removeLeadingZeros(matchB[1]); // Remove leading zeros
      numB = parseInt(digitB, 10);
    }
 
    if (numA !== -1 && numB !== -1) {
      return numA - numB;
    }
 
    // Sort by modified date if no leading numbers
    const dateA = a.dates?.modified ?? new Date(0);
    const dateB = b.dates?.modified ?? new Date(0);
    return dateB.getTime() - dateA.getTime();
  },
}),