Skip to content

Commit

Permalink
chore(contented-pipeline): un-complicate getSanitizedPath parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed Oct 10, 2023
1 parent f77d327 commit 350f0c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/contented-pipeline/src/Pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
24 changes: 12 additions & 12 deletions packages/contented-pipeline/src/Pipeline.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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', () => {
Expand All @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 350f0c8

Please sign in to comment.