Skip to content

Commit d273ee1

Browse files
Copilotmbohal
andcommitted
Address all new feedback: add types, make TSDoc a link, use dynamic element, add warning for unprocessed elements
Co-authored-by: mbohal <4589176+mbohal@users.noreply.github.com>
1 parent 555ccc7 commit d273ee1

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"webpack-dev-server": "^5.2.0"
7171
},
7272
"scripts": {
73-
"build": "webpack --mode=production && node scripts/processDocoffFunctionDoc.js",
73+
"build": "webpack --mode=production",
7474
"prepublishOnly": "npm run build",
7575
"start": "webpack serve --mode=development",
7676
"test": "npm run test:eslint && npm run test:jest",

public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ <h3>Layout</h3>
239239

240240
<h2><code>docoff-function-doc</code></h2>
241241

242-
<p><code>&ltdocoff-function-doc&gt;</code> displays function documentation extracted from TSDoc comments in TypeScript files.</p>
242+
<p><code>&ltdocoff-function-doc&gt;</code> displays function documentation extracted from <a href="https://tsdoc.org/" target="_blank">TSDoc</a> comments in TypeScript files.</p>
243243

244244
<h3>Usage</h3>
245245

@@ -252,7 +252,7 @@ <h3>Example</h3>
252252

253253
<p>Displaying documentation for example functions:</p>
254254

255-
<dl><dt><strong>formatName</strong></dt><dd>Formats a user's full name with proper capitalization</dd><dt>Parameter: <code>firstName</code></dt><dd>The user's first name</dd><dt>Parameter: <code>lastName</code></dt><dd>The user's last name</dd><dt>Parameter: <code>includeTitle</code></dt><dd>Whether to include a title prefix</dd><dt>Returns:</dt><dd>The formatted full name</dd></dl>
255+
<dl><dt><strong>formatName</strong></dt><dd>Formats a user's full name with proper capitalization</dd><dt>Parameter: <code>firstName</code>: <span style="color: #0066cc;">string</span></dt><dd>The user's first name</dd><dt>Parameter: <code>lastName</code>: <span style="color: #0066cc;">string</span></dt><dd>The user's last name</dd><dt>Parameter: <code>includeTitle</code>: <span style="color: #0066cc;">boolean</span> (optional)</dt><dd>Whether to include a title prefix</dd><dt>Returns: <span style="color: #009900;">string</span></dt><dd>The formatted full name</dd></dl>
256256

257257
</div>
258258

scripts/processDocoffFunctionDoc.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ async function generateFunctionDoc(filePath, functionName, baseDir) {
9898
throw new Error(`No TSDoc comment found for function '${functionName}'`);
9999
}
100100

101+
// Extract type information from the function
102+
const typeInfo = extractTypeInformation(functionNode);
103+
101104
// Parse TSDoc comment
102105
const parser = new TSDocParser();
103106
const parserContext = parser.parseString(tsdocComment);
@@ -107,7 +110,7 @@ async function generateFunctionDoc(filePath, functionName, baseDir) {
107110
}
108111

109112
// Generate HTML from parsed TSDoc
110-
return generateHTMLFromTSDoc(functionName, parserContext.docComment);
113+
return generateHTMLFromTSDoc(functionName, parserContext.docComment, typeInfo);
111114
}
112115

113116
function findFunctionNode(sourceFile, functionName) {
@@ -155,7 +158,41 @@ function extractTSDocComment(sourceFile, functionNode) {
155158
return null;
156159
}
157160

158-
function generateHTMLFromTSDoc(functionName, docComment) {
161+
function extractTypeInformation(functionNode) {
162+
const typeInfo = {
163+
parameters: [],
164+
returnType: null
165+
};
166+
167+
// Extract parameter types
168+
if (functionNode.parameters) {
169+
for (const param of functionNode.parameters) {
170+
const paramName = param.name.text;
171+
let paramType = 'any';
172+
173+
if (param.type) {
174+
paramType = param.type.getText();
175+
}
176+
177+
const isOptional = param.questionToken !== undefined || param.initializer !== undefined;
178+
179+
typeInfo.parameters.push({
180+
name: paramName,
181+
type: paramType,
182+
optional: isOptional
183+
});
184+
}
185+
}
186+
187+
// Extract return type
188+
if (functionNode.type) {
189+
typeInfo.returnType = functionNode.type.getText();
190+
}
191+
192+
return typeInfo;
193+
}
194+
195+
function generateHTMLFromTSDoc(functionName, docComment, typeInfo) {
159196
const summary = docComment.summarySection;
160197
const params = docComment.params;
161198
const returnsBlock = docComment.returnsBlock;
@@ -175,15 +212,22 @@ function generateHTMLFromTSDoc(functionName, docComment) {
175212
for (const param of params.blocks) {
176213
const paramName = param.parameterName;
177214
const paramDescription = param.content ? extractTextFromNodes(param.content.nodes) : '';
178-
html += `<dt>Parameter: <code>${paramName}</code></dt>`;
215+
216+
// Find type information for this parameter
217+
const typeInfoParam = typeInfo.parameters.find(p => p.name === paramName);
218+
const typeDisplay = typeInfoParam ? `<span style="color: #0066cc;">${typeInfoParam.type}</span>` : '';
219+
const optionalDisplay = typeInfoParam && typeInfoParam.optional ? ' (optional)' : '';
220+
221+
html += `<dt>Parameter: <code>${paramName}</code>${typeDisplay ? `: ${typeDisplay}` : ''}${optionalDisplay}</dt>`;
179222
html += `<dd>${paramDescription}</dd>`;
180223
}
181224
}
182225

183226
// Returns
184227
if (returnsBlock && returnsBlock.content) {
185228
const returnDescription = extractTextFromNodes(returnsBlock.content.nodes);
186-
html += `<dt>Returns:</dt>`;
229+
const returnTypeDisplay = typeInfo.returnType ? `<span style="color: #009900;">${typeInfo.returnType}</span>` : '';
230+
html += `<dt>Returns${returnTypeDisplay ? `: ${returnTypeDisplay}` : ''}</dt>`;
187231
html += `<dd>${returnDescription}</dd>`;
188232
}
189233

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Custom element for displaying function documentation
3+
* Shows a warning when not processed by the webpack plugin
4+
*/
5+
export class DocoffFunctionDoc extends HTMLElement {
6+
static get observedAttributes() {
7+
return ['src'];
8+
}
9+
10+
connectedCallback() {
11+
this.render();
12+
}
13+
14+
attributeChangedCallback() {
15+
this.render();
16+
}
17+
18+
get src() {
19+
return this.getAttribute('src');
20+
}
21+
22+
render() {
23+
if (!this.src) {
24+
this.innerHTML = '<div style="color: red;">Error: src attribute is required</div>';
25+
return;
26+
}
27+
28+
// If this element is still present, it means the webpack plugin didn't process it
29+
this.innerHTML = `
30+
<div style="background: #fff3cd; border: 1px solid #ffeaa7; padding: 10px; border-radius: 4px; color: #856404;">
31+
<strong>Warning:</strong> This file needs to be processed through the build system first.
32+
<br>Function documentation for <code>${this.src}</code> will be generated during build.
33+
</div>
34+
`;
35+
}
36+
}

src/DocoffFunctionDoc/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { DocoffFunctionDoc } from './DocoffFunctionDoc';

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { DocoffPlaceholder } from './DocoffPlaceholder';
22
import { DocoffReactPreview } from './DocoffReactPreview';
33
import { DocoffReactBase } from './DocoffReactBase';
44
import { DocoffReactProps } from './DocoffReactProps';
5+
import { DocoffFunctionDoc } from './DocoffFunctionDoc';
56

67
customElements.define('docoff-react-preview', DocoffReactPreview, { extends: 'textarea' });
78
customElements.define('docoff-react-base', DocoffReactBase, { extends: 'textarea' });
89
customElements.define('docoff-react-props', DocoffReactProps);
910
customElements.define('docoff-placeholder', DocoffPlaceholder);
11+
customElements.define('docoff-function-doc', DocoffFunctionDoc);
1012

1113
// For comfortable usage in Markdown any `<code>` elements with class `language-docoff-*`
1214
// get replaced by the respective custom elements

0 commit comments

Comments
 (0)