Files
docs.nestjs.com/tools/transforms/content-package/processors/extractContentTitle.ts
2024-10-18 09:22:15 +02:00

31 lines
900 B
TypeScript

import { Processor, DocCollection } from 'dgeni';
/**
* Extracts the title of a content file.
* This processor assumes that the first line of the
* content includes the title.
*/
export class ExtractContentTitleProcessor implements Processor {
$runAfter = ['renderDocsProcessor'];
$runBefore = ['convertToJsonProcessor'];
$process(docs: DocCollection) {
docs.forEach((doc) => {
if (doc.docType === 'content') {
try {
const firstLine: string = doc.content.split('\n')[0];
const title = firstLine.replace(/#/g, '').trim();
doc.title = title;
} catch {
// We do not care if the title does not exist here
// convertToJson will complain later in case the title
// does not exist
}
}
});
}
}
export function extractContentTitleProcessor() {
return new ExtractContentTitleProcessor();
}