WooCommerce Docs: Fix a bug where passing full file urls made ids unstable. (#39357)

Fix a bug where passing full file urls made ids unstable.
This commit is contained in:
Sam Seay 2023-07-26 12:54:45 +08:00 committed by Kyle Nel
parent ce79211c93
commit f479625708
No known key found for this signature in database
2 changed files with 60 additions and 5 deletions

View File

@ -6,7 +6,10 @@ import path from 'path';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { generateManifestFromDirectory } from '../generate-manifest'; import {
generateManifestFromDirectory,
generatePostId,
} from '../generate-manifest';
describe( 'generateManifest', () => { describe( 'generateManifest', () => {
const dir = path.join( __dirname, './fixtures/example-docs' ); const dir = path.join( __dirname, './fixtures/example-docs' );
@ -95,6 +98,20 @@ describe( 'generateManifest', () => {
); );
} ); } );
it( 'should generate posts with stable IDs', async () => {
const manifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
expect( manifest.categories[ 0 ].posts[ 0 ].id ).toEqual(
'29bce0a522cef4cd72aad4dd1c9ad5d0b6780704'
);
} );
it( 'should create a hash for each manifest', async () => { it( 'should create a hash for each manifest', async () => {
const manifest = await generateManifestFromDirectory( const manifest = await generateManifestFromDirectory(
rootDir, rootDir,
@ -121,3 +138,33 @@ describe( 'generateManifest', () => {
); );
} ); } );
} ); } );
describe( 'generatePostId', () => {
it( 'should generate a stable ID for the same file', () => {
const id1 = generatePostId(
'get-started/local-development.md',
'woodocs'
);
const id2 = generatePostId(
'get-started/local-development.md',
'woodocs'
);
expect( id1 ).toEqual( id2 );
} );
it( 'should generate a different ID for different prefixes', () => {
const id1 = generatePostId(
'get-started/local-development.md',
'foodocs'
);
const id2 = generatePostId(
'get-started/local-development.md',
'woodocs'
);
expect( id1 ).not.toEqual( id2 );
} );
} );

View File

@ -22,9 +22,9 @@ interface Post {
[ key: string ]: unknown; [ key: string ]: unknown;
} }
function generatePageId( filePath: string, prefix = '' ) { export function generatePostId( filePath: string, prefix = '' ) {
const hash = crypto.createHash( 'sha1' ); const hash = crypto.createHash( 'sha1' );
hash.update( prefix + filePath ); hash.update( `${ prefix }/${ filePath }` );
return hash.digest( 'hex' ); return hash.digest( 'hex' );
} }
@ -34,6 +34,7 @@ async function processDirectory(
projectName: string, projectName: string,
baseUrl: string, baseUrl: string,
baseEditUrl: string, baseEditUrl: string,
fullPathToDocs: string,
checkReadme = true checkReadme = true
): Promise< Category > { ): Promise< Category > {
const category: Category = {}; const category: Category = {};
@ -76,6 +77,9 @@ async function processDirectory(
const post: Post = { ...fileFrontmatter }; const post: Post = { ...fileFrontmatter };
// get the folder name of rootDirectory.
const relativePath = path.relative( fullPathToDocs, filePath );
category.posts.push( { category.posts.push( {
...post, ...post,
url: generateFileUrl( url: generateFileUrl(
@ -84,7 +88,7 @@ async function processDirectory(
subDirectory, subDirectory,
filePath filePath
), ),
id: generatePageId( filePath, projectName ), id: generatePostId( relativePath, projectName ),
} ); } );
} }
} ); } );
@ -101,7 +105,8 @@ async function processDirectory(
subdirectory, subdirectory,
projectName, projectName,
baseUrl, baseUrl,
baseEditUrl baseEditUrl,
fullPathToDocs
); );
category.categories.push( subcategory ); category.categories.push( subcategory );
@ -117,12 +122,15 @@ export async function generateManifestFromDirectory(
baseUrl: string, baseUrl: string,
baseEditUrl: string baseEditUrl: string
) { ) {
const fullPathToDocs = subDirectory;
const manifest = await processDirectory( const manifest = await processDirectory(
rootDirectory, rootDirectory,
subDirectory, subDirectory,
projectName, projectName,
baseUrl, baseUrl,
baseEditUrl, baseEditUrl,
fullPathToDocs,
false false
); );