Skip to content

Commit

Permalink
feat(dgeni): add support for extra fields in JSON (#3038)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Aug 27, 2024
1 parent 2a3cf26 commit d830f75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 16 additions & 2 deletions tools/dgeni/src/processors/convertToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ export class ConvertToJsonProcessor implements Processor {
name = 'convertToJson';
$runBefore = ['writeFilesProcessor'];
docTypes = [];
/**
* Extra doc fields to be copied into the written JSON document.
* Take care that these fields are serializable.
*/
extraFields = [];

constructor(public log, public createDocMessage) {}

$process(docs: Document[]) {
const docTypes = this.docTypes;
docs.forEach((doc) => {
if (docTypes.indexOf(doc.docType) !== -1) {
if (docTypes.includes(doc.docType)) {
const contents = doc.renderedContent || '';

let title = doc.title;
Expand All @@ -34,7 +39,16 @@ export class ConvertToJsonProcessor implements Processor {
this.log.warn(this.createDocMessage('Title property expected', doc));
}

doc.renderedContent = JSON.stringify({ id: doc.path, title, contents, tableOfContents }, null, 2);
doc.renderedContent = JSON.stringify({
...this.extraFields.reduce((acc, field) => {
acc[field] = doc[field];
return acc;
}, {}),
id: doc.path,
title,
contents,
tableOfContents,
}, null, 2);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import {
Document,
} from 'dgeni';

export class DesignExampleConvertToJsonProcessor implements Processor {
name = 'convertToJson';
$runBefore = ['writeFilesProcessor'];
docTypes = [];

constructor(public log, public createDocMessage) {}
import { ConvertToJsonProcessor } from '../../../processors/convertToJson';

export class DesignExampleConvertToJsonProcessor extends ConvertToJsonProcessor implements Processor {
$process(docs: Document[]) {
const docTypes = this.docTypes;
docs.forEach((doc) => {
if (docTypes.indexOf(doc.docType) !== -1) {
if (docTypes.includes(doc.docType)) {
doc.renderedContent = JSON.stringify({
id: doc.id,
docType: doc.docType,
Expand Down

0 comments on commit d830f75

Please sign in to comment.