Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nextra] sort defaultMeta by frontMatter.date, if missing by frontMatter.title and after by capitalized page name + TESTS 🧪 #894

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-papayas-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra': patch
---

sort `defaultMeta` by `frontMatter.date`, if missing by `frontMatter.title` and after by page name
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved
56 changes: 56 additions & 0 deletions packages/nextra/__test__/sort-pages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest'
import { sortPages } from '../src/utils'

describe('sortPages()', () => {
it('should should sort by date', () => {
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved
const data = sortPages([
{ name: 'baz', frontMatter: { date: new Date('1995-10-21') } },
{ name: 'foo', frontMatter: { date: new Date('1992-10-21') } },
{ name: 'quz', frontMatter: { date: new Date('1998-10-21') } }
])
expect(data).toEqual([
['quz', 'Quz'],
['baz', 'Baz'],
['foo', 'Foo']
])
})

it('should should sort by date first and after by title', () => {
dimaMachina marked this conversation as resolved.
Show resolved Hide resolved
const data = sortPages([
{ name: 'quz' },
{ name: 'foo', frontMatter: { date: new Date('1992-10-21') } },
{ name: 'baz' }
])
expect(data).toEqual([
['foo', 'Foo'],
['baz', 'Baz'],
['quz', 'Quz']
])
})

it('should take priority `frontMatter.title` over name', () => {
const data = sortPages([
{ name: 'baz' },
{ name: 'foo', frontMatter: { title: 'abc' } },
{ name: 'quz' }
])
expect(data).toEqual([
['foo', 'abc'],
['baz', 'Baz'],
['quz', 'Quz']
])
})

it('should sort numeric', () => {
const data = sortPages([
{ name: '10-baz' },
{ name: '0-foo' },
{ name: '2.5-quz' }
])
expect(data).toEqual([
['0-foo', '0 Foo'],
['2.5-quz', '2.5 Quz'],
['10-baz', '10 Baz']
])
})
})
12 changes: 5 additions & 7 deletions packages/nextra/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './types'
import fs from 'graceful-fs'
import { promisify } from 'node:util'
import { parseFileName, parseJsonFile, truthy } from './utils'
import { parseFileName, parseJsonFile, sortPages, truthy } from './utils'
import path from 'node:path'
import slash from 'slash'
import grayMatter from 'gray-matter'
Expand Down Expand Up @@ -106,15 +106,13 @@ export async function collectFiles(
const metaIndex = items.findIndex(
item => item.kind === 'Meta' && item.locale === locale
)
const defaultMeta: [string, string][] = mdxPages
.filter(item => item.locale === locale)
.map(item => [
item.name,
item.frontMatter?.title || title(item.name.replace(/[-_]/g, ' '))
])

const defaultMeta = sortPages(mdxPages, locale)

const metaFilename = locale
? META_FILENAME.replace('.', `.${locale}.`)
: META_FILENAME

const metaPath = path.join(dir, metaFilename) as MetaJsonPath

if (metaIndex === -1) {
Expand Down
29 changes: 28 additions & 1 deletion packages/nextra/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import title from 'title'
import { LOCALE_REGEX } from './constants'
import { Meta } from './types'
import { MdxFile, Meta } from './types'

export function parseFileName(filePath: string): {
name: string
Expand Down Expand Up @@ -41,3 +42,29 @@ export function truthy<T>(value: T): value is Truthy<T> {
export function normalizeMeta(meta: Meta): Exclude<Meta, string> {
return typeof meta === 'string' ? { title: meta } : meta
}

export function sortPages(
pages: Pick<MdxFile, 'name' | 'frontMatter' | 'locale'>[],
locale?: string
): [string, string][] {
return pages
.filter(item => item.locale === locale)
.map(item => ({
name: item.name,
date: item.frontMatter?.date,
title: item.frontMatter?.title || title(item.name.replace(/[-_]/g, ' '))
}))
.sort((a, b) => {
if (a.date && b.date) {
return new Date(b.date).getTime() - new Date(a.date).getTime()
}
if (a.date) {
return -1 // sort a before b
}
if (b.date) {
return 1 // sort a after b
}
return a.title.localeCompare(b.title, locale, { numeric: true })
})
.map(item => [item.name, item.title])
}