-
Notifications
You must be signed in to change notification settings - Fork 19
wip: refactor to handle edge case for collection sources #79
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { generateContentFromDocument, generateDocumentFromContent, removeReservedKeysFromDocument } from './utils/content' | ||
| export { generateIdFromFsPath, parseSourceBase } from './utils/collection' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { CollectionSource, CollectionInfo } from '@nuxt/content' | ||
| import { join } from 'pathe' | ||
|
|
||
| export function parseSourceBase(source: CollectionSource) { | ||
| const [fixPart, ...rest] = source.include.includes('*') ? source.include.split('*') : ['', source.include] | ||
| return { | ||
| fixed: fixPart || '', | ||
| dynamic: '*' + rest.join('*'), | ||
| } | ||
| } | ||
|
|
||
| export function generateIdFromFsPath(path: string, collectionInfo: CollectionInfo) { | ||
| const { fixed } = parseSourceBase(collectionInfo.source[0]!) | ||
|
|
||
| const pathWithoutFixed = path.substring(fixed.length) | ||
|
|
||
| return join(collectionInfo.name, collectionInfo.source[0]?.prefix || '', pathWithoutFixed) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import type { CollectionInfo } from '@nuxt/content' | ||
|
|
||
| export const collections: Record<string, CollectionInfo> = { | ||
| landing: { | ||
| name: 'landing', | ||
| pascalName: 'Landing', | ||
| tableName: '_content_landing', | ||
| source: [ | ||
| { | ||
| _resolved: true, | ||
| prefix: '/', | ||
| cwd: '/Users/larbish/Documents/nuxt/modules/studio/playground/content', | ||
| include: 'index.md', | ||
| }, | ||
| ], | ||
| type: 'page', | ||
| fields: { | ||
| id: 'string', | ||
| title: 'string', | ||
| body: 'json', | ||
| description: 'string', | ||
| extension: 'string', | ||
| meta: 'json', | ||
| navigation: 'json', | ||
| path: 'string', | ||
| seo: 'json', | ||
| stem: 'string', | ||
| }, | ||
| schema: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $ref: '#/definitions/docs', | ||
| definitions: {}, | ||
| }, | ||
| tableDefinition: '', | ||
| }, | ||
| docs: { | ||
| name: 'docs', | ||
| pascalName: 'Docs', | ||
| tableName: '_content_docs', | ||
| source: [ | ||
| { | ||
| _resolved: true, | ||
| prefix: '/', | ||
| cwd: '/Users/larbish/Documents/nuxt/modules/studio/playground/content', | ||
| include: '**', | ||
| exclude: [ | ||
| 'index.md', | ||
| ], | ||
| }, | ||
| ], | ||
| type: 'page', | ||
| fields: { | ||
| id: 'string', | ||
| title: 'string', | ||
| body: 'json', | ||
| description: 'string', | ||
| extension: 'string', | ||
| links: 'json', | ||
| meta: 'json', | ||
| navigation: 'json', | ||
| path: 'string', | ||
| seo: 'json', | ||
| stem: 'string', | ||
| }, | ||
| schema: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $ref: '#/definitions/__SCHEMA__', | ||
| definitions: {}, | ||
| }, | ||
| tableDefinition: 'CREATE TABLE IF NOT EXISTS', | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, it, expect } from 'vitest' | ||
| import { generateIdFromFsPath } from '../../../src/utils/collection' | ||
| import type { CollectionInfo } from '@nuxt/content' | ||
| import { collections } from '../../mocks/collection' | ||
|
|
||
| describe('generateIdFromFsPath', () => { | ||
| it('should generate id for single file with no prefix', () => { | ||
| const path = 'index.md' | ||
| const result = generateIdFromFsPath(path, collections.landing!) | ||
| expect(result).toBe('landing/index.md') | ||
| }) | ||
|
|
||
| it('should generate id for nested file with global pattern', () => { | ||
| const path = '1.getting-started/2.introduction.md' | ||
| const result = generateIdFromFsPath(path, collections.docs!) | ||
| expect(result).toBe('docs/1.getting-started/2.introduction.md') | ||
| }) | ||
|
|
||
| it('should handle deeply nested paths', () => { | ||
| const path = '2.essentials/1.nested/3.components.md' | ||
| const result = generateIdFromFsPath(path, collections.docs!) | ||
| expect(result).toBe('docs/2.essentials/1.nested/3.components.md') | ||
| }) | ||
|
|
||
| it('should handle collection with custom prefix', () => { | ||
| const customCollection: CollectionInfo = { | ||
| name: 'docs_en', | ||
| pascalName: 'DocsEn', | ||
| tableName: '_content_docs_en', | ||
| source: [ | ||
| { | ||
| _resolved: true, | ||
| prefix: '/en', | ||
| cwd: '', | ||
| include: 'en/**/*', | ||
| exclude: ['en/index.md'], | ||
| }, | ||
| ], | ||
| type: 'page', | ||
| fields: {}, | ||
| schema: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $ref: '#/definitions/docs_en', | ||
| definitions: {}, | ||
| }, | ||
| tableDefinition: '', | ||
| } | ||
|
|
||
| const path = 'en/1.getting-started/2.introduction.md' | ||
| const result = generateIdFromFsPath(path, customCollection) | ||
| expect(result).toBe('docs_en/en/1.getting-started/2.introduction.md') | ||
| }) | ||
|
|
||
| it('should handle empty prefix correctly', () => { | ||
| const customCollection: CollectionInfo = { | ||
| name: 'pages', | ||
| pascalName: 'Pages', | ||
| tableName: '_content_pages', | ||
| source: [ | ||
| { | ||
| _resolved: true, | ||
| prefix: '', | ||
| cwd: '', | ||
| include: 'content/**/*.md', | ||
| }, | ||
| ], | ||
| type: 'page', | ||
| fields: {}, | ||
| schema: { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $ref: '#/definitions/pages', | ||
| definitions: {}, | ||
| }, | ||
| tableDefinition: '', | ||
| } | ||
|
|
||
| const path = 'content/about.md' | ||
| const result = generateIdFromFsPath(path, customCollection) | ||
| expect(result).toBe('pages/about.md') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function signature mismatch:
generateIdFromFsPathnow expects aCollectionInfoobject, but all call sites throughout the codebase are passing collection name strings instead, which will cause TypeScript compilation errors.View Details
📝 Patch Details
Analysis
Function signature mismatch causes TypeScript compilation errors in generateIdFromFsPath
What fails:
generateIdFromFsPathfunction expectsCollectionInfoobject but call sites pass collection name strings, causing TypeScript errors inuseContext.ts(8 calls) anduseTree.ts(1 call)How to reproduce:
pnpm exec vue-tsc -p src/app/tsconfig.app.json --noEmitResult: TypeScript compilation errors:
Expected: TypeScript compilation should pass without errors as the function should accept collection name strings for backward compatibility with app-side code that uses
TreeRootId.Media,TreeRootId.Content, anditem.collections[0]string values.