Skip to content

Commit dd50aca

Browse files
author
Sebastian Schürmann
committed
feature(vanillin): add a own doctag for demo cases
1 parent baf9492 commit dd50aca

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/vanillin/src/doc-gen.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
import { TSDocParser, type ParserContext, type DocComment } from '@microsoft/tsdoc';
1+
import { TSDocParser, type ParserContext, type DocComment, TSDocConfiguration, TSDocTagDefinition, TSDocTagSyntaxKind } from '@microsoft/tsdoc';
22
import * as path from 'path';
33
import * as fs from 'fs';
44
import { Formatter } from './formatter';
55
export class DocGen {
6+
67
parseDoc(doc: string): ParserContext {
78
const inputFilename: string = path.resolve(doc);
89
const inputBuffer: string = fs.readFileSync(inputFilename).toString();
9-
const tsdocParser: TSDocParser = new TSDocParser();
10+
11+
const customConfiguration: TSDocConfiguration = new TSDocConfiguration();
12+
13+
const customBlockDefinition: TSDocTagDefinition = new TSDocTagDefinition({
14+
tagName: '@demo',
15+
syntaxKind: TSDocTagSyntaxKind.BlockTag
16+
});
17+
18+
customConfiguration.addTagDefinitions([
19+
customBlockDefinition
20+
]);
21+
22+
const tsdocParser: TSDocParser = new TSDocParser(customConfiguration);
1023
const parserContext: ParserContext = tsdocParser.parseString(inputBuffer);
1124
return parserContext;
1225
}
@@ -16,4 +29,10 @@ export class DocGen {
1629
const result = Formatter.renderDocNodes(docComment.getChildNodes());
1730
return result;
1831
}
32+
33+
renderDocs(context: ParserContext) {
34+
const docComment: DocComment = context.docComment;
35+
const result = Formatter.renderDocNodes(docComment.customBlocks);
36+
return result;
37+
}
1938
}

packages/vanillin/test/doc-gen.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ describe('DocGen', () => {
88
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
99
assert.ok(result, 'ParserContext should be returned');
1010
});
11+
12+
it('returns the demo tag', () => {
13+
const docGen = new DocGen();
14+
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
15+
assert.equal(result.docComment.customBlocks.length, 1);
16+
});
17+
18+
it('rendered result contains tag', () => {
19+
const docGen = new DocGen();
20+
const result = docGen.parseDoc('./test/fixtures/my-circle.ts');
21+
const doc = docGen.renderDocs(result);
22+
assert.match(doc, /my-circle/);
23+
});
1124
});

packages/vanillin/test/fixtures/my-circle.ts

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
* <my-circle size="100" color="blue"></my-circle>
77
* ```
88
*/
9+
/**
10+
* A demo of a custom web component that renders a circle using SVG.
11+
*
12+
* @demo
13+
* ```html
14+
* <my-circle size="100" color="blue"></my-circle>
15+
* ```
16+
*/
917
class MyCircle extends HTMLElement {
1018
/** The diameter of the circle in pixels. Default is 50. */
1119
private _size: number = 50;

0 commit comments

Comments
 (0)