Skip to content

Commit 292de16

Browse files
author
Sebastian Schürmann
committed
refactor(banira): refactor doc page generation interfaces
1 parent 61e16a7 commit 292de16

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

packages/banira/src/doc-gen.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,29 @@ export class DocGen {
3030
syntaxKind: TSDocTagSyntaxKind.BlockTag
3131
});
3232

33-
constructor() {
33+
public readonly tagName: string;
34+
35+
get src(): string {
36+
return `./dist/${this.tagName}.js`;
37+
}
38+
39+
get title(): string {
40+
return `<${this.tagName}> Component Demo`;
41+
}
42+
43+
constructor(tagName: string = "my-circle") {
44+
this.tagName = tagName;
3445
// Add custom tag definitions to detect demo blocks
3546
this.customConfiguration.addTagDefinitions([
3647
DocGen.CUSTOM_BLOCK_DEFINITION_DEMO
3748
]);
3849
this.tsdocParser = new TSDocParser(this.customConfiguration);
3950
}
4051

52+
fromString(sourceCode: string): ParserContext {
53+
return this.tsdocParser.parseString(sourceCode);
54+
}
55+
4156
/**
4257
* Parses a TypeScript source file to extract its documentation.
4358
*

packages/banira/src/formatter/doc-page.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export class FormatterDocPage {
1919
return this.context.docComment.summarySection
2020
}
2121

22-
createDocPage(): string {
23-
const title = "MyCircle Component Demo";
24-
const src = "../dist/my-circle.js"
25-
const tagName = "my-circle"
22+
createDocPage(tagName: string = "my-circle", src: string = "../dist/my-circle.js", title: string = "MyCircle Component Demo"): string {
2623
return `<!DOCTYPE html>
2724
<html lang="en">
2825
<head>

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

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
1-
import { describe, it } from "node:test";
1+
import { describe, it, before } from "node:test";
22
import assert from "node:assert";
33
import { DocGen } from '../src/doc-gen';
4+
import { ParserContext } from "@microsoft/tsdoc";
45

56
describe('DocGen', () => {
6-
it('should parse a typescript file and return a ParserContext', async () => {
7-
const docGen = new DocGen();
8-
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
9-
assert.ok(result, 'ParserContext should be returned');
7+
let docGen: DocGen;
8+
let context: ParserContext;
9+
10+
before(async () => {
11+
docGen = new DocGen('my-circle');
12+
context = await docGen.parseDoc('./test/fixtures/my-circle.ts');
13+
});
14+
15+
it('should parse a typescript file and return a ParserContext', () => {
16+
assert.ok(context, 'ParserContext should be returned');
1017
});
1118

12-
it('returns the demo tag', async () => {
13-
const docGen = new DocGen();
14-
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
15-
assert.equal(result.docComment.customBlocks.length, 1);
19+
it('returns the demo tag', () => {
20+
assert.equal(context.docComment.customBlocks.length, 1);
1621
});
1722

18-
it('rendered result contains tag', async () => {
19-
const docGen = new DocGen();
20-
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
21-
const doc = docGen.renderDocs(result);
23+
it('rendered result contains tag', () => {
24+
const doc = docGen.renderDocs(context);
2225
assert.match(doc, /my-circle/);
2326
});
27+
28+
describe('getters', () => {
29+
30+
it('returns correct path for default tag', () => {
31+
assert.equal(docGen.src, './dist/my-circle.js');
32+
});
33+
34+
it('returns correct path for custom tag', () => {
35+
const customDocGen = new DocGen('custom-element');
36+
assert.equal(customDocGen.src, './dist/custom-element.js');
37+
});
38+
it('returns formatted title for default tag', () => {
39+
assert.equal(docGen.title, '<my-circle> Component Demo');
40+
});
41+
42+
it('returns formatted title for custom tag', () => {
43+
const customDocGen = new DocGen('custom-element');
44+
assert.equal(customDocGen.title, '<custom-element> Component Demo');
45+
});
46+
});
2447
});

packages/banira/test/doc.gen.formatter-page.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('DocGen Formatter', () => {
1010
let formatter: FormatterDocPage;
1111

1212
before(async () => {
13-
docGen = new DocGen();
13+
docGen = new DocGen("my-circle");
1414
parsed = await docGen.parseDoc('./test/fixtures/my-circle.ts');
1515
formatter = new FormatterDocPage(parsed);
1616
})
@@ -19,12 +19,12 @@ describe('DocGen Formatter', () => {
1919
});
2020

2121
it('should create a doc page', async () => {
22-
const result = formatter.createDocPage();
22+
const result = formatter.createDocPage(docGen.tagName, docGen.src, docGen.title);
2323
assert.ok(result, 'Doc page should be created');
2424
});
2525

2626
it('contains the demo tag', async () => {
27-
const result = formatter.createDocPage();
27+
const result = formatter.createDocPage(docGen.tagName, docGen.src, docGen.title);
2828
assert.match(result, /<my-circle><\/my-circle>/);
2929
});
3030
});

0 commit comments

Comments
 (0)