Skip to content

Commit

Permalink
chore(contented): include headings in FileIndex (#587)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:

Include `headings: FileContentHeadings[];` in `FileIndex` to allow "lite
search plugins" to just read `index.json` to build search indexes
without traversing through all `fileId.json`. This make sense because
other that `html` in `FileContent` headings is a useful metadata to
locate sub sections within pages.
  • Loading branch information
fuxingloh authored Aug 24, 2023
1 parent d710093 commit 7717c0c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/contented-pipeline/src/Pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export abstract class ContentedPipeline {
modifiedDate: await this.computeModifiedDate(filePath),
sections,
fields: {},
headings: [],
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contented-pipeline/src/PipelineFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export interface FileIndex extends PipelineFile {
sections: string[];
modifiedDate: number;
fields: Record<string, any>;
headings: FileContentHeadings[];
}

/**
* FileContent with html, saved as individual file.
*/
export interface FileContent extends FileIndex {
html: string;
headings: FileContentHeadings[];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/contented-processor/src/ContentedProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ContentedProcessor {

for (const file of files) {
for (const content of await this.process(file)) {
result.pipelines[content.type].push(cloneFileIndex(content));
result.pipelines[content.type].push(toFileIndex(content));
}
}

Expand Down Expand Up @@ -169,13 +169,14 @@ function getPipelineUniqueKey(pipeline: Pipeline) {
return `${pipeline.type}:${pipeline.dir ?? ''}:${pipeline.pattern.join(',')}`;
}

function cloneFileIndex(index: FileIndex): FileIndex {
function toFileIndex(index: FileContent): FileIndex {
return {
fileId: index.fileId,
fields: index.fields,
modifiedDate: index.modifiedDate,
path: index.path,
sections: index.sections,
type: index.type,
headings: index.headings,
};
}
40 changes: 40 additions & 0 deletions packages/contented-processor/src/ContentedProcessor.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('build', () => {
description: 'Title',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [],
modifiedDate: expect.any(Number),
path: '/path-1',
sections: [],
Expand All @@ -163,6 +164,14 @@ describe('build', () => {
description: 'Content',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [
{
children: [],
depth: 2,
headingId: 'header',
title: 'Header',
},
],
modifiedDate: expect.any(Number),
path: '/category/section/path',
sections: ['Category', 'Section'],
Expand Down Expand Up @@ -190,6 +199,7 @@ describe('build', () => {
description: 'Title',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [],
modifiedDate: expect.any(Number),
path: '/path-1',
sections: [],
Expand All @@ -201,6 +211,20 @@ describe('build', () => {
description: 'Markdown for testing ContentedProcessor.unit.ts.',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [
{
children: [],
depth: 1,
headingId: 'what-is-going-on',
title: 'What is going on',
},
{
children: [],
depth: 1,
headingId: 'multiple-title-but-take-the-first-one',
title: 'Multiple title but take the first one',
},
],
modifiedDate: expect.any(Number),
path: '/foo-bar',
sections: [],
Expand All @@ -212,6 +236,14 @@ describe('build', () => {
description: 'Content',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [
{
children: [],
depth: 2,
headingId: 'header',
title: 'Header',
},
],
modifiedDate: expect.any(Number),
path: '/category/section/path',
sections: ['Category', 'Section'],
Expand All @@ -223,6 +255,14 @@ describe('build', () => {
description: 'Content',
},
fileId: expect.stringMatching(/[0-f]{64}/),
headings: [
{
children: [],
depth: 2,
headingId: 'header',
title: 'Header',
},
],
modifiedDate: expect.any(Number),
path: '/category/slug',
sections: ['Category'],
Expand Down

0 comments on commit 7717c0c

Please sign in to comment.