Skip to content

Commit db769a3

Browse files
authored
feat: urlFragment for methods/properties/events (#43)
1 parent fa9bfe7 commit db769a3

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/ParsedDocumentation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export declare type DocumentationBlock = {
5454
name: string;
5555
description: string;
5656
additionalTags: DocumentationTag[];
57+
urlFragment?: string;
5758
};
5859
export declare type MethodDocumentationBlock = DocumentationBlock & {
5960
signature: string;

src/__tests__/markdown-helpers.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
findFirstHeading,
1414
consumeTypedKeysList,
1515
findProcess,
16+
slugifyHeading,
1617
} from '../markdown-helpers';
1718
import { DocumentationTag } from '../ParsedDocumentation';
1819

@@ -539,4 +540,13 @@ foo`),
539540
expect(proc.renderer).toEqual(true);
540541
});
541542
});
543+
544+
describe('slugifyHeading', () => {
545+
it('should correctly slugify a complex heading', () => {
546+
const heading =
547+
'`systemPreferences.isHighContrastColorScheme()` _macOS_ _Windows_ _Deprecated_';
548+
const slugified = 'systempreferencesishighcontrastcolorscheme-macos-windows-deprecated';
549+
expect(slugifyHeading(heading)).toBe(slugified);
550+
});
551+
});
542552
});

src/block-parsers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
findContentAfterHeadingClose,
1414
StripReturnTypeBehavior,
1515
consumeTypedKeysList,
16+
slugifyHeading,
1617
} from './markdown-helpers';
1718
import {
1819
MethodDocumentationBlock,
@@ -130,6 +131,7 @@ export const _headingToMethodBlock = (
130131
parameters,
131132
returns: parsedReturnType,
132133
additionalTags: parseHeadingTags(headingTags),
134+
urlFragment: `#${slugifyHeading(heading.heading)}`,
133135
};
134136
};
135137

@@ -159,6 +161,7 @@ export const _headingToPropertyBlock = (heading: HeadingContent): PropertyDocume
159161
description: parsedDescription,
160162
required: !/\(optional\)/i.test(parsedDescription),
161163
additionalTags: parseHeadingTags(headingTags),
164+
urlFragment: `#${slugifyHeading(heading.heading)}`,
162165
...parsedReturnType!,
163166
};
164167
};
@@ -196,6 +199,7 @@ export const _headingToEventBlock = (heading: HeadingContent): EventDocumentatio
196199
description,
197200
parameters,
198201
additionalTags: parseHeadingTags(headingTags),
202+
urlFragment: `#${slugifyHeading(heading.heading)}`,
199203
};
200204
};
201205

src/markdown-helpers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,10 @@ export const findProcess = (tokens: Token[]): ProcessBlock => {
766766
}
767767
return { main: true, renderer: true };
768768
};
769+
770+
export const slugifyHeading = (heading: string): string => {
771+
return heading
772+
.replace(/[^A-Za-z0-9 \-]/g, '')
773+
.replace(/ /g, '-')
774+
.toLowerCase();
775+
};

0 commit comments

Comments
 (0)