Skip to content

Commit

Permalink
feat(dgeni,docs-utils): centralize paths for different doc kinds (#2950)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Aug 1, 2024
1 parent c02a02e commit d907c13
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
6 changes: 6 additions & 0 deletions libs/docs-utils/src/kind/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum DaffDocKind {
GUIDE = 'GUIDE',
EXPLANATION = 'EXPLANATION',
PACKAGE = 'PACKAGE',
API = 'API',
}
8 changes: 8 additions & 0 deletions libs/docs-utils/src/kind/path-segment-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DaffDocKind } from './enum';

export const DAFF_DOC_KIND_PATH_SEGMENT_MAP = <const>{
[DaffDocKind.GUIDE]: 'guides',
[DaffDocKind.EXPLANATION]: 'explanations',
[DaffDocKind.PACKAGE]: 'packages',
[DaffDocKind.API]: 'api',
};
2 changes: 2 additions & 0 deletions libs/docs-utils/src/kind/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './enum';
export * from './path-segment-map';
1 change: 1 addition & 0 deletions libs/docs-utils/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { crossOsFilename } from './cross-os-filename';
export * from './kind/public_api';
33 changes: 14 additions & 19 deletions tools/dgeni/src/processors/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { slugify } from 'markdown-toc';
import { marked } from 'marked';
import { markedHighlight } from 'marked-highlight';

import {
DAFF_DOC_KIND_PATH_SEGMENT_MAP,
DaffDocKind,
} from '@daffodil/docs-utils';

hljs.registerLanguage('typescript', typescript);
hljs.registerLanguage('ts', typescript);
hljs.registerLanguage('xml', xml);
Expand All @@ -20,17 +25,11 @@ hljs.registerLanguage('bash', bash);
hljs.registerLanguage('graphql', graphql);
hljs.registerLanguage('gql', graphql);

enum DocKind {
GUIDE = 'GUIDE',
EXPLANATION = 'EXPLANATION',
PACKAGE = 'PACKAGE',
API = 'API',
}
const DOC_KIND_REGEX = {
[DocKind.GUIDE]: /\/docs\/guides\/(?<path>.+)\.md/,
[DocKind.EXPLANATION]: /\/docs\/explanations\/(?<path>.+)\.md/,
[DocKind.PACKAGE]: /\/libs\/(?<path>.+)\.md/,
[DocKind.API]: /\/libs\/(?<path>.+)\.ts/,
[DaffDocKind.GUIDE]: /\/docs\/guides\/(?<path>.+)\.md/,
[DaffDocKind.EXPLANATION]: /\/docs\/explanations\/(?<path>.+)\.md/,
[DaffDocKind.PACKAGE]: /\/libs\/(?<path>.+)\.md/,
[DaffDocKind.API]: /\/libs\/(?<path>.+)\.ts/,
};
const getLinkUrl = (path: string): string => {
const kind = (<Array<keyof typeof DOC_KIND_REGEX>>Object.keys(DOC_KIND_REGEX)).find((k) => DOC_KIND_REGEX[k].test(path));
Expand All @@ -41,18 +40,14 @@ const getLinkUrl = (path: string): string => {
}

switch (kind) {
case DocKind.GUIDE:
return `/docs/guides/${match.groups.path}`;
case DaffDocKind.GUIDE:
case DaffDocKind.EXPLANATION:
case DaffDocKind.API:
return `/docs/${DAFF_DOC_KIND_PATH_SEGMENT_MAP[kind]}/${match.groups.path}`;

case DocKind.EXPLANATION:
return `/docs/explanations/${match.groups.path}`;

case DocKind.PACKAGE:
case DaffDocKind.PACKAGE:
return `/docs/packages/${match.groups.path}`.replaceAll(/\/(?:readme|src|guides)/gi, '');

case DocKind.API:
return `/docs/api/${match.groups.path}`;

default:
return path;
}
Expand Down

0 comments on commit d907c13

Please sign in to comment.