@jasonshimmy/vite-plugin-cer-app 0.19.3 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/commits.txt +1 -1
  3. package/dist/cli/commands/preview.d.ts.map +1 -1
  4. package/dist/cli/commands/preview.js +2 -0
  5. package/dist/cli/commands/preview.js.map +1 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/plugin/build-ssg.d.ts.map +1 -1
  9. package/dist/plugin/build-ssg.js +11 -0
  10. package/dist/plugin/build-ssg.js.map +1 -1
  11. package/dist/plugin/content/emitter.d.ts +19 -0
  12. package/dist/plugin/content/emitter.d.ts.map +1 -0
  13. package/dist/plugin/content/emitter.js +42 -0
  14. package/dist/plugin/content/emitter.js.map +1 -0
  15. package/dist/plugin/content/index.d.ts +32 -0
  16. package/dist/plugin/content/index.d.ts.map +1 -0
  17. package/dist/plugin/content/index.js +199 -0
  18. package/dist/plugin/content/index.js.map +1 -0
  19. package/dist/plugin/content/parser.d.ts +18 -0
  20. package/dist/plugin/content/parser.d.ts.map +1 -0
  21. package/dist/plugin/content/parser.js +221 -0
  22. package/dist/plugin/content/parser.js.map +1 -0
  23. package/dist/plugin/content/path-utils.d.ts +19 -0
  24. package/dist/plugin/content/path-utils.d.ts.map +1 -0
  25. package/dist/plugin/content/path-utils.js +40 -0
  26. package/dist/plugin/content/path-utils.js.map +1 -0
  27. package/dist/plugin/content/scanner.d.ts +12 -0
  28. package/dist/plugin/content/scanner.d.ts.map +1 -0
  29. package/dist/plugin/content/scanner.js +18 -0
  30. package/dist/plugin/content/scanner.js.map +1 -0
  31. package/dist/plugin/content/search.d.ts +9 -0
  32. package/dist/plugin/content/search.d.ts.map +1 -0
  33. package/dist/plugin/content/search.js +24 -0
  34. package/dist/plugin/content/search.js.map +1 -0
  35. package/dist/plugin/dts-generator.d.ts.map +1 -1
  36. package/dist/plugin/dts-generator.js +10 -1
  37. package/dist/plugin/dts-generator.js.map +1 -1
  38. package/dist/plugin/index.d.ts.map +1 -1
  39. package/dist/plugin/index.js +4 -1
  40. package/dist/plugin/index.js.map +1 -1
  41. package/dist/plugin/transforms/auto-import.d.ts.map +1 -1
  42. package/dist/plugin/transforms/auto-import.js +2 -0
  43. package/dist/plugin/transforms/auto-import.js.map +1 -1
  44. package/dist/runtime/composables/index.d.ts +3 -0
  45. package/dist/runtime/composables/index.d.ts.map +1 -1
  46. package/dist/runtime/composables/index.js +2 -0
  47. package/dist/runtime/composables/index.js.map +1 -1
  48. package/dist/runtime/composables/use-content-search.d.ts +49 -0
  49. package/dist/runtime/composables/use-content-search.d.ts.map +1 -0
  50. package/dist/runtime/composables/use-content-search.js +101 -0
  51. package/dist/runtime/composables/use-content-search.js.map +1 -0
  52. package/dist/runtime/composables/use-content.d.ts +51 -0
  53. package/dist/runtime/composables/use-content.d.ts.map +1 -0
  54. package/dist/runtime/composables/use-content.js +127 -0
  55. package/dist/runtime/composables/use-content.js.map +1 -0
  56. package/dist/runtime/content/client.d.ts +20 -0
  57. package/dist/runtime/content/client.d.ts.map +1 -0
  58. package/dist/runtime/content/client.js +163 -0
  59. package/dist/runtime/content/client.js.map +1 -0
  60. package/dist/types/config.d.ts +2 -0
  61. package/dist/types/config.d.ts.map +1 -1
  62. package/dist/types/config.js.map +1 -1
  63. package/dist/types/content.d.ts +63 -0
  64. package/dist/types/content.d.ts.map +1 -0
  65. package/dist/types/content.js +2 -0
  66. package/dist/types/content.js.map +1 -0
  67. package/docs/composables.md +115 -10
  68. package/docs/configuration.md +33 -0
  69. package/docs/content.md +453 -0
  70. package/e2e/cypress/e2e/content.cy.ts +291 -0
  71. package/e2e/kitchen-sink/app/pages/content-blog.ts +37 -0
  72. package/e2e/kitchen-sink/app/pages/content-doc.ts +42 -0
  73. package/e2e/kitchen-sink/app/pages/content-fallback.ts +36 -0
  74. package/e2e/kitchen-sink/app/pages/content-index.ts +39 -0
  75. package/e2e/kitchen-sink/app/pages/content-search.ts +35 -0
  76. package/e2e/kitchen-sink/cer.config.ts +1 -0
  77. package/e2e/kitchen-sink/content/blog/2026-04-01-hello.md +26 -0
  78. package/e2e/kitchen-sink/content/blog/2026-04-02-draft.md +10 -0
  79. package/e2e/kitchen-sink/content/blog/index.md +8 -0
  80. package/e2e/kitchen-sink/content/blog/no-frontmatter.md +7 -0
  81. package/e2e/kitchen-sink/content/docs/getting-started.md +46 -0
  82. package/e2e/kitchen-sink/content/index.md +16 -0
  83. package/package.json +10 -7
  84. package/src/__tests__/plugin/build-ssg.test.ts +2 -1
  85. package/src/__tests__/plugin/content/emitter.test.ts +117 -0
  86. package/src/__tests__/plugin/content/loader.test.ts +162 -0
  87. package/src/__tests__/plugin/content/parser.test.ts +381 -0
  88. package/src/__tests__/plugin/content/path-utils.test.ts +53 -0
  89. package/src/__tests__/plugin/content/search.test.ts +119 -0
  90. package/src/__tests__/plugin/dts-generator.test.ts +39 -0
  91. package/src/__tests__/plugin/transforms/auto-import.test.ts +14 -0
  92. package/src/__tests__/runtime/use-content-search.test.ts +139 -0
  93. package/src/__tests__/runtime/use-content.test.ts +226 -0
  94. package/src/cli/commands/preview.ts +2 -0
  95. package/src/index.ts +3 -0
  96. package/src/plugin/build-ssg.ts +12 -0
  97. package/src/plugin/content/emitter.ts +50 -0
  98. package/src/plugin/content/index.ts +236 -0
  99. package/src/plugin/content/parser.ts +259 -0
  100. package/src/plugin/content/path-utils.ts +47 -0
  101. package/src/plugin/content/scanner.ts +26 -0
  102. package/src/plugin/content/search.ts +28 -0
  103. package/src/plugin/dts-generator.ts +10 -1
  104. package/src/plugin/index.ts +6 -1
  105. package/src/plugin/transforms/auto-import.ts +2 -0
  106. package/src/runtime/composables/index.ts +3 -0
  107. package/src/runtime/composables/use-content-search.ts +121 -0
  108. package/src/runtime/composables/use-content.ts +146 -0
  109. package/src/runtime/content/client.ts +168 -0
  110. package/src/types/config.ts +2 -0
  111. package/src/types/content.ts +66 -0
@@ -0,0 +1,117 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { contentPathToFile, emitContentFiles } from '../../../plugin/content/emitter.js'
3
+ import { mkdtempSync, readFileSync, existsSync } from 'node:fs'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'pathe'
6
+ import type { ContentItem } from '../../../types/content.js'
7
+
8
+ // ─── contentPathToFile ────────────────────────────────────────────────────────
9
+
10
+ describe('contentPathToFile', () => {
11
+ it('maps / to index.json', () => {
12
+ expect(contentPathToFile('/')).toBe('index.json')
13
+ })
14
+
15
+ it('maps /about to about.json', () => {
16
+ expect(contentPathToFile('/about')).toBe('about.json')
17
+ })
18
+
19
+ it('maps /blog/hello to blog/hello.json', () => {
20
+ expect(contentPathToFile('/blog/hello')).toBe('blog/hello.json')
21
+ })
22
+
23
+ it('maps /blog to blog.json', () => {
24
+ expect(contentPathToFile('/blog')).toBe('blog.json')
25
+ })
26
+
27
+ it('maps /docs/getting-started to docs/getting-started.json', () => {
28
+ expect(contentPathToFile('/docs/getting-started')).toBe('docs/getting-started.json')
29
+ })
30
+ })
31
+
32
+ // ─── emitContentFiles ─────────────────────────────────────────────────────────
33
+
34
+ describe('emitContentFiles', () => {
35
+ const items: ContentItem[] = [
36
+ {
37
+ _path: '/',
38
+ _file: 'index.md',
39
+ _type: 'markdown',
40
+ title: 'Home',
41
+ description: 'Welcome',
42
+ body: '<h1>Home</h1>',
43
+ toc: [],
44
+ },
45
+ {
46
+ _path: '/blog/hello',
47
+ _file: 'blog/hello.md',
48
+ _type: 'markdown',
49
+ title: 'Hello',
50
+ description: 'First post',
51
+ date: '2026-04-03',
52
+ draft: false,
53
+ body: '<h1>Hello</h1>',
54
+ toc: [{ depth: 1, id: 'hello', text: 'Hello' }],
55
+ excerpt: '<p>Intro</p>',
56
+ },
57
+ ]
58
+
59
+ it('writes manifest.json with lean ContentMeta (no body/toc/excerpt/_file)', () => {
60
+ const outDir = mkdtempSync(join(tmpdir(), 'cer-emitter-'))
61
+ emitContentFiles(items, outDir, '{}')
62
+ const manifest = JSON.parse(readFileSync(join(outDir, '_content/manifest.json'), 'utf-8'))
63
+ expect(Array.isArray(manifest)).toBe(true)
64
+ expect(manifest).toHaveLength(2)
65
+ const root = manifest.find((m: { _path: string }) => m._path === '/')
66
+ expect(root.title).toBe('Home')
67
+ expect('body' in root).toBe(false)
68
+ expect('toc' in root).toBe(false)
69
+ expect('excerpt' in root).toBe(false)
70
+ expect('_file' in root).toBe(false)
71
+ })
72
+
73
+ it('writes search-index.json with provided content', () => {
74
+ const outDir = mkdtempSync(join(tmpdir(), 'cer-emitter-'))
75
+ emitContentFiles(items, outDir, '{"test":true}')
76
+ const idx = JSON.parse(readFileSync(join(outDir, '_content/search-index.json'), 'utf-8'))
77
+ expect(idx).toEqual({ test: true })
78
+ })
79
+
80
+ it('writes root document to _content/index.json', () => {
81
+ const outDir = mkdtempSync(join(tmpdir(), 'cer-emitter-'))
82
+ emitContentFiles(items, outDir, '{}')
83
+ expect(existsSync(join(outDir, '_content/index.json'))).toBe(true)
84
+ const doc = JSON.parse(readFileSync(join(outDir, '_content/index.json'), 'utf-8'))
85
+ expect(doc._path).toBe('/')
86
+ expect(doc.body).toBe('<h1>Home</h1>')
87
+ })
88
+
89
+ it('writes nested document to correct subdirectory', () => {
90
+ const outDir = mkdtempSync(join(tmpdir(), 'cer-emitter-'))
91
+ emitContentFiles(items, outDir, '{}')
92
+ const docPath = join(outDir, '_content/blog/hello.json')
93
+ expect(existsSync(docPath)).toBe(true)
94
+ const doc = JSON.parse(readFileSync(docPath, 'utf-8'))
95
+ expect(doc._path).toBe('/blog/hello')
96
+ expect(doc.toc).toEqual([{ depth: 1, id: 'hello', text: 'Hello' }])
97
+ expect(doc.excerpt).toBe('<p>Intro</p>')
98
+ })
99
+
100
+ it('coexisting blog.json and blog/ dir is allowed', () => {
101
+ const items2: ContentItem[] = [
102
+ ...items,
103
+ {
104
+ _path: '/blog',
105
+ _file: 'blog/index.md',
106
+ _type: 'markdown',
107
+ title: 'Blog',
108
+ body: '<p>Blog</p>',
109
+ toc: [],
110
+ },
111
+ ]
112
+ const outDir = mkdtempSync(join(tmpdir(), 'cer-emitter-'))
113
+ emitContentFiles(items2, outDir, '{}')
114
+ expect(existsSync(join(outDir, '_content/blog.json'))).toBe(true)
115
+ expect(existsSync(join(outDir, '_content/blog/hello.json'))).toBe(true)
116
+ })
117
+ })
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Tests for cerContent() plugin factory helpers:
3
+ * - CONTENT_STORE_KEY constant
4
+ * - resolveContentDir()
5
+ * - loadContentStore()
6
+ *
7
+ * The Vite plugin hooks (buildStart, configureServer, closeBundle) are exercised
8
+ * by the e2e suite across all three build modes.
9
+ */
10
+ import { describe, it, expect, beforeAll, afterAll } from 'vitest'
11
+ import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'
12
+ import { tmpdir } from 'node:os'
13
+ import { join } from 'pathe'
14
+ import {
15
+ CONTENT_STORE_KEY,
16
+ resolveContentDir,
17
+ loadContentStore,
18
+ } from '../../../plugin/content/index.js'
19
+
20
+ let tmpDir: string
21
+ let contentDir: string
22
+
23
+ beforeAll(() => {
24
+ tmpDir = mkdtempSync(join(tmpdir(), 'cer-content-loader-'))
25
+ contentDir = join(tmpDir, 'content')
26
+ mkdirSync(contentDir, { recursive: true })
27
+ mkdirSync(join(contentDir, 'blog'), { recursive: true })
28
+ mkdirSync(join(contentDir, 'data'), { recursive: true })
29
+
30
+ writeFileSync(join(contentDir, 'index.md'), '---\ntitle: Home\ndescription: Welcome\n---\n\n# Home\n')
31
+ writeFileSync(join(contentDir, 'about.md'), '---\ntitle: About\ndate: 2026-04-01\n---\n\n# About\n')
32
+ writeFileSync(
33
+ join(contentDir, 'blog', '2026-04-03-hello.md'),
34
+ '---\ntitle: Hello World\ndate: 2026-04-03\ndraft: false\n---\n\nHello post!',
35
+ )
36
+ writeFileSync(
37
+ join(contentDir, 'blog', 'secret.md'),
38
+ '---\ntitle: Secret Draft\ndraft: true\n---\n\nSecret!',
39
+ )
40
+ writeFileSync(join(contentDir, 'data', 'products.json'), JSON.stringify([{ id: 1 }]))
41
+ })
42
+
43
+ afterAll(() => {
44
+ rmSync(tmpDir, { recursive: true, force: true })
45
+ })
46
+
47
+ // ─── CONTENT_STORE_KEY ────────────────────────────────────────────────────────
48
+
49
+ describe('CONTENT_STORE_KEY', () => {
50
+ it('equals __CER_CONTENT_STORE__', () => {
51
+ expect(CONTENT_STORE_KEY).toBe('__CER_CONTENT_STORE__')
52
+ })
53
+ })
54
+
55
+ // ─── resolveContentDir ────────────────────────────────────────────────────────
56
+
57
+ describe('resolveContentDir', () => {
58
+ it('uses "content" when no config is supplied', () => {
59
+ expect(resolveContentDir('/root')).toBe('/root/content')
60
+ })
61
+
62
+ it('uses "content" when config.dir is undefined', () => {
63
+ expect(resolveContentDir('/root', {})).toBe('/root/content')
64
+ })
65
+
66
+ it('uses the configured dir when provided', () => {
67
+ expect(resolveContentDir('/root', { dir: 'docs' })).toBe('/root/docs')
68
+ })
69
+
70
+ it('resolves relative to the project root, not the app source dir', () => {
71
+ // content/ is a sibling of app/, not a child of it
72
+ expect(resolveContentDir('/workspace/my-project', { dir: 'posts' })).toBe(
73
+ '/workspace/my-project/posts',
74
+ )
75
+ })
76
+ })
77
+
78
+ // ─── loadContentStore ─────────────────────────────────────────────────────────
79
+
80
+ describe('loadContentStore — nonexistent dir', () => {
81
+ it('returns empty array when contentDir does not exist', async () => {
82
+ const items = await loadContentStore('/path/does/not/exist', false, false)
83
+ expect(items).toEqual([])
84
+ })
85
+ })
86
+
87
+ describe('loadContentStore — dev mode (isProduction=false)', () => {
88
+ it('loads all files including drafts', async () => {
89
+ const items = await loadContentStore(contentDir, false, false)
90
+ const paths = items.map((i) => i._path).sort()
91
+ // Root, about, blog/hello, blog/secret, data/products
92
+ expect(paths).toContain('/')
93
+ expect(paths).toContain('/about')
94
+ expect(paths).toContain('/blog/hello')
95
+ expect(paths).toContain('/blog/secret')
96
+ expect(paths).toContain('/data/products')
97
+ })
98
+
99
+ it('includes draft items', async () => {
100
+ const items = await loadContentStore(contentDir, false, false)
101
+ const secret = items.find((i) => i._path === '/blog/secret')
102
+ expect(secret).toBeDefined()
103
+ expect(secret?.draft).toBe(true)
104
+ })
105
+
106
+ it('strips date prefix from slug', async () => {
107
+ const items = await loadContentStore(contentDir, false, false)
108
+ expect(items.find((i) => i._path === '/blog/hello')).toBeDefined()
109
+ expect(items.find((i) => i._path === '/blog/2026-04-03-hello')).toBeUndefined()
110
+ })
111
+
112
+ it('each item has required ContentItem fields', async () => {
113
+ const items = await loadContentStore(contentDir, false, false)
114
+ for (const item of items) {
115
+ expect(typeof item._path).toBe('string')
116
+ expect(typeof item._file).toBe('string')
117
+ expect(item._type === 'markdown' || item._type === 'json').toBe(true)
118
+ expect(typeof item.body).toBe('string')
119
+ expect(Array.isArray(item.toc)).toBe(true)
120
+ }
121
+ })
122
+
123
+ it('normalises date fields to strings (not Date objects)', async () => {
124
+ const items = await loadContentStore(contentDir, false, false)
125
+ const about = items.find((i) => i._path === '/about')
126
+ expect(about).toBeDefined()
127
+ expect(typeof about?.date).toBe('string')
128
+ expect(about?.date as string).toMatch(/^\d{4}-\d{2}-\d{2}$/)
129
+ })
130
+ })
131
+
132
+ describe('loadContentStore — production mode (isProduction=true, isDraft=false)', () => {
133
+ it('excludes draft items', async () => {
134
+ const items = await loadContentStore(contentDir, false, true)
135
+ const secret = items.find((i) => i._path === '/blog/secret')
136
+ expect(secret).toBeUndefined()
137
+ })
138
+
139
+ it('includes non-draft items', async () => {
140
+ const items = await loadContentStore(contentDir, false, true)
141
+ expect(items.find((i) => i._path === '/blog/hello')).toBeDefined()
142
+ expect(items.find((i) => i._path === '/about')).toBeDefined()
143
+ })
144
+ })
145
+
146
+ describe('loadContentStore — production mode with drafts enabled (isDraft=true)', () => {
147
+ it('includes draft items when isDraft=true in production', async () => {
148
+ const items = await loadContentStore(contentDir, true, true)
149
+ const secret = items.find((i) => i._path === '/blog/secret')
150
+ expect(secret).toBeDefined()
151
+ })
152
+ })
153
+
154
+ describe('loadContentStore — JSON files', () => {
155
+ it('includes JSON files with _type json', async () => {
156
+ const items = await loadContentStore(contentDir, false, false)
157
+ const products = items.find((i) => i._path === '/data/products')
158
+ expect(products).toBeDefined()
159
+ expect(products?._type).toBe('json')
160
+ expect(JSON.parse(products?.body ?? '[]')).toEqual([{ id: 1 }])
161
+ })
162
+ })
@@ -0,0 +1,381 @@
1
+ import { describe, it, expect, beforeAll, afterAll } from 'vitest'
2
+ import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from 'node:fs'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'pathe'
5
+ import { parseContentFile } from '../../../plugin/content/parser.js'
6
+ import type { ContentFile } from '../../../plugin/content/scanner.js'
7
+
8
+ // ─── Helpers ──────────────────────────────────────────────────────────────────
9
+
10
+ let tmpDir: string
11
+ let contentDir: string
12
+
13
+ beforeAll(() => {
14
+ tmpDir = mkdtempSync(join(tmpdir(), 'cer-content-parser-'))
15
+ contentDir = join(tmpDir, 'content')
16
+ mkdirSync(contentDir, { recursive: true })
17
+ mkdirSync(join(contentDir, 'blog'), { recursive: true })
18
+ mkdirSync(join(contentDir, 'data'), { recursive: true })
19
+
20
+ writeFileSync(join(contentDir, 'index.md'), `---
21
+ title: Home
22
+ description: Welcome home
23
+ ---
24
+
25
+ # Home
26
+
27
+ Body content.
28
+ `)
29
+
30
+ writeFileSync(join(contentDir, 'about.md'), `---
31
+ title: About
32
+ date: 2026-04-01
33
+ draft: false
34
+ tags: [web]
35
+ ---
36
+
37
+ # About
38
+
39
+ ## Section One
40
+
41
+ Content here.
42
+ `)
43
+
44
+ writeFileSync(join(contentDir, 'blog', 'hello.md'), `---
45
+ title: Hello World
46
+ description: My first post
47
+ date: 2026-04-03
48
+ ---
49
+
50
+ Intro paragraph.
51
+
52
+ <!-- more -->
53
+
54
+ Rest of the body.
55
+ `)
56
+
57
+ writeFileSync(join(contentDir, 'blog', 'no-excerpt.md'), `---
58
+ title: No Excerpt
59
+ ---
60
+
61
+ Just content, no more marker.
62
+ `)
63
+
64
+ writeFileSync(join(contentDir, 'data', 'products.json'), JSON.stringify([{ id: 1, name: 'Widget' }]))
65
+
66
+ // ── Fallback fixtures ────────────────────────────────────────────────────
67
+ // No frontmatter at all — title and description derived from body
68
+ writeFileSync(join(contentDir, 'no-frontmatter.md'), `# Derived Title
69
+
70
+ First paragraph for description.
71
+
72
+ Second paragraph.
73
+ `)
74
+
75
+ // Frontmatter title only — description derived from body
76
+ writeFileSync(join(contentDir, 'title-only.md'), `---
77
+ title: Explicit Title
78
+ ---
79
+
80
+ Description will come from this paragraph.
81
+ `)
82
+
83
+ // Frontmatter description only — title derived from h1
84
+ writeFileSync(join(contentDir, 'desc-only.md'), `---
85
+ description: Explicit description
86
+ ---
87
+
88
+ # Derived H1 Title
89
+
90
+ Some paragraph.
91
+ `)
92
+
93
+ // Both set in frontmatter — body values must NOT overwrite them
94
+ writeFileSync(join(contentDir, 'both-frontmatter.md'), `---
95
+ title: FM Title
96
+ description: FM Description
97
+ ---
98
+
99
+ # Different H1
100
+
101
+ Different paragraph.
102
+ `)
103
+
104
+ // h1 with inline formatting — plain text only
105
+ writeFileSync(join(contentDir, 'formatted-h1.md'), `# Hello **World**
106
+
107
+ Some intro.
108
+ `)
109
+
110
+ // Long paragraph — description truncated to 160 chars + ellipsis
111
+ writeFileSync(join(contentDir, 'long-para.md'), `# Long
112
+
113
+ ${'A'.repeat(200)}
114
+ `)
115
+
116
+ // No h1, only h2 — title fallback must remain undefined
117
+ writeFileSync(join(contentDir, 'no-h1.md'), `## Section Only
118
+
119
+ A paragraph here.
120
+ `)
121
+ })
122
+
123
+ function makeFile(filePath: string, ext: 'md' | 'json'): ContentFile {
124
+ return { filePath, ext }
125
+ }
126
+
127
+ // ─── Markdown parsing ─────────────────────────────────────────────────────────
128
+
129
+ describe('parseContentFile — Markdown', () => {
130
+ it('returns correct _path for index.md', () => {
131
+ const item = parseContentFile(makeFile(join(contentDir, 'index.md'), 'md'), contentDir)
132
+ expect(item._path).toBe('/')
133
+ })
134
+
135
+ it('sets _type to markdown', () => {
136
+ const item = parseContentFile(makeFile(join(contentDir, 'index.md'), 'md'), contentDir)
137
+ expect(item._type).toBe('markdown')
138
+ })
139
+
140
+ it('sets _file relative to contentDir', () => {
141
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
142
+ expect(item._file).toBe('about.md')
143
+ })
144
+
145
+ it('extracts frontmatter fields', () => {
146
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
147
+ expect(item.title).toBe('About')
148
+ // gray-matter parses YAML date strings as Date objects; verify date truthy and correct type
149
+ expect(item.date).toBeTruthy()
150
+ expect(item.draft).toBe(false)
151
+ expect(item.tags).toEqual(['web'])
152
+ })
153
+
154
+ it('renders Markdown body to HTML', () => {
155
+ const item = parseContentFile(makeFile(join(contentDir, 'index.md'), 'md'), contentDir)
156
+ expect(item.body).toContain('<h1')
157
+ expect(item.body).toContain('Home')
158
+ })
159
+
160
+ it('extracts headings into toc', () => {
161
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
162
+ expect(Array.isArray(item.toc)).toBe(true)
163
+ const h1 = item.toc.find((h) => h.depth === 1)
164
+ const h2 = item.toc.find((h) => h.depth === 2)
165
+ expect(h1).toBeDefined()
166
+ expect(h2).toBeDefined()
167
+ expect(h2?.text).toBe('Section One')
168
+ expect(h2?.id).toBe('section-one')
169
+ })
170
+
171
+ it('adds id attributes to headings in body HTML', () => {
172
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
173
+ expect(item.body).toContain('id="section-one"')
174
+ })
175
+
176
+ it('toc id matches heading id attribute in body', () => {
177
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
178
+ for (const h of item.toc) {
179
+ expect(item.body).toContain(`id="${h.id}"`)
180
+ }
181
+ })
182
+
183
+ it('sets excerpt when <!-- more --> marker is present', () => {
184
+ const item = parseContentFile(
185
+ makeFile(join(contentDir, 'blog', 'hello.md'), 'md'),
186
+ contentDir,
187
+ )
188
+ expect(item.excerpt).toBeDefined()
189
+ expect(item.excerpt).toContain('Intro paragraph')
190
+ expect(item.excerpt).not.toContain('Rest of the body')
191
+ })
192
+
193
+ it('excerpt is absent when no <!-- more --> marker', () => {
194
+ const item = parseContentFile(
195
+ makeFile(join(contentDir, 'blog', 'no-excerpt.md'), 'md'),
196
+ contentDir,
197
+ )
198
+ expect(item.excerpt).toBeUndefined()
199
+ })
200
+
201
+ it('body contains full content including text after <!-- more -->', () => {
202
+ const item = parseContentFile(
203
+ makeFile(join(contentDir, 'blog', 'hello.md'), 'md'),
204
+ contentDir,
205
+ )
206
+ expect(item.body).toContain('Intro paragraph')
207
+ expect(item.body).toContain('Rest of the body')
208
+ // The <!-- more --> marker itself must not appear in the rendered body HTML
209
+ expect(item.body).not.toContain('<!-- more -->')
210
+ })
211
+ })
212
+
213
+ // ─── JSON parsing ─────────────────────────────────────────────────────────────
214
+
215
+ describe('parseContentFile — JSON', () => {
216
+ it('sets _type to json', () => {
217
+ const item = parseContentFile(
218
+ makeFile(join(contentDir, 'data', 'products.json'), 'json'),
219
+ contentDir,
220
+ )
221
+ expect(item._type).toBe('json')
222
+ })
223
+
224
+ it('sets body to raw JSON string', () => {
225
+ const item = parseContentFile(
226
+ makeFile(join(contentDir, 'data', 'products.json'), 'json'),
227
+ contentDir,
228
+ )
229
+ expect(JSON.parse(item.body)).toEqual([{ id: 1, name: 'Widget' }])
230
+ })
231
+
232
+ it('sets toc to empty array', () => {
233
+ const item = parseContentFile(
234
+ makeFile(join(contentDir, 'data', 'products.json'), 'json'),
235
+ contentDir,
236
+ )
237
+ expect(item.toc).toEqual([])
238
+ })
239
+
240
+ it('sets correct _path for json file', () => {
241
+ const item = parseContentFile(
242
+ makeFile(join(contentDir, 'data', 'products.json'), 'json'),
243
+ contentDir,
244
+ )
245
+ expect(item._path).toBe('/data/products')
246
+ })
247
+
248
+ it('throws a descriptive error for invalid JSON', () => {
249
+ const badFile = join(contentDir, 'data', 'bad.json')
250
+ writeFileSync(badFile, '{not valid json}')
251
+ expect(() =>
252
+ parseContentFile(makeFile(badFile, 'json'), contentDir),
253
+ ).toThrow(/Invalid JSON/)
254
+ })
255
+ })
256
+
257
+ // ─── toContentMeta ────────────────────────────────────────────────────────────
258
+
259
+ describe('toContentMeta', () => {
260
+ it('strips _file, body, toc, excerpt from ContentItem', async () => {
261
+ const { toContentMeta } = await import('../../../plugin/content/parser.js')
262
+ const item = parseContentFile(
263
+ makeFile(join(contentDir, 'blog', 'hello.md'), 'md'),
264
+ contentDir,
265
+ )
266
+ const meta = toContentMeta(item)
267
+ expect('_file' in meta).toBe(false)
268
+ expect('body' in meta).toBe(false)
269
+ expect('toc' in meta).toBe(false)
270
+ expect('excerpt' in meta).toBe(false)
271
+ expect(meta._path).toBe('/blog/hello')
272
+ expect(meta.title).toBe('Hello World')
273
+ })
274
+ })
275
+
276
+ // ─── Date normalisation ───────────────────────────────────────────────────────
277
+
278
+ describe('parseContentFile — date normalisation', () => {
279
+ it('converts a gray-matter Date object to a YYYY-MM-DD string', () => {
280
+ // gray-matter parses `date: 2026-04-01` as a JS Date object.
281
+ // The parser must normalise it to a string so the in-memory server store and
282
+ // the client (after JSON round-trip) are consistent.
283
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
284
+ expect(typeof item.date).toBe('string')
285
+ expect(item.date as string).toMatch(/^\d{4}-\d{2}-\d{2}$/)
286
+ })
287
+
288
+ it('normalised date string is comparable with other ISO date strings', () => {
289
+ const item = parseContentFile(makeFile(join(contentDir, 'about.md'), 'md'), contentDir)
290
+ const date = item.date as string
291
+ // Verify that where-predicate comparisons work correctly post-normalisation
292
+ expect(date >= '2026-01-01').toBe(true)
293
+ expect(date < '2027-01-01').toBe(true)
294
+ })
295
+ })
296
+
297
+ // ─── Fallback title / description ────────────────────────────────────────────
298
+
299
+ describe('parseContentFile — fallback title from h1', () => {
300
+ it('derives title from h1 when frontmatter title is absent', () => {
301
+ const item = parseContentFile(makeFile(join(contentDir, 'no-frontmatter.md'), 'md'), contentDir)
302
+ expect(item.title).toBe('Derived Title')
303
+ })
304
+
305
+ it('frontmatter title is not overwritten when present', () => {
306
+ const item = parseContentFile(makeFile(join(contentDir, 'title-only.md'), 'md'), contentDir)
307
+ expect(item.title).toBe('Explicit Title')
308
+ })
309
+
310
+ it('derives title from h1 when only description is in frontmatter', () => {
311
+ const item = parseContentFile(makeFile(join(contentDir, 'desc-only.md'), 'md'), contentDir)
312
+ expect(item.title).toBe('Derived H1 Title')
313
+ })
314
+
315
+ it('frontmatter title wins over h1 when both set', () => {
316
+ const item = parseContentFile(makeFile(join(contentDir, 'both-frontmatter.md'), 'md'), contentDir)
317
+ expect(item.title).toBe('FM Title')
318
+ })
319
+
320
+ it('strips inline formatting — title is plain text', () => {
321
+ const item = parseContentFile(makeFile(join(contentDir, 'formatted-h1.md'), 'md'), contentDir)
322
+ expect(item.title).toBe('Hello World')
323
+ })
324
+
325
+ it('does not derive title from h2 — must be h1 only', () => {
326
+ const item = parseContentFile(makeFile(join(contentDir, 'no-h1.md'), 'md'), contentDir)
327
+ expect(item.title).toBeUndefined()
328
+ })
329
+ })
330
+
331
+ describe('parseContentFile — fallback description from first paragraph', () => {
332
+ it('derives description from first paragraph when frontmatter description is absent', () => {
333
+ const item = parseContentFile(makeFile(join(contentDir, 'no-frontmatter.md'), 'md'), contentDir)
334
+ expect(item.description).toBe('First paragraph for description.')
335
+ })
336
+
337
+ it('frontmatter description is not overwritten when present', () => {
338
+ const item = parseContentFile(makeFile(join(contentDir, 'desc-only.md'), 'md'), contentDir)
339
+ expect(item.description).toBe('Explicit description')
340
+ })
341
+
342
+ it('derives description from first paragraph when only title is in frontmatter', () => {
343
+ const item = parseContentFile(makeFile(join(contentDir, 'title-only.md'), 'md'), contentDir)
344
+ expect(item.description).toBe('Description will come from this paragraph.')
345
+ })
346
+
347
+ it('frontmatter description wins when both set', () => {
348
+ const item = parseContentFile(makeFile(join(contentDir, 'both-frontmatter.md'), 'md'), contentDir)
349
+ expect(item.description).toBe('FM Description')
350
+ })
351
+
352
+ it('truncates long paragraphs to 160 chars with ellipsis', () => {
353
+ const item = parseContentFile(makeFile(join(contentDir, 'long-para.md'), 'md'), contentDir)
354
+ expect(typeof item.description).toBe('string')
355
+ expect((item.description as string).length).toBeLessThanOrEqual(164) // 160 + '…' (3 bytes)
356
+ expect(item.description as string).toMatch(/…$/)
357
+ })
358
+
359
+ it('derived description is on ContentMeta (present in manifest)', async () => {
360
+ const { toContentMeta } = await import('../../../plugin/content/parser.js')
361
+ const item = parseContentFile(makeFile(join(contentDir, 'no-frontmatter.md'), 'md'), contentDir)
362
+ const meta = toContentMeta(item)
363
+ expect(meta.description).toBe('First paragraph for description.')
364
+ })
365
+ })
366
+
367
+ describe('parseContentFile — JSON fallback', () => {
368
+ it('does not apply title/description fallbacks to JSON files', () => {
369
+ // JSON files have no markdown body to extract from
370
+ const item = parseContentFile(
371
+ makeFile(join(contentDir, 'data', 'products.json'), 'json'),
372
+ contentDir,
373
+ )
374
+ expect(item.title).toBeUndefined()
375
+ expect(item.description).toBeUndefined()
376
+ })
377
+ })
378
+
379
+ afterAll(() => {
380
+ rmSync(tmpDir, { recursive: true, force: true })
381
+ })