From 350f0c88ef1fbddb79f6b94f62b86f925722d68d Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Tue, 10 Oct 2023 21:10:22 +0800 Subject: [PATCH 1/2] chore(contented-pipeline): un-complicate getSanitizedPath parsing --- packages/contented-pipeline/src/Pipeline.ts | 3 ++- .../contented-pipeline/src/Pipeline.unit.ts | 24 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/contented-pipeline/src/Pipeline.ts b/packages/contented-pipeline/src/Pipeline.ts index b36d565c..ea60f788 100644 --- a/packages/contented-pipeline/src/Pipeline.ts +++ b/packages/contented-pipeline/src/Pipeline.ts @@ -135,7 +135,8 @@ export abstract class ContentedPipeline { } protected replacePrefix(path: string): string { - const matched = path.match(/^(:\d+:|\(\d+\)|\[\d+]|\d+-)(.+)$/); + // Remove the numbered prefix if it exists: For example, from 00-file.md to file.md + const matched = path.match(/^(\d+-)(.+)$/); if (matched !== null) { return matched[2]; } diff --git a/packages/contented-pipeline/src/Pipeline.unit.ts b/packages/contented-pipeline/src/Pipeline.unit.ts index 1aa9acc2..0743d65b 100644 --- a/packages/contented-pipeline/src/Pipeline.unit.ts +++ b/packages/contented-pipeline/src/Pipeline.unit.ts @@ -28,13 +28,13 @@ it('should replace numeric prefix path', () => { expect(pipeline.getSanitizedPath('path-1.md')).toStrictEqual('path-1'); - expect(pipeline.getSanitizedPath(':01:path.md')).toStrictEqual('path'); + expect(pipeline.getSanitizedPath(':01:path.md')).toStrictEqual('01-path'); expect(pipeline.getSanitizedPath(':01path.md')).toStrictEqual('01path'); - expect(pipeline.getSanitizedPath('[01]path.md')).toStrictEqual('path'); + expect(pipeline.getSanitizedPath('[01]path.md')).toStrictEqual('01-path'); expect(pipeline.getSanitizedPath('01]path.md')).toStrictEqual('01-path'); - expect(pipeline.getSanitizedPath('(01)path.md')).toStrictEqual('path'); + expect(pipeline.getSanitizedPath('(01)path.md')).toStrictEqual('01-path'); expect(pipeline.getSanitizedPath('(01path.md')).toStrictEqual('01path'); expect(pipeline.getSanitizedPath('01-path.md')).toStrictEqual('path'); @@ -45,9 +45,9 @@ it('should replace numeric prefix path', () => { expect(pipeline.getSanitizedPath('Header/Path.md')).toStrictEqual('header/path'); expect(pipeline.getSanitizedPath('Header/01-Path.md')).toStrictEqual('header/path'); expect(pipeline.getSanitizedPath('01-Header/01-Path.md')).toStrictEqual('header/path'); - expect(pipeline.getSanitizedPath('01-Header/[01]Path.md')).toStrictEqual('header/path'); - expect(pipeline.getSanitizedPath('(01)Header/[01]Path.md')).toStrictEqual('header/path'); - expect(pipeline.getSanitizedPath(':01:Header/[01-Path.md')).toStrictEqual('header/01-path'); + expect(pipeline.getSanitizedPath('01-Header/[01]Path.md')).toStrictEqual('header/01-path'); + expect(pipeline.getSanitizedPath('(01)Header/[01]Path.md')).toStrictEqual('01-header/01-path'); + expect(pipeline.getSanitizedPath(':01:Header/[01-Path.md')).toStrictEqual('01-header/01-path'); }); it('should preserve fragment identifiers for files', () => { @@ -60,13 +60,13 @@ it('should preserve fragment identifiers for files', () => { // With file extensions expect(pipeline.getSanitizedPath('path-1.md#content1')).toStrictEqual('path-1#content1'); - expect(pipeline.getSanitizedPath(':01:path.md#content1')).toStrictEqual('path#content1'); + expect(pipeline.getSanitizedPath(':01:path.md#content1')).toStrictEqual('01-path#content1'); expect(pipeline.getSanitizedPath(':01path.md#content1')).toStrictEqual('01path#content1'); - expect(pipeline.getSanitizedPath('[01]path.md#content1')).toStrictEqual('path#content1'); + expect(pipeline.getSanitizedPath('[01]path.md#content1')).toStrictEqual('01-path#content1'); expect(pipeline.getSanitizedPath('01]path.md#content1')).toStrictEqual('01-path#content1'); - expect(pipeline.getSanitizedPath('(01)path.md#content1')).toStrictEqual('path#content1'); + expect(pipeline.getSanitizedPath('(01)path.md#content1')).toStrictEqual('01-path#content1'); expect(pipeline.getSanitizedPath('(01path.md#content1')).toStrictEqual('01path#content1'); expect(pipeline.getSanitizedPath('01-path.md#content1')).toStrictEqual('path#content1'); @@ -77,9 +77,9 @@ it('should preserve fragment identifiers for files', () => { expect(pipeline.getSanitizedPath('Header/Path.md#content1')).toStrictEqual('header/path#content1'); expect(pipeline.getSanitizedPath('Header/01-Path.md#content1')).toStrictEqual('header/path#content1'); expect(pipeline.getSanitizedPath('01-Header/01-Path.md#content1')).toStrictEqual('header/path#content1'); - expect(pipeline.getSanitizedPath('01-Header/[01]Path.md#content1')).toStrictEqual('header/path#content1'); - expect(pipeline.getSanitizedPath('(01)Header/[01]Path.md#content1')).toStrictEqual('header/path#content1'); - expect(pipeline.getSanitizedPath(':01:Header/[01-Path.md#content1')).toStrictEqual('header/01-path#content1'); + expect(pipeline.getSanitizedPath('01-Header/[01]Path.md#content1')).toStrictEqual('header/01-path#content1'); + expect(pipeline.getSanitizedPath('(01)Header/[01]Path.md#content1')).toStrictEqual('01-header/01-path#content1'); + expect(pipeline.getSanitizedPath(':01:Header/[01-Path.md#content1')).toStrictEqual('01-header/01-path#content1'); // Without file extensions expect(pipeline.getSanitizedPath('path01#content1')).toStrictEqual('path01#content1'); From d14f1af8425ed16793fb8c9c9b48396428a3a089 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Tue, 10 Oct 2023 23:44:29 +0800 Subject: [PATCH 2/2] fix all other tests --- .../:1:slug.md => 1-Category/2-slug.md} | 0 .../Section/2-path.md} | 0 .../fixtures/{:2:path-1.md => 2-path-1.md} | 0 .../src/ContentedProcessor.unit.ts | 28 +++++++++---------- 4 files changed, 14 insertions(+), 14 deletions(-) rename packages/contented-processor/fixtures/{:1:Category/:1:slug.md => 1-Category/2-slug.md} (100%) rename packages/contented-processor/fixtures/{:1:Category/Section/:2:path.md => 1-Category/Section/2-path.md} (100%) rename packages/contented-processor/fixtures/{:2:path-1.md => 2-path-1.md} (100%) diff --git a/packages/contented-processor/fixtures/:1:Category/:1:slug.md b/packages/contented-processor/fixtures/1-Category/2-slug.md similarity index 100% rename from packages/contented-processor/fixtures/:1:Category/:1:slug.md rename to packages/contented-processor/fixtures/1-Category/2-slug.md diff --git a/packages/contented-processor/fixtures/:1:Category/Section/:2:path.md b/packages/contented-processor/fixtures/1-Category/Section/2-path.md similarity index 100% rename from packages/contented-processor/fixtures/:1:Category/Section/:2:path.md rename to packages/contented-processor/fixtures/1-Category/Section/2-path.md diff --git a/packages/contented-processor/fixtures/:2:path-1.md b/packages/contented-processor/fixtures/2-path-1.md similarity index 100% rename from packages/contented-processor/fixtures/:2:path-1.md rename to packages/contented-processor/fixtures/2-path-1.md diff --git a/packages/contented-processor/src/ContentedProcessor.unit.ts b/packages/contented-processor/src/ContentedProcessor.unit.ts index 553a973a..fc179b36 100644 --- a/packages/contented-processor/src/ContentedProcessor.unit.ts +++ b/packages/contented-processor/src/ContentedProcessor.unit.ts @@ -52,8 +52,8 @@ describe('process', () => { ]); }); - it('should process :2:path-1.md', async () => { - expect(await processor.process(':2:path-1.md')).toStrictEqual([ + it('should process 2-path-1.md', async () => { + expect(await processor.process('2-path-1.md')).toStrictEqual([ { type: 'Markdown', fields: { @@ -70,8 +70,8 @@ describe('process', () => { ]); }); - it('should process :1:Category/:1:slug.md', async () => { - expect(await processor.process(':1:Category/:1:slug.md')).toStrictEqual([ + it('should process 1-Category/2-slug.md', async () => { + expect(await processor.process('1-Category/2-slug.md')).toStrictEqual([ { type: 'Markdown', fields: { @@ -95,8 +95,8 @@ describe('process', () => { ]); }); - it('should process :1:Category/Section/:2:path.md', async () => { - expect(await processor.process(':1:Category/Section/:2:path.md')).toStrictEqual([ + it('should process 1-Category/Section/2-path.md', async () => { + expect(await processor.process('1-Category/Section/2-path.md')).toStrictEqual([ { type: 'Markdown', fields: { @@ -214,7 +214,7 @@ describe('build', () => { const processor = new ContentedProcessor(config); it('should build 2 files', async () => { - const result = await processor.build(':2:path-1.md', ':1:Category/Section/:2:path.md'); + const result = await processor.build('2-path-1.md', '1-Category/Section/2-path.md'); expect(result).toStrictEqual({ pipelines: { Markdown: [ @@ -256,10 +256,10 @@ describe('build', () => { it('should build 4 files', async () => { const result = await processor.build( - ':2:path-1.md', + '2-path-1.md', 'Foo.Bar.md', - ':1:Category/Section/:2:path.md', - ':1:Category/:1:slug.md', + '1-Category/Section/2-path.md', + '1-Category/2-slug.md', ); expect(result).toStrictEqual({ @@ -373,7 +373,7 @@ describe('custom', () => { const processor = new ContentedProcessor(config); it('should use custom pipeline', async () => { - expect(await processor.process(':2:path-1.md')).toStrictEqual([ + expect(await processor.process('2-path-1.md')).toStrictEqual([ { fileId: expect.stringMatching(/[0-f]{64}/), modifiedDate: expect.any(Number), @@ -431,7 +431,7 @@ it('should dedup 2 files', async () => { pipelines: [ { type: 'Markdown', - pattern: '**/:1:Category/*.md', + pattern: '**/1-Category/*.md', processor: 'md', }, { @@ -442,7 +442,7 @@ it('should dedup 2 files', async () => { ], }; const processor = new ContentedProcessor(config); - await processor.build(':2:path-1.md', ':1:Category/Section/:2:path.md'); + await processor.build('2-path-1.md', '1-Category/Section/2-path.md'); }); it('should sort 2 files', async () => { @@ -464,7 +464,7 @@ it('should sort 2 files', async () => { ], }; const processor = new ContentedProcessor(config); - const files = await processor.build(':2:path-1.md', ':1:Category/Section/:2:path.md'); + const files = await processor.build('2-path-1.md', '1-Category/Section/2-path.md'); expect(files.pipelines.Markdown[0]).toMatchObject({ path: '/category/section/path', });