Skip to content

Commit

Permalink
feat(dgeni,docs,daffio): implement ToC for all doc kinds (#3423)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Jan 10, 2025
1 parent d3cee1e commit 676e498
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<daffio-doc-article [breadcrumbs]="doc().breadcrumbs">
<daffio-doc-article [breadcrumbs]="doc().breadcrumbs" [toc]="doc().tableOfContents">
@if (isApiPackage()) {
<daffio-api-package [doc]="doc()"></daffio-api-package>
} @else {
<div [innerHTML]="doc().contents | safe"></div>
@if (doc().examples.length > 0) {
<h2 id="examples">Examples</h2>
}
@for (example of doc().examples; track example.id) {
<h3 [attr.id]="example.id">{{example.caption}}</h3>
<div [innerHTML]="example.body | safe"></div>
}
}
</daffio-doc-article>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<daffio-doc-article [breadcrumbs]="doc().breadcrumbs">
<daffio-doc-article [breadcrumbs]="doc().breadcrumbs" [toc]="doc().tableOfContents">
<div [innerHTML]="doc().contents | safe"></div>
</daffio-doc-article>
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';

import { DaffLinkSetModule } from '@daffodil/design/link-set';
import { DaffGuideDoc } from '@daffodil/docs-utils';
import { DaffDoc } from '@daffodil/docs-utils';

import { DaffioDocsTableOfContentsComponent } from './table-of-contents.component';
import { DaffioDocsFactory } from '../../testing/factories/docs.factory';

describe('DaffioDocsTableOfContentsComponent', () => {
let component: DaffioDocsTableOfContentsComponent;
let fixture: ComponentFixture<DaffioDocsTableOfContentsComponent>;
let stubDaffioDoc: DaffGuideDoc;
let stubDaffioDoc: DaffDoc;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@angular/core';

import {
DaffGuideDoc,
DaffDoc,
DaffDocKind,
} from '@daffodil/docs-utils';

Expand All @@ -23,8 +23,8 @@ import { DaffioDocsDynamicContent } from '../../../dynamic-content/dynamic-conte
DaffioSafeHtmlPipe,
],
})
export class DaffioDocsGuidesContentComponent implements DaffioDocsDynamicContent<DaffGuideDoc> {
export class DaffioDocsGuidesContentComponent implements DaffioDocsDynamicContent<DaffDoc> {
static readonly kind = DaffDocKind.GUIDE;

doc = input<DaffGuideDoc>();
doc = input<DaffDoc>();
}
6 changes: 3 additions & 3 deletions apps/daffio/src/app/docs/testing/factories/docs.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { sample } from '@daffodil/core';
import { DaffModelFactory } from '@daffodil/core/testing';
import {
DaffDocKind,
DaffGuideDoc,
DaffDoc,
} from '@daffodil/docs-utils';

export class MockDoc implements DaffGuideDoc {
export class MockDoc implements DaffDoc {
id = String(faker.datatype.number(1000));
kind = sample(Object.values(DaffDocKind));
title = faker.lorem.words();
Expand All @@ -27,7 +27,7 @@ export class MockDoc implements DaffGuideDoc {
@Injectable({
providedIn: 'root',
})
export class DaffioDocsFactory extends DaffModelFactory<DaffGuideDoc>{
export class DaffioDocsFactory extends DaffModelFactory<DaffDoc>{
constructor() {
super(MockDoc);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/docs-utils/src/doc/api.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DaffDocExample } from './example.type';
import { DaffDoc } from './type';
import { DaffDocExample } from '../example/public_api';

/**
* An API doc that includes the type of the symbol.
Expand Down
9 changes: 0 additions & 9 deletions libs/docs-utils/src/doc/guide.type.ts

This file was deleted.

17 changes: 10 additions & 7 deletions libs/docs-utils/src/doc/package-guide.type.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { DaffApiDoc } from '@daffodil/docs-utils';

import { DaffGuideDoc } from './guide.type';
import { DaffDoc } from './type';
import { DaffDocTableOfContents } from '../toc/public_api';

/**
* A guide doc for a package.
*/
export interface DaffPackageGuideDoc extends DaffGuideDoc {
export interface DaffPackageGuideDoc extends DaffDoc {
/**
* A list of symbol paths exported from the package.
*/
symbols?: Array<string>;
symbols: Array<string>;
/**
* A list of rendered API docs.
*/
api: Array<string>;
/**
* A list of API docs for symbols exported from this package.
* A table of contents for the API section.
*/
api?: Array<DaffApiDoc>;
apiToc: DaffDocTableOfContents;
}
3 changes: 0 additions & 3 deletions libs/docs-utils/src/doc/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './api.type';
export * from './example.type';
export * from './guide.type';
export * from './package-guide.type';
export * from './toc.type';
export * from './type';
5 changes: 3 additions & 2 deletions libs/docs-utils/src/doc/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DaffBreadcrumb } from '@daffodil/docs-utils';

import { DaffBreadcrumb } from '../breadcrumb/public_api';
import { DaffDocKind } from '../kind/public_api';
import { DaffDocTableOfContents } from '../toc/public_api';

/**
* A basic generated document that represents some piece of documentation.
Expand All @@ -11,4 +11,5 @@ export interface DaffDoc {
contents: string;
breadcrumbs: Array<DaffBreadcrumb>;
kind: DaffDocKind;
tableOfContents: DaffDocTableOfContents;
}
1 change: 1 addition & 0 deletions libs/docs-utils/src/example/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './type';
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* A usage example.
*/
export interface DaffDocExample {
/**
* The ID of the example.
* Since not all examples will have captions, a unique is useful for fragment anchor scrolling.
*/
id: string;
/**
* The short caption describing the example.
* This should be just plain text.
Expand Down
9 changes: 6 additions & 3 deletions libs/docs-utils/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { crossOsFilename } from './cross-os-filename';
export * from './kind/public_api';
export * from './path';

export * from './breadcrumb/public_api';
export * from './nav/public_api';
export * from './doc/public_api';
export * from './path';
export * from './example/public_api';
export * from './kind/public_api';
export * from './nav/public_api';
export * from './toc/public_api';
1 change: 1 addition & 0 deletions libs/docs-utils/src/toc/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './type';
File renamed without changes.
11 changes: 9 additions & 2 deletions tools/dgeni/src/processors/add-api-symbols-to-package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Document } from 'dgeni';
import type { Environment } from 'nunjucks';

import { DaffApiDoc } from '@daffodil/docs-utils';

import { CollectLinkableSymbolsProcessor } from './collect-linkable-symbols';
import { API_TEMPLATES_PATH } from '../transforms/config';
import { FilterableProcessor } from '../utils/filterable-processor.type';
Expand Down Expand Up @@ -30,8 +32,13 @@ export class AddApiSymbolsToPackagesProcessor implements FilterableProcessor {

const ret = docs.map(doc => {
if (this.docTypes.includes(doc.docType)) {
doc.symbols = CollectLinkableSymbolsProcessor.packages.get(this.lookup(doc))?.map((d) => d.path);
doc.api = CollectLinkableSymbolsProcessor.packages.get(this.lookup(doc))?.map((symbol) => render(this.templateFinder.getFinder()(symbol), { doc: symbol, child: true }));
const exportDocs = CollectLinkableSymbolsProcessor.packages.get(this.lookup(doc));
doc.symbols = exportDocs?.map((d) => d.path);
doc.api = exportDocs?.map((symbol) => render(this.templateFinder.getFinder()(symbol), { doc: symbol, child: true }));
doc.apiToc = exportDocs?.flatMap((symbol: DaffApiDoc) => symbol.tableOfContents.map((entry) => ({
...entry,
lvl: entry.lvl + 1,
})));
}
return doc;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Document } from 'dgeni';
import { slugify } from 'markdown-toc';

import { DaffDocExample } from '@daffodil/docs-utils';
import {
DaffDocExample,
DaffDocTableOfContents,
} from '@daffodil/docs-utils';

import { MARKDOWN_CODE_PROCESSOR_NAME } from '../../../processors/markdown';
import { FilterableProcessor } from '../../../utils/filterable-processor.type';

export const EXAMPLES_PROCESSOR_NAME = 'examples';

const genExamplesToc = (examples: Array<DaffDocExample>): DaffDocTableOfContents =>
examples.map((example) => ({
content: example.caption,
lvl: 3,
slug: example.id,
}));

/**
* Adds subpackage entry point docs to the containing package doc.
*/
Expand All @@ -20,10 +31,20 @@ export class ExamplesProcessor implements FilterableProcessor {
$process(docs: Array<Document>): Array<Document> {
return docs.map((doc) => {
if (this.docTypes.includes(doc.docType)) {
doc.examples = doc.tags.tagsByName.get('example')?.map((example): DaffDocExample => ({
doc.examples = doc.tags.tagsByName.get('example')?.map((example, i): DaffDocExample => ({
id: slugify(`${doc.name}-example-${i}`),
caption: example.caption,
body: example.body,
})) || [];
doc.tableOfContents = doc.examples.length > 0 ? [
{
content: 'Examples',
lvl: 2,
// TODO: add doc-specific prefix
slug: 'examples',
},
...genExamplesToc(doc.examples),
] : [];
}
return doc;
});
Expand Down

0 comments on commit 676e498

Please sign in to comment.