Skip to content

Commit 525ce39

Browse files
authored
feat(docs-utils,dgeni): extract docsgen pathing helpers (#3039)
1 parent 1798c89 commit 525ce39

File tree

4 files changed

+122
-33
lines changed

4 files changed

+122
-33
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { DaffDocKind } from './enum';
2+
import {
3+
daffDocsGetKind,
4+
daffDocsGetLinkUrl,
5+
} from './helpers';
6+
7+
describe('@daffodil/docs-utils | daffDocsGetKind', () => {
8+
describe('for a API path', () => {
9+
it('should return API kind', () => {
10+
const path = '/libs/core/sub/src/symbol.ts';
11+
const result = daffDocsGetKind(path);
12+
expect(result).toEqual(DaffDocKind.API);
13+
});
14+
});
15+
16+
describe('for a package guide path', () => {
17+
it('should return package kind', () => {
18+
const path = '/libs/core/guides/test/guide.md';
19+
const result = daffDocsGetKind(path);
20+
expect(result).toEqual(DaffDocKind.PACKAGE);
21+
});
22+
});
23+
24+
describe('for a global guide path', () => {
25+
it('should return guide kind', () => {
26+
const path = '/docs/guides/test/guide.md';
27+
const result = daffDocsGetKind(path);
28+
expect(result).toEqual(DaffDocKind.GUIDE);
29+
});
30+
});
31+
32+
describe('for a global explanation path', () => {
33+
it('should return explanation kind', () => {
34+
const path = '/docs/explanations/test/guide.md';
35+
const result = daffDocsGetKind(path);
36+
expect(result).toEqual(DaffDocKind.EXPLANATION);
37+
});
38+
});
39+
});
40+
41+
describe('@daffodil/docs-utils | daffDocsGetLinkUrl', () => {
42+
describe('for a API path', () => {
43+
it('should return an API link', () => {
44+
const path = '/libs/core/sub/src/symbol.ts';
45+
const result = daffDocsGetLinkUrl(path);
46+
expect(result).toEqual('/docs/api/core/sub/symbol');
47+
});
48+
});
49+
50+
describe('for a package guide path', () => {
51+
it('should return a package link', () => {
52+
const path = '/libs/core/guides/test/guide.md';
53+
const result = daffDocsGetLinkUrl(path);
54+
expect(result).toEqual('/docs/packages/core/test/guide');
55+
});
56+
});
57+
58+
describe('for a global guide path', () => {
59+
it('should return a guide link', () => {
60+
const path = '/docs/guides/test/guide.md';
61+
const result = daffDocsGetLinkUrl(path);
62+
expect(result).toEqual('/docs/guides/test/guide');
63+
});
64+
});
65+
66+
describe('for a global explanation path', () => {
67+
it('should return an explanation link', () => {
68+
const path = '/docs/explanations/test/guide.md';
69+
const result = daffDocsGetLinkUrl(path);
70+
expect(result).toEqual('/docs/explanations/test/guide');
71+
});
72+
});
73+
});

libs/docs-utils/src/kind/helpers.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { DaffDocKind } from './enum';
2+
import { DAFF_DOC_KIND_PATH_SEGMENT_MAP } from './path-segment-map';
3+
4+
const DOC_KIND_REGEX = {
5+
[DaffDocKind.GUIDE]: /\/docs\/guides\/(?<path>.+)\.md/,
6+
[DaffDocKind.EXPLANATION]: /\/docs\/explanations\/(?<path>.+)\.md/,
7+
[DaffDocKind.PACKAGE]: /\/libs\/(?<path>.+)\.md/,
8+
[DaffDocKind.API]: /\/libs\/(?<path>.+)\.ts/,
9+
};
10+
11+
/**
12+
* Returns the kind of document based on the passed filepath.
13+
*
14+
* @param path the file path relative to the project root.
15+
*/
16+
export const daffDocsGetKind = (path: string): string => (<Array<keyof typeof DOC_KIND_REGEX>>Object.keys(DOC_KIND_REGEX)).find((k) => DOC_KIND_REGEX[k].test(path));
17+
18+
/**
19+
* Returns the URL that links to the document referenced by the passed path.
20+
*
21+
* @param path the file path relative to the project root.
22+
*/
23+
// TODO: combine with path generation logic in the creation of docs own paths
24+
export const daffDocsGetLinkUrl = (path: string): string => {
25+
const kind = daffDocsGetKind(path);
26+
const match = DOC_KIND_REGEX[kind]?.exec(path);
27+
28+
if (!match) {
29+
return path;
30+
}
31+
32+
const matchPath = match.groups.path.replaceAll(/\/(?:readme|src|guides)/gi, '');
33+
34+
switch (kind) {
35+
case DaffDocKind.GUIDE:
36+
case DaffDocKind.EXPLANATION:
37+
case DaffDocKind.API:
38+
return `/docs/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[kind]}/${matchPath}`;
39+
40+
case DaffDocKind.PACKAGE:
41+
return `/docs/packages/${matchPath}`;
42+
43+
default:
44+
return path;
45+
}
46+
};
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './enum';
22
export * from './path-segment-map';
3+
export * from './helpers';

tools/dgeni/src/processors/markdown.ts

+2-33
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import { slugify } from 'markdown-toc';
1212
import { marked } from 'marked';
1313
import { markedHighlight } from 'marked-highlight';
1414

15-
import {
16-
DAFF_DOC_KIND_PATH_SEGMENT_MAP,
17-
DaffDocKind,
18-
} from '@daffodil/docs-utils';
15+
import { daffDocsGetLinkUrl } from '@daffodil/docs-utils';
1916

2017
import { CollectLinkableSymbolsProcessor } from './collect-linkable-symbols';
2118

@@ -27,34 +24,6 @@ hljs.registerLanguage('bash', bash);
2724
hljs.registerLanguage('graphql', graphql);
2825
hljs.registerLanguage('gql', graphql);
2926

30-
const DOC_KIND_REGEX = {
31-
[DaffDocKind.GUIDE]: /\/docs\/guides\/(?<path>.+)\.md/,
32-
[DaffDocKind.EXPLANATION]: /\/docs\/explanations\/(?<path>.+)\.md/,
33-
[DaffDocKind.PACKAGE]: /\/libs\/(?<path>.+)\.md/,
34-
[DaffDocKind.API]: /\/libs\/(?<path>.+)\.ts/,
35-
};
36-
const getLinkUrl = (path: string): string => {
37-
const kind = (<Array<keyof typeof DOC_KIND_REGEX>>Object.keys(DOC_KIND_REGEX)).find((k) => DOC_KIND_REGEX[k].test(path));
38-
const match = DOC_KIND_REGEX[kind]?.exec(path);
39-
40-
if (!match) {
41-
return path;
42-
}
43-
44-
switch (kind) {
45-
case DaffDocKind.GUIDE:
46-
case DaffDocKind.EXPLANATION:
47-
case DaffDocKind.API:
48-
return `/docs/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[kind]}/${match.groups.path}`;
49-
50-
case DaffDocKind.PACKAGE:
51-
return `/docs/packages/${match.groups.path}`.replaceAll(/\/(?:readme|src|guides)/gi, '');
52-
53-
default:
54-
return path;
55-
}
56-
};
57-
5827
// marked.use(markedMermaid);
5928
marked.use(
6029
markedHighlight({
@@ -70,7 +39,7 @@ marked.use({
7039
walkTokens: (token) => {
7140
switch (token.type) {
7241
case 'link':
73-
token.href = getLinkUrl(token.href);
42+
token.href = daffDocsGetLinkUrl(token.href);
7443
break;
7544

7645
default:

0 commit comments

Comments
 (0)