import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import path from 'node:path' import type { GitPluginPageData } from '@vuepress/plugin-git' import { describe, expect, it } from 'vitest' import type { Page } from 'vuepress/core' import { appendDateToPage } from '../../src/node/appendDate.js' import { getDateString } from '../../src/node/formatDate.js' const CREATED_TIME = 1_700_000_000_000 const DATE_TEXT = getDateString(new Date(CREATED_TIME)) const createTempDir = (): Promise => mkdtemp(path.join(tmpdir(), 'vuepress-append-date-')) const writeTempFile = async ( dir: string, name: string, content: string, ): Promise => { const filePath = path.join(dir, name) await writeFile(filePath, content, 'utf-8') return filePath } const createPage = ( filePath: string, frontmatter: Record = {}, ): Page => ({ filePath, frontmatter, data: { git: { createdTime: CREATED_TIME } }, }) as unknown as Page // A single frontmatter block has exactly two delimiter lines const countFrontmatterDelimiters = (content: string): number => content .replace(/^\uFEFF/u, '') .split(/\r?\n/u) .filter((line) => line === '---').length // An LF that is not part of a CRLF means the EOL style was mixed const hasLoneLf = (content: string): boolean => content.replaceAll('\r\n', '').includes('\n') describe(appendDateToPage, () => { it('should prepend the date to LF frontmatter', async () => { const dir = await createTempDir() try { const original = '---\ntitle: Hello\nauthor: Test\n---\n\n# Heading\n\nBody text\n' const filePath = await writeTempFile(dir, 'lf.md', original) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `---\ndate: ${DATE_TEXT}\ntitle: Hello\nauthor: Test\n---\n\n# Heading\n\nBody text\n`, ) expect(countFrontmatterDelimiters(content)).toBe(2) expect(content).toContain('# Heading') expect(content).toContain('Body text') expect(page.frontmatter.date).toBeInstanceOf(Date) expect(page.frontmatter.date).toStrictEqual(new Date(CREATED_TIME)) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should prepend the date to CRLF frontmatter and keep CRLF', async () => { const dir = await createTempDir() try { const original = '---\r\ntitle: Hello\r\nauthor: Test\r\n---\r\n\r\n# Heading\r\n\r\nBody text\r\n' const filePath = await writeTempFile(dir, 'crlf.md', original) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `---\r\ndate: ${DATE_TEXT}\r\ntitle: Hello\r\nauthor: Test\r\n---\r\n\r\n# Heading\r\n\r\nBody text\r\n`, ) expect(countFrontmatterDelimiters(content)).toBe(2) expect(content).toContain('# Heading') expect(content).toContain('Body text') expect(content).toContain('\r\n') expect(hasLoneLf(content)).toBe(false) expect(page.frontmatter.date).toBeInstanceOf(Date) expect(page.frontmatter.date).toStrictEqual(new Date(CREATED_TIME)) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should prepend the date to BOM + LF frontmatter and keep the BOM', async () => { const dir = await createTempDir() try { const original = '\uFEFF---\ntitle: Hello\n---\n\n# Heading\n\nBody text\n' const filePath = await writeTempFile(dir, 'bom-lf.md', original) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `\uFEFF---\ndate: ${DATE_TEXT}\ntitle: Hello\n---\n\n# Heading\n\nBody text\n`, ) expect(countFrontmatterDelimiters(content)).toBe(2) expect(content.charCodeAt(0)).toBe(0xfe_ff) expect(content.indexOf('\uFEFF', 1)).toBe(-1) expect(content).toContain('# Heading') expect(page.frontmatter.date).toBeInstanceOf(Date) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should prepend the date to BOM + CRLF frontmatter and keep the BOM and CRLF', async () => { const dir = await createTempDir() try { const original = '\uFEFF---\r\ntitle: Hello\r\n---\r\n\r\n# Heading\r\n\r\nBody text\r\n' const filePath = await writeTempFile(dir, 'bom-crlf.md', original) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `\uFEFF---\r\ndate: ${DATE_TEXT}\r\ntitle: Hello\r\n---\r\n\r\n# Heading\r\n\r\nBody text\r\n`, ) expect(countFrontmatterDelimiters(content)).toBe(2) expect(content.charCodeAt(0)).toBe(0xfe_ff) expect(content.indexOf('\uFEFF', 1)).toBe(-1) expect(hasLoneLf(content)).toBe(false) expect(page.frontmatter.date).toBeInstanceOf(Date) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should create a frontmatter block when there is none', async () => { const dir = await createTempDir() try { const original = '# Heading\n\nBody text\n' const filePath = await writeTempFile(dir, 'no-frontmatter.md', original) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `---\ndate: ${DATE_TEXT}\n---\n\n# Heading\n\nBody text\n`, ) expect(countFrontmatterDelimiters(content)).toBe(2) expect(content.startsWith(`---\ndate: ${DATE_TEXT}\n---\n\n`)).toBe(true) expect(page.frontmatter.date).toBeInstanceOf(Date) expect(page.frontmatter.date).toStrictEqual(new Date(CREATED_TIME)) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should hoist a leading BOM in front of a new frontmatter block', async () => { const dir = await createTempDir() try { const original = '\uFEFF# Heading\n\nBody text\n' const filePath = await writeTempFile( dir, 'no-frontmatter-bom.md', original, ) const page = createPage(filePath) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `\uFEFF---\ndate: ${DATE_TEXT}\n---\n\n# Heading\n\nBody text\n`, ) expect(content.charCodeAt(0)).toBe(0xfe_ff) expect(content.indexOf('\uFEFF', 1)).toBe(-1) expect(countFrontmatterDelimiters(content)).toBe(2) expect(page.frontmatter.date).toBeInstanceOf(Date) } finally { await rm(dir, { force: true, recursive: true }) } }) it('should use the configured key for the appended date', async () => { const dir = await createTempDir() try { const original = '---\ntitle: Hello\n---\n\nBody text\n' const filePath = await writeTempFile(dir, 'custom-key.md', original) const page = createPage(filePath) await appendDateToPage({ key: 'updated' }, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe( `---\nupdated: ${DATE_TEXT}\ntitle: Hello\n---\n\nBody text\n`, ) expect(page.frontmatter.updated).toBeInstanceOf(Date) expect(page.frontmatter.date).toBeUndefined() } finally { await rm(dir, { force: true, recursive: true }) } }) it('should not touch the file when the frontmatter key is already set', async () => { const dir = await createTempDir() try { const original = '---\r\ndate: 2020-01-01\r\ntitle: Hello\r\n---\r\n\r\nBody text\r\n' const filePath = await writeTempFile(dir, 'existing-date.md', original) const existingDate = new Date('2020-01-01') const page = createPage(filePath, { date: existingDate }) await appendDateToPage({}, page) const content = await readFile(filePath, 'utf-8') expect(content).toBe(original) expect(countFrontmatterDelimiters(content)).toBe(2) expect(page.frontmatter.date).toBe(existingDate) expect(page.frontmatter.date).not.toStrictEqual(new Date(CREATED_TIME)) } finally { await rm(dir, { force: true, recursive: true }) } }) })