Skip to content

Commit

Permalink
chore(contented-pipeline): un-complicate getSanitizedPath parsing (#635)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:

As per title.
  • Loading branch information
fuxingloh authored Oct 10, 2023
1 parent bc67628 commit 6f5d0e5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 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
28 changes: 14 additions & 14 deletions packages/contented-processor/src/ContentedProcessor.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -431,7 +431,7 @@ it('should dedup 2 files', async () => {
pipelines: [
{
type: 'Markdown',
pattern: '**/:1:Category/*.md',
pattern: '**/1-Category/*.md',
processor: 'md',
},
{
Expand All @@ -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 () => {
Expand All @@ -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',
});
Expand Down

0 comments on commit 6f5d0e5

Please sign in to comment.