Skip to content

Commit 02ed01b

Browse files
committed
feat(api): Add decorator processor
1 parent 8a0e735 commit 02ed01b

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed

compiler/api/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function postProcessors(postProcessHtml: any, autoLinkCode: any, API_DOC_TYPES:
7373
postProcessHtml.plugins = [
7474
require('./post-processors/autolink-headings'),
7575
autoLinkCode,
76+
require('./post-processors/codeFormatting'),
7677
];
7778
}
7879

@@ -131,6 +132,7 @@ const nestjs = new Package('nestjs', [
131132
.processor(require('./processors/shortDescription'))
132133
.processor(require('./processors/removeInjectableConstructors'))
133134
.processor(require('./processors/processModuleDocs'))
135+
.processor(require('./processors/processDecorators'))
134136
.processor(require('./processors/computeOutputPath'))
135137
.processor(require('./processors/fixInternalDocumentLinks'))
136138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const has = require('hast-util-has-property');
2+
const is = require('hast-util-is-element');
3+
const slug = require('rehype-slug');
4+
const visit = require('unist-util-visit');
5+
6+
const hasClass = (node, cls) => {
7+
const className = node.properties.className;
8+
return className && className.includes(cls);
9+
};
10+
11+
const addClass = (node, cls) => {
12+
if (!node.properties.className) {
13+
node.properties.className = '';
14+
}
15+
node.properties.className += ' ' + cls;
16+
};
17+
18+
const link = options => tree =>
19+
visit(tree, node => {
20+
if (
21+
is(node, 'pre') &&
22+
!hasClass(node, 'language-typescript') &&
23+
node.children.find(child => child.tagName === 'code')
24+
) {
25+
addClass(node, 'language-typescript');
26+
const $code = node.children.find(child => child.tagName === 'code');
27+
addClass($code, 'language-typescript');
28+
if (!$code.children[0].value.startsWith('\n')) {
29+
$code.children.unshift({ type: 'text', value: '\n' });
30+
}
31+
}
32+
});
33+
34+
module.exports = [slug, [link]];
35+
36+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { byId } from '../util/byId';
2+
3+
module.exports = function processDecorators() {
4+
return {
5+
docTypes: [],
6+
$runAfter: ['processing-docs'],
7+
$runBefore: ['docs-processed'],
8+
$process(docs: any[]) {
9+
docs.forEach((doc, index) => {
10+
if (
11+
doc.fileInfo.baseName.endsWith('decorator') &&
12+
(doc.docType === 'function' || doc.docType === 'const')
13+
) {
14+
doc.docType = 'decorator';
15+
doc.template = 'decorator.template.html';
16+
}
17+
});
18+
}
19+
};
20+
};

compiler/api/processors/processPackages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = function processPackages() {
1919

2020
if (doc.exports) {
2121
const publicExports = doc.exports.filter(doc => !doc.privateExport);
22+
doc.modules = publicExports.filter(doc => doc.docType === 'decorator').sort(byId);
2223
doc.modules = publicExports.filter(doc => doc.docType === 'nestmodule').sort(byId);
2324
doc.classes = publicExports.filter(doc => doc.docType === 'class').sort(byId);
2425
doc.injectables = publicExports.filter(doc => doc.docType === 'injectable').sort(byId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{% import "lib/githubLinks.html" as github -%}
2+
{% import "lib/memberHelpers.html" as memberHelper -%}
3+
{% import "lib/paramList.html" as params -%}
4+
{% extends 'export-base.template.html' %}
5+
6+
{% block overview %}{% include "includes/decorator-overview.html" %}{% endblock %}
7+
{% block details %}
8+
{% include "includes/description.html" %}
9+
10+
<section class="decorator-options">
11+
{% if doc.members.length %}
12+
<h2>Options</h2>
13+
{% for option in doc.members %}
14+
<a id="{$ option.anchor $}"></a>
15+
<table class="is-full-width option-table">
16+
<thead><tr><th>
17+
<div class="with-github-links">
18+
<h3>{$ option.name $}</h3>
19+
{$ github.githubLinks(option, versionInfo) $}
20+
</div>
21+
</th></tr></thead>
22+
<tbody>
23+
<tr>
24+
<td>
25+
{% if option.shortDescription %}{$ option.shortDescription | marked $}{% endif %}
26+
</td>
27+
</tr>
28+
<tr>
29+
<td>
30+
<code-example language="ts" hideCopy="true" class="no-box api-heading">
31+
{$ option.name $}: {$ option.type | escape $}
32+
</code-example>
33+
</td>
34+
</tr>
35+
<tr>
36+
<td>
37+
{%- if option.description %}
38+
{$ option.description | marked({ h3: 'h4' }) $}
39+
{% endif %}
40+
41+
{%- if option.usageNotes %}
42+
{$ option.usageNotes | marked({ h3: 'h4' }) $}
43+
{% endif %}
44+
</td>
45+
</tr>
46+
</tbody>
47+
</table>
48+
{% endfor %}
49+
{% endif %}
50+
</section>
51+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% import "lib/descendants.html" as descendants -%}
2+
3+
{% macro renderOptionsTable(doc) %}
4+
<table class="is-full-width list-table option-overview">
5+
<thead>
6+
<tr><th>Option</th><th>Description</th></tr>
7+
</thead>
8+
<tbody>
9+
{%- for option in doc.members %}
10+
<tr class="option">
11+
<td>
12+
<a class="code-anchor" href="{$ doc.path $}#{$ option.anchor | urlencode $}">
13+
<code>{$ option.name $}</code>
14+
</a>
15+
</td>
16+
<td>{$ option.shortDescription | marked $}</td>
17+
</tr>
18+
{% endfor -%}
19+
</tbody>
20+
</table>
21+
22+
{% for ancestor in doc.extendsClauses %}{% if ancestor.doc %}
23+
<h3 class="no-toc">Inherited from <a class="code-anchor" href="{$ ancestor.doc.path $}">{$ ancestor.doc.name $}</a> decorator</h3>
24+
{$ renderOptionsTable(ancestor.doc) $}
25+
{% endif %}{% endfor %}
26+
{% endmacro %}
27+
28+
{% if doc.members.length %}
29+
<section class="decorator-overview">
30+
{$ renderOptionsTable(doc) $}
31+
{$ descendants.renderDescendants(doc, 'decorator', 'Subclasses') $}
32+
</section>
33+
{% endif %}

0 commit comments

Comments
 (0)