The filename customization API allows you to customize the filename of a document based on its content. This is useful when you do not want your editors to have to worry about the filename of a document.
A couple things to keep in mind when customizing the filename:
/
it will be treated as an absolute path relative to the collection root/foo/bar/blog-post
will be saved as <MyCollectionPath>/post/blog-post.md
/
it will be treated as a relative to your current folderbar/blog-post
will be saved as <MyCollectionPath>/<CurrentDirectory>/bar/blog-post.md
Property | Description |
ui.filename.readonly | Disable the editor from editing the filename |
ui.filename.slugify | A function that takes in the values of the form and returns the filename |
To use the filename customization API, you need to pass a slugify
function that allows you to customize the filename of a document based on its content.
export default defineConfig({//...schema: {collections: [{label: 'Blog Posts',name: 'post',path: 'content/post',format: 'md',ui: {filename: {// if disabled, the editor can not edit the filenamereadonly: true,// Example of using a custom slugify functionslugify: (values) => {// Values is an object containing all the values of the form. In this case it is {title?: string, topic?: string}return `${values?.topic || 'no-topic'}-${values?.title?.toLowerCase().replace(/ /g, '-')}`},},},fields: [{type: 'string',label: 'Title',name: 'title',},{type: 'string',label: 'Topic',name: 'topic',options: ['programming', 'blacksmithing'],},],},],},})
If no slugify function is provided and there is a field with isTItle: true
. A default slugify function will be used that strips out every non-alphanumeric character and replaces spaces with dashes.
export default defineConfig({//...schema: {collections: [{label: 'Blog Posts',name: 'post',path: 'content/post',format: 'md',fields: [{type: 'string',label: 'Title',name: 'title',// If no slugify function is provided, then by default the "title" field will be used to generate the filenameisTitle: true,required: true,},{type: 'string',label: 'Topic',name: 'topic',options: ['programming', 'blacksmithing'],},],},],},})
© TinaCMS 2019–2024