Skip to content

Commit 1444056

Browse files
committed
descriptions
Signed-off-by: flakey5 <[email protected]>
1 parent 0fa285d commit 1444056

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"html-minifier-terser": "^7.2.0",
3636
"rehype-stringify": "^10.0.1",
3737
"remark-gfm": "^4.0.0",
38+
"remark-html": "^16.0.1",
3839
"remark-parse": "^11.0.0",
3940
"remark-rehype": "^11.1.1",
4041
"remark-stringify": "^11.0.0",

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { unified } from 'unified';
2+
import html from 'remark-html';
13
import {
24
DEFAULT_EXPRESSION,
35
LEADING_HYPHEN,
@@ -160,7 +162,6 @@ function parseSignature(textRaw, values) {
160162
// We have a default value, save it and then cut it off of the signature
161163
defaultValue = declaredParameter.substring(equalSignPos, 1).trim();
162164
declaredParameter = declaredParameter.substring(0, equalSignPos);
163-
console.log('eq', declaredParameter);
164165
}
165166

166167
let parameter = rawParameters[i];
@@ -188,7 +189,7 @@ function parseSignature(textRaw, values) {
188189
}
189190

190191
if (!parameter) {
191-
// Couldn't find the shared one, I have no idea what this case is but we'll see
192+
// Couldn't find the shared one
192193
if (declaredParameter.startsWith('...')) {
193194
parameter = { name: declaredParameter };
194195
} else {
@@ -221,10 +222,13 @@ function textJoin(nodes) {
221222
return nodes
222223
.map(node => {
223224
switch (node.type) {
224-
case `strong`:
225+
case 'strong':
225226
return `**${textJoin(node.children)}**`;
226-
case `emphasis`:
227+
case 'emphasis':
227228
return `_${textJoin(node.children)}_`;
229+
case 'link': {
230+
return `[${node.label}][]`;
231+
}
228232
default:
229233
if (node.children) {
230234
return textJoin(node.children);
@@ -249,7 +253,9 @@ function parseListItem(child) {
249253

250254
current.textRaw = textJoin(
251255
child.children.filter(node => node.type !== 'list')
252-
);
256+
)
257+
.replace(/\s+/g, ' ')
258+
.replace(/<!--.*?-->/gs, '');
253259

254260
if (!current.textRaw) {
255261
throw new Error(`empty list item: ${JSON.stringify(child)}`);
@@ -260,20 +266,25 @@ function parseListItem(child) {
260266
// Extract name
261267
if (RETURN_EXPRESSION.test(text)) {
262268
current.name = 'return';
269+
270+
let [, returnType] = text.match(/`(.*?)`/);
271+
returnType = returnType.substring(1, returnType.length - 1);
272+
current.type = returnType;
273+
263274
text = text.replace(RETURN_EXPRESSION, '');
264275
} else {
265276
const [, name] = text.match(NAME_EXPRESSION) || [];
266277
if (name) {
267278
current.name = name;
268279
text = text.replace(NAME_EXPRESSION, '');
269280
}
270-
}
271281

272-
// Extract type (if provided)
273-
const [, type] = text.match(TYPE_EXPRESSION) || [];
274-
if (type) {
275-
current.type = type;
276-
text = text.replace(TYPE_EXPRESSION, '');
282+
// Extract type (if provided)
283+
const [, type] = text.match(TYPE_EXPRESSION) || [];
284+
if (type) {
285+
current.type = type;
286+
text = text.replace(TYPE_EXPRESSION, '');
287+
}
277288
}
278289

279290
// Remove leading hyphens
@@ -350,6 +361,7 @@ function handleEntry(entry, parentSection) {
350361
if (stability) {
351362
section.stability = parseInt(stability[1], 10);
352363
section.stabilityText = stability[2].replaceAll('\n', ' ').trim();
364+
353365
delete nodes[i];
354366
needsCompressing = true;
355367
}
@@ -433,7 +445,20 @@ function handleEntry(entry, parentSection) {
433445
section.shortDesc = section.desc;
434446
}
435447

436-
// TODO parse definitoons
448+
// Render the description as if it was html
449+
section.desc = unified()
450+
.use(function () {
451+
this.Parser = () => ({ type: 'root', children: nodes });
452+
})
453+
.use(html, { sanitize: false })
454+
.processSync('')
455+
.toString()
456+
.trim();
457+
458+
if (!section.desc) {
459+
// Rendering returned nothing
460+
delete section.desc;
461+
}
437462
};
438463

439464
/**

0 commit comments

Comments
 (0)