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:
parent
9b7570f8b2
commit
26851bd3ce
|
@ -6,7 +6,10 @@ import path from 'path';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { generateManifestFromDirectory } from '../generate-manifest';
|
||||
import {
|
||||
generateManifestFromDirectory,
|
||||
generatePostId,
|
||||
} from '../generate-manifest';
|
||||
|
||||
describe( 'generateManifest', () => {
|
||||
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 () => {
|
||||
const manifest = await generateManifestFromDirectory(
|
||||
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 );
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -22,9 +22,9 @@ interface Post {
|
|||
[ key: string ]: unknown;
|
||||
}
|
||||
|
||||
function generatePageId( filePath: string, prefix = '' ) {
|
||||
export function generatePostId( filePath: string, prefix = '' ) {
|
||||
const hash = crypto.createHash( 'sha1' );
|
||||
hash.update( prefix + filePath );
|
||||
hash.update( `${ prefix }/${ filePath }` );
|
||||
return hash.digest( 'hex' );
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ async function processDirectory(
|
|||
projectName: string,
|
||||
baseUrl: string,
|
||||
baseEditUrl: string,
|
||||
fullPathToDocs: string,
|
||||
checkReadme = true
|
||||
): Promise< Category > {
|
||||
const category: Category = {};
|
||||
|
@ -76,6 +77,9 @@ async function processDirectory(
|
|||
|
||||
const post: Post = { ...fileFrontmatter };
|
||||
|
||||
// get the folder name of rootDirectory.
|
||||
const relativePath = path.relative( fullPathToDocs, filePath );
|
||||
|
||||
category.posts.push( {
|
||||
...post,
|
||||
url: generateFileUrl(
|
||||
|
@ -84,7 +88,7 @@ async function processDirectory(
|
|||
subDirectory,
|
||||
filePath
|
||||
),
|
||||
id: generatePageId( filePath, projectName ),
|
||||
id: generatePostId( relativePath, projectName ),
|
||||
} );
|
||||
}
|
||||
} );
|
||||
|
@ -101,7 +105,8 @@ async function processDirectory(
|
|||
subdirectory,
|
||||
projectName,
|
||||
baseUrl,
|
||||
baseEditUrl
|
||||
baseEditUrl,
|
||||
fullPathToDocs
|
||||
);
|
||||
|
||||
category.categories.push( subcategory );
|
||||
|
@ -117,12 +122,15 @@ export async function generateManifestFromDirectory(
|
|||
baseUrl: string,
|
||||
baseEditUrl: string
|
||||
) {
|
||||
const fullPathToDocs = subDirectory;
|
||||
|
||||
const manifest = await processDirectory(
|
||||
rootDirectory,
|
||||
subDirectory,
|
||||
projectName,
|
||||
baseUrl,
|
||||
baseEditUrl,
|
||||
fullPathToDocs,
|
||||
false
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue