Skip to content

Commit 90a908e

Browse files
authored
fix(queries): allow complex types in type linking regex (#814)
1 parent 55ecf24 commit 90a908e

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@node-core/doc-kit",
33
"type": "module",
4-
"version": "1.3.6",
4+
"version": "1.3.7",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/nodejs/doc-kit.git"

src/utils/queries/__tests__/index.test.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,61 @@ describe('QUERIES', () => {
3333
strictEqual(QUERIES.standardYamlFrontmatter.test(content), false);
3434
});
3535
});
36+
37+
describe('normalizeTypes', () => {
38+
it('matches basic types', () => {
39+
const content = '{string}';
40+
QUERIES.normalizeTypes.lastIndex = 0;
41+
ok(QUERIES.normalizeTypes.test(content));
42+
});
43+
44+
it('matches complex types with generics', () => {
45+
const content1 = '{Readonly<object>}';
46+
const content2 = '{Map<string, "ignore"|null>}';
47+
48+
QUERIES.normalizeTypes.lastIndex = 0;
49+
ok(
50+
QUERIES.normalizeTypes.test(content1),
51+
'Should match Readonly<object>'
52+
);
53+
QUERIES.normalizeTypes.lastIndex = 0;
54+
ok(QUERIES.normalizeTypes.test(content2), 'Should match Map<...>');
55+
});
56+
57+
it('matches complex union types with parentheses', () => {
58+
const content = '{(string|number)}';
59+
QUERIES.normalizeTypes.lastIndex = 0;
60+
ok(
61+
QUERIES.normalizeTypes.test(content),
62+
'Should match union with parentheses'
63+
);
64+
});
65+
});
66+
67+
describe('linksWithTypes', () => {
68+
it('matches basic type links', () => {
69+
const content = '[`<string>`](https://mdn...)';
70+
QUERIES.linksWithTypes.lastIndex = 0;
71+
ok(QUERIES.linksWithTypes.test(content));
72+
});
73+
74+
it('matches complex type links with generics', () => {
75+
const content1 =
76+
'[`<Readonly>`](https://mdn...)<[`<object>`](https://mdn...)>';
77+
QUERIES.linksWithTypes.lastIndex = 0;
78+
ok(
79+
QUERIES.linksWithTypes.test(content1),
80+
'Should match generic type link'
81+
);
82+
});
83+
84+
it('matches complex type links with unions', () => {
85+
const content2 =
86+
'<([`<string>`](https://mdn...)|[`<number>`](https://mdn...))>';
87+
QUERIES.linksWithTypes.lastIndex = 0;
88+
ok(QUERIES.linksWithTypes.test(content2), 'Should match union type link');
89+
});
90+
});
3691
});
3792

3893
describe('UNIST', () => {

src/utils/queries/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export const QUERIES = {
88
// Fixes the references to Markdown pages into the API documentation
99
markdownUrl: /^(?![+a-zA-Z]+:)([^#?]+)\.md(#.+)?$/,
1010
// ReGeX to match the {Type}<Type> (API type references)
11-
normalizeTypes: /(\{|<)(?! )[^<({})>]+(?! )(\}|>)/g,
11+
normalizeTypes: /(\{|<)(?! )[^{}]+(?! )(\}|>)/g,
1212
// ReGex to match the type API type references that got already parsed
1313
// so that they can be transformed into HTML links
14-
linksWithTypes: /\[`<[^<({})>]+>`\]\((\S+)\)/g,
14+
linksWithTypes: /\[`<[^{}]+>`\]\((\S+)\)/g,
1515
// ReGeX for handling Stability Indexes Metadata
1616
stabilityIndex: /^Stability: ([0-5](?:\.[0-3])?)(?:\s*-\s*)?(.*)$/s,
1717
// ReGeX for handling the Stability Index Prefix

0 commit comments

Comments
 (0)