fix category title, add post_title to docs manifest

This commit is contained in:
Ron Rennick 2023-11-27 14:52:48 -04:00
parent 5a66522601
commit 322f433455
2 changed files with 13 additions and 12 deletions

View File

@ -30,12 +30,16 @@ export const generatePostFrontMatter = (
// See https://github.com/jonschlinkert/gray-matter/issues/62#issuecomment-577628177 for more details.
yaml: ( s ) => yaml.load( s, { schema: yaml.JSON_SCHEMA } ),
},
} ).data;
} );
const content = frontMatter.content.split( '\n' );
const headings = content.filter( ( line ) => '# ' == line.substring( 0, 2 ) );
const title = ( headings[0]?.substring( 2 ) ?? '' ).trim();
return Object.keys( frontMatter )
frontMatter.data.post_title = frontMatter.data.post_title ?? title;
return Object.keys( frontMatter.data )
.filter( ( key ) => allowList.includes( key ) )
.reduce( ( obj, key ) => {
obj[ key ] = frontMatter[ key ];
obj[ key ] = frontMatter.data[ key ];
return obj;
}, {} );
};

View File

@ -42,15 +42,12 @@ async function processDirectory(
// Process README.md (if exists) for the category definition.
const readmePath = path.join( subDirectory, 'README.md' );
if ( checkReadme && fs.existsSync( readmePath ) ) {
const readmeContent = fs.readFileSync( readmePath, 'utf-8' );
const frontMatter = generatePostFrontMatter( readmeContent );
Object.assign( category, frontMatter );
} else if ( checkReadme ) {
// derive the category title from the directory name, capitalize first letter
const categoryTitle = path.basename( subDirectory );
category.category_title =
categoryTitle.charAt( 0 ).toUpperCase() + categoryTitle.slice( 1 );
if ( checkReadme ) {
// derive the category title from the directory name, capitalize first letter of each word.
const categoryFolder = path.basename( subDirectory ).split( '-' );
const categoryTitle = categoryFolder
.map( ( slugPart ) => slugPart.charAt( 0 ).toUpperCase() + slugPart.slice( 1 ) );
category.category_title = categoryTitle.join( ' ' );
}
const markdownFiles = glob.sync( path.join( subDirectory, '*.md' ) );