@graphql-markdown/printer-legacy 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "bugs": {
6
6
  "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
7
  },
8
- "version": "1.2.2",
8
+ "version": "1.3.0",
9
9
  "license": "MIT",
10
10
  "main": "src/index.js",
11
11
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "stryker": "stryker run"
23
23
  },
24
24
  "dependencies": {
25
- "@graphql-markdown/utils": "^1.2.1"
25
+ "@graphql-markdown/utils": "^1.4.0"
26
26
  },
27
27
  "directories": {
28
28
  "test": "tests"
package/src/badge.js CHANGED
@@ -1,12 +1,18 @@
1
1
  const {
2
2
  object: { hasProperty },
3
- graphql: { isNonNullType, isListType, isDeprecated, getNamedType },
3
+ graphql: {
4
+ isNonNullType,
5
+ isListType,
6
+ isDeprecated,
7
+ getNamedType,
8
+ getConstDirectiveMap,
9
+ },
4
10
  } = require("@graphql-markdown/utils");
5
11
 
6
12
  const { getLinkCategory } = require("./link");
7
13
  const { getGroup } = require("./group");
8
14
 
9
- const getTypeBadges = (type) => {
15
+ const getTypeBadges = (type, options) => {
10
16
  const rootType = hasProperty(type, "type") ? type.type : type;
11
17
 
12
18
  const badges = [];
@@ -33,6 +39,11 @@ const getTypeBadges = (type) => {
33
39
  badges.push(group);
34
40
  }
35
41
 
42
+ const constDirectiveMap = getConstDirectiveMap(type, options);
43
+ if (typeof constDirectiveMap !== "undefined") {
44
+ badges.push(...Object.keys(constDirectiveMap).map((badge) => `@${badge}`));
45
+ }
46
+
36
47
  return badges;
37
48
  };
38
49
 
@@ -41,7 +52,7 @@ const printBadges = (type, options) => {
41
52
  return "";
42
53
  }
43
54
 
44
- const badges = getTypeBadges(type);
55
+ const badges = getTypeBadges(type, options);
45
56
 
46
57
  if (badges.length === 0) {
47
58
  return "";
package/src/common.js CHANGED
@@ -1,9 +1,11 @@
1
1
  const {
2
2
  string: { escapeMDX },
3
- object: { hasProperty },
4
- graphql: { isDeprecated },
3
+ object: { hasProperty, isEmpty },
4
+ graphql: { isDeprecated, getConstDirectiveMap },
5
5
  } = require("@graphql-markdown/utils");
6
6
 
7
+ const { getCustomDirectiveDescription } = require("./directive");
8
+
7
9
  const { MARKDOWN_EOP, NO_DESCRIPTION_TEXT } = require("./const/strings");
8
10
 
9
11
  const printDeprecation = (type) => {
@@ -19,17 +21,35 @@ const printDeprecation = (type) => {
19
21
  return `<Badge class="warning" text="DEPRECATED${reason}"/>${MARKDOWN_EOP}`;
20
22
  };
21
23
 
22
- const printDescription = (type, noText = NO_DESCRIPTION_TEXT) => {
24
+ const printCustomDirectives = (type, options) => {
25
+ const constDirectiveMap = getConstDirectiveMap(type, options);
26
+
27
+ if (typeof constDirectiveMap !== "object" || isEmpty(constDirectiveMap)) {
28
+ return "";
29
+ }
30
+
31
+ const content = Object.values(constDirectiveMap)
32
+ .map((constDirectiveOption) =>
33
+ getCustomDirectiveDescription(type, constDirectiveOption),
34
+ )
35
+ .join(MARKDOWN_EOP);
36
+
37
+ return `${content}${MARKDOWN_EOP}`;
38
+ };
39
+
40
+ const printDescription = (type, options, noText = NO_DESCRIPTION_TEXT) => {
23
41
  const replacement = typeof noText === "string" ? noText : NO_DESCRIPTION_TEXT;
24
42
  const description =
25
43
  hasProperty(type, "description") && typeof type.description === "string"
26
44
  ? escapeMDX(type.description)
27
45
  : replacement;
28
46
  const deprecation = printDeprecation(type);
29
- return `${deprecation}${description}`;
47
+ const customDirectives = printCustomDirectives(type, options);
48
+ return `${deprecation}${customDirectives}${description}`;
30
49
  };
31
50
 
32
51
  module.exports = {
33
52
  printDeprecation,
34
53
  printDescription,
54
+ printCustomDirectives,
35
55
  };
@@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = {
13
13
  typeBadges: true,
14
14
  skipDocDirective: undefined,
15
15
  printDeprecated: OPTION_DEPRECATED.DEFAULT,
16
+ customDirectives: {},
16
17
  };
17
18
 
18
19
  module.exports = {
@@ -0,0 +1,58 @@
1
+ const {
2
+ graphql: { getConstDirectiveMap },
3
+ object: { hasProperty },
4
+ } = require("@graphql-markdown/utils");
5
+ const {
6
+ HEADER_SECTION_LEVEL,
7
+ HEADER_SECTION_SUB_LEVEL,
8
+ MARKDOWN_EOP,
9
+ MARKDOWN_EOL,
10
+ } = require("./const/strings");
11
+ const { printLink } = require("./link");
12
+
13
+ function printCustomDirectives(type, options) {
14
+ const constDirectiveMap = getConstDirectiveMap(type, options);
15
+
16
+ if (
17
+ typeof constDirectiveMap !== "object" ||
18
+ !Object.keys(constDirectiveMap).length
19
+ ) {
20
+ return "";
21
+ }
22
+
23
+ const content = Object.values(constDirectiveMap)
24
+ .map((constDirectiveOption) =>
25
+ printCustomDirective(type, constDirectiveOption, options),
26
+ )
27
+ .join(MARKDOWN_EOP);
28
+
29
+ return `${HEADER_SECTION_LEVEL} Directives${MARKDOWN_EOP}${content}${MARKDOWN_EOP}`;
30
+ }
31
+
32
+ function printCustomDirective(type, constDirectiveOption, options) {
33
+ const typeNameLink = printLink(constDirectiveOption.type, {
34
+ ...options,
35
+ withAttributes: false,
36
+ });
37
+ const description = getCustomDirectiveDescription(type, constDirectiveOption);
38
+
39
+ return `${HEADER_SECTION_SUB_LEVEL} ${typeNameLink}${MARKDOWN_EOL}> ${description}${MARKDOWN_EOL}> `;
40
+ }
41
+
42
+ function getCustomDirectiveDescription(type, constDirectiveOption) {
43
+ if (
44
+ typeof constDirectiveOption === "undefined" ||
45
+ !hasProperty(constDirectiveOption, "descriptor") ||
46
+ typeof constDirectiveOption.descriptor !== "function"
47
+ ) {
48
+ return "";
49
+ }
50
+
51
+ return constDirectiveOption.descriptor(constDirectiveOption.type, type);
52
+ }
53
+
54
+ module.exports = {
55
+ printCustomDirectives,
56
+ printCustomDirective,
57
+ getCustomDirectiveDescription,
58
+ };
package/src/group.js CHANGED
@@ -4,13 +4,16 @@ const {
4
4
  graphql: { getNamedType },
5
5
  } = require("@graphql-markdown/utils");
6
6
 
7
- const getGroup = (type, groups) => {
7
+ const getGroup = (type, groups, typeCategory) => {
8
8
  if (typeof groups !== "object" || groups === null) {
9
9
  return "";
10
10
  }
11
11
  const graphLQLNamedType = getNamedType(type);
12
12
  const typeName = graphLQLNamedType.name || graphLQLNamedType;
13
- return hasProperty(groups, typeName) ? toSlug(groups[typeName]) : "";
13
+ return hasProperty(groups, typeCategory) &&
14
+ hasProperty(groups[typeCategory], typeName)
15
+ ? toSlug(groups[typeCategory][typeName])
16
+ : "";
14
17
  };
15
18
 
16
19
  module.exports = {
package/src/link.js CHANGED
@@ -92,7 +92,7 @@ class Link {
92
92
  ? DEPRECATED
93
93
  : "";
94
94
  const group = hasProperty(options, "groups")
95
- ? getGroup(type, options.groups)
95
+ ? getGroup(type, options.groups, category.plural)
96
96
  : "";
97
97
  const url = pathUrl.join(
98
98
  options.basePath,
package/src/printer.js CHANGED
@@ -19,6 +19,7 @@ const {
19
19
 
20
20
  const { printRelations } = require("./relation");
21
21
  const { printDescription } = require("./common");
22
+ const { printCustomDirectives } = require("./directive");
22
23
  const {
23
24
  printOperationMetadata,
24
25
  printCodeOperation,
@@ -55,10 +56,11 @@ class Printer {
55
56
  schema,
56
57
  baseURL,
57
58
  linkRoot = "/",
58
- { groups, printTypeOptions, skipDocDirective } = {
59
+ { groups, printTypeOptions, skipDocDirective, customDirectives } = {
59
60
  groups: undefined,
60
61
  printTypeOptions: undefined,
61
62
  skipDocDirective: undefined,
63
+ customDirectives: undefined,
62
64
  },
63
65
  ) => {
64
66
  if (typeof Printer.options !== "undefined" && Printer.options !== null) {
@@ -76,6 +78,7 @@ class Printer {
76
78
  skipDocDirective: skipDocDirective ?? undefined,
77
79
  printDeprecated:
78
80
  printTypeOptions?.deprecated ?? OPTION_DEPRECATED.DEFAULT,
81
+ customDirectives,
79
82
  };
80
83
  };
81
84
 
@@ -138,6 +141,8 @@ class Printer {
138
141
  return MARKDOWN_SOC + code.trim() + MARKDOWN_EOC;
139
142
  };
140
143
 
144
+ static printCustomDirectives = printCustomDirectives;
145
+
141
146
  static printTypeMetadata = (type, options) => {
142
147
  let metadata;
143
148
 
@@ -193,12 +198,22 @@ class Printer {
193
198
  });
194
199
  const description = Printer.printDescription(type);
195
200
  const code = Printer.printCode(type, printTypeOptions);
201
+ const customDirectives = Printer.printCustomDirectives(
202
+ type,
203
+ printTypeOptions,
204
+ );
196
205
  const metadata = Printer.printTypeMetadata(type, printTypeOptions);
197
206
  const relations = Printer.printRelations(type, printTypeOptions);
198
207
 
199
- return [header, mdx, description, code, metadata, relations].join(
200
- MARKDOWN_EOP,
201
- );
208
+ return [
209
+ header,
210
+ mdx,
211
+ description,
212
+ code,
213
+ customDirectives,
214
+ metadata,
215
+ relations,
216
+ ].join(MARKDOWN_EOP);
202
217
  };
203
218
  }
204
219
 
package/src/section.js CHANGED
@@ -152,7 +152,7 @@ const printSectionItem = (type, options) => {
152
152
  }
153
153
 
154
154
  const typeNameLink = printLink(type, { ...options, withAttributes: false });
155
- const description = printDescription(type, "").replaceAll(
155
+ const description = printDescription(type, options, "").replaceAll(
156
156
  "\n",
157
157
  `${MARKDOWN_EOL}> `,
158
158
  );