Skip to content

Commit f03a453

Browse files
authored
Merge pull request #33 from tsedio/fix-generate-id-per-symbol
fix: add id to generate file name
2 parents 84d9702 + ebd1698 commit f03a453

File tree

18 files changed

+123
-71
lines changed

18 files changed

+123
-71
lines changed

packages/ts-doc/components/code-highlight/code-highlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {stripsComments} = require("../../src/utils/strips");
2-
const {highlight} = require("../../src/highlight");
2+
const {highlight} = require("./highlight");
33

44
module.exports = {
55
name: "codeHighlight",

packages/ts-doc/src/highlight/index.js renamed to packages/ts-doc/components/code-highlight/highlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const {context} = require("../context");
3+
const {context} = require("../../src/context");
44
const KEYWORDS =
55
/(\benum\b|\bstatic\b|\bclass\b|\binterface\b|\bprivate\b|\bpublic\b|\bconst\b|\blet\b|\bprotected\b|\bimplements\b|\bconstructor\b|\breadonly\b|\babstract\b|\bimport\b|\bexport\b|\bas\b|\bfrom\b|\bextends\b)/g;
66
const TYPES = /(\bany\b|\bstring\b|\bboolean\b|\bnumber\b|\bDate\b|\bvoid\b)/g;

packages/ts-doc/src/highlight/index.spec.js renamed to packages/ts-doc/components/code-highlight/highlight.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {expect} = require("chai");
2-
const {highlight} = require("./index");
2+
const {highlight} = require("./highlight");
33

44
describe("Highlight", () => {
55
it("should highlight content", () => {

packages/ts-doc/components/symbol-params/symbol-params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {bindSymbols} = require("../../src/highlight");
1+
const {bindSymbols} = require("../code-highlight/highlight");
22
module.exports = {
33
name: "symbolParams",
44
trim: false,

packages/ts-doc/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
"fs-extra": "8.1.0",
3535
"glob": "7.1.6",
3636
"globby": "8.0.2",
37+
"lodash": "^4.17.21",
3738
"listr": "^0.14.3",
3839
"normalize-path": "3.0.0",
3940
"read-pkg-up": "7.0.0"
4041
},
4142
"devDependencies": {},
4243
"peerDependencies": {}
43-
}
44+
}

packages/ts-doc/src/models/DocSymbol.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require("path");
22
const normalizePath = require("normalize-path");
33
const {context} = require("../context");
44
const {descriptionParser} = require("../parsers/description-parser.js");
5+
const {dashCase} = require("../utils/dashCase");
56

67
const _filterParams = (labels) => {
78
return labels
@@ -45,6 +46,10 @@ class DocSymbol {
4546
this.labels = [];
4647
}
4748

49+
get id() {
50+
return dashCase([this.symbolType, this.symbolName].filter(Boolean).join("-"));
51+
}
52+
4853
get path() {
4954
return this.docFile.path;
5055
}
@@ -112,11 +117,7 @@ class DocSymbol {
112117
* @returns {*}
113118
*/
114119
get url() {
115-
const url = [
116-
context.baseUrl,
117-
normalizePath(path.dirname(this.docFile.relativePackagePath)), //.replace(/\.ts$/, '')
118-
`${this.symbolName}.html`
119-
].join("/");
120+
const url = [context.baseUrl, normalizePath(path.dirname(this.docFile.relativePackagePath)), `${this.id}.html`].join("/");
120121

121122
return context.outputResolver(url);
122123
}
@@ -126,7 +127,7 @@ class DocSymbol {
126127
* @returns {*}
127128
*/
128129
get outputPath() {
129-
const file = normalizePath(path.join(context.outputDir, path.dirname(this.docFile.relativePackagePath), `${this.symbolName}.md`));
130+
const file = normalizePath(path.join(context.outputDir, path.dirname(this.docFile.relativePackagePath), `${this.id}.md`));
130131

131132
return context.outputResolver(file);
132133
}

packages/ts-doc/src/models/DocSymbol.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("DocSymbol", () => {
2626

2727
this.docSymbol = new DocSymbol();
2828
this.docSymbol.symbolName = "SymbolName";
29+
this.docSymbol.symbolType = "Const";
2930
await this.docSymbol.setDocFile({
3031
file: path.join(process.cwd(), "/packages/common/di/lib/file.d.ts"),
3132
path: path.join(process.cwd(), "/packages/common/di/lib/file.ts"),
@@ -88,10 +89,10 @@ describe("DocSymbol", () => {
8889
});
8990

9091
it("should return the url", () => {
91-
expect(this.docSymbol.url).to.eq("/api/common/di/SymbolName.html");
92+
expect(this.docSymbol.url).to.eq("/api/common/di/const-symbol-name.html");
9293
});
9394

9495
it("should return the outputPath", () => {
95-
expect(this.docSymbol.outputPath).to.eq(path.join(process.cwd(), "/docs/api/common/di/SymbolName.md"));
96+
expect(this.docSymbol.outputPath).to.eq(path.join(process.cwd(), "/docs/api/common/di/const-symbol-name.md"));
9697
});
9798
});

packages/ts-doc/src/parsers/DocParser.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,31 @@ class DocParser {
3737
static async parse(docFile) {
3838
const {symbols} = new DocParser(docFile.contents).parse();
3939

40-
for (const [, symbol] of symbols) {
41-
try {
42-
await symbol.setDocFile(docFile);
40+
const promises = await Promise.all(
41+
[...symbols.values()].map(async (symbol) => {
42+
try {
43+
await symbol.setDocFile(docFile);
44+
45+
return symbol;
46+
} catch (er) {
47+
logger.error("Fail to process symbol", {symbol, error: er});
48+
}
49+
})
50+
);
51+
52+
const parsedSymbols = await Promise.all(promises);
53+
54+
for (const symbol of parsedSymbols) {
55+
if (symbol) {
4356
const newSymbol = context.symbols.push(symbol);
57+
4458
docFile.symbols.set(newSymbol.symbolName, newSymbol);
45-
} catch (er) {
46-
logger.error("Fail to process symbol", {symbol, error: er});
4759
}
4860
}
4961

5062
return docFile.symbols;
5163
}
5264

53-
// /**
54-
// *
55-
// * @param str
56-
// * @returns {string}
57-
// */
58-
// overview (str = '') {
59-
// return stripsTags(stripsComments(str))
60-
// .split('\n')
61-
// .filter(o => !!o.trim())
62-
// .join('\n')
63-
// .trim()
64-
// }
65-
6665
/**
6766
*
6867
*/

packages/ts-doc/src/scan/scan.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@ module.exports = {
5858
let symbolsSize = 0;
5959
const files = await globby(patterns);
6060

61-
for (const file of files) {
62-
try {
63-
const symbols = await DocParser.parse(new DocFile(file));
61+
// paginate files by 5
62+
for (let i = 0; i < files.length; i += 5) {
63+
const filesChunk = files.slice(i, i + 5);
6464

65-
symbols.forEach((symbol) => {
66-
context.logger(`Scanned symbol '${chalk.cyan(symbol.symbolName)}'`);
67-
symbolsSize++;
68-
});
69-
} catch (er) {
70-
context.logger.error(chalk.red(er), er.stack);
71-
}
65+
await Promise.all(
66+
filesChunk.map(async (file) => {
67+
try {
68+
const symbols = await DocParser.parse(new DocFile(file));
69+
70+
symbols.forEach((symbol) => {
71+
context.logger(`Scanned symbol '${chalk.cyan(symbol.symbolName)}'`);
72+
symbolsSize++;
73+
});
74+
} catch (er) {
75+
context.logger.error(chalk.red(er), er.stack);
76+
}
77+
})
78+
);
7279
}
7380

7481
context.logger(`${chalk.green(symbolsSize)} scanned symbols`);

packages/ts-doc/src/tasks/build-api.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ module.exports = {
1515
.then(() => context.readPkg())
1616
.then(() => scanComponents(context.templatesDir))
1717
.then(() => scanFiles(context.scanPatterns))
18-
.then(() => {
18+
.then(async () => {
1919
let symbols = 0;
20-
context.symbols.forEach((symbol) => {
21-
const content = context.components.page(symbol);
22-
symbols++;
23-
return writeSymbol(symbol, content);
24-
});
20+
21+
await Promise.all(
22+
context.symbols.toArray().map((symbol) => {
23+
const content = context.components.page(symbol);
24+
symbols++;
25+
return writeSymbol(symbol, content);
26+
})
27+
);
28+
2529
logger(chalk.green(symbols) + " symbols write");
2630
})
2731
.then(() => writeJson())

0 commit comments

Comments
 (0)