@graphql-markdown/printer-legacy 1.1.6 → 1.2.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/src/link.js ADDED
@@ -0,0 +1,178 @@
1
+ const {
2
+ string: { toSlug },
3
+ url: { pathUrl },
4
+ object: { hasProperty },
5
+ graphql: {
6
+ isEnumType,
7
+ isUnionType,
8
+ isObjectType,
9
+ isScalarType,
10
+ isOperation,
11
+ isDirectiveType,
12
+ isInterfaceType,
13
+ getNamedType,
14
+ isInputType,
15
+ hasDirective,
16
+ getTypeName,
17
+ isListType,
18
+ isNonNullType,
19
+ isLeafType,
20
+ isDeprecated,
21
+ },
22
+ } = require("@graphql-markdown/utils");
23
+
24
+ const { getGroup } = require("./group");
25
+ const { ROOT_TYPE_LOCALE, DEPRECATED } = require("./const/strings");
26
+ const { OPTION_DEPRECATED } = require("./const/options");
27
+
28
+ class Link {
29
+ static getLinkCategory = (graphLQLNamedType) => {
30
+ switch (true) {
31
+ case isEnumType(graphLQLNamedType):
32
+ return ROOT_TYPE_LOCALE.ENUM;
33
+ case isUnionType(graphLQLNamedType):
34
+ return ROOT_TYPE_LOCALE.UNION;
35
+ case isInterfaceType(graphLQLNamedType):
36
+ return ROOT_TYPE_LOCALE.INTERFACE;
37
+ case isObjectType(graphLQLNamedType):
38
+ return ROOT_TYPE_LOCALE.TYPE;
39
+ case isInputType(graphLQLNamedType):
40
+ return ROOT_TYPE_LOCALE.INPUT;
41
+ case isScalarType(graphLQLNamedType):
42
+ return ROOT_TYPE_LOCALE.SCALAR;
43
+ case isDirectiveType(graphLQLNamedType):
44
+ return ROOT_TYPE_LOCALE.DIRECTIVE;
45
+ case isOperation(graphLQLNamedType):
46
+ return ROOT_TYPE_LOCALE.OPERATION;
47
+ }
48
+ return undefined;
49
+ };
50
+
51
+ static toLink = (type, name, operation, options) => {
52
+ const fallback = {
53
+ text: name,
54
+ url: "#",
55
+ };
56
+
57
+ if (typeof type === "undefined") {
58
+ return fallback;
59
+ }
60
+
61
+ if (hasProperty(options, "skipDocDirective")) {
62
+ if (hasDirective(type, options.skipDocDirective)) {
63
+ return fallback;
64
+ }
65
+ }
66
+
67
+ const graphLQLNamedType = getNamedType(type);
68
+
69
+ let category = Link.getLinkCategory(graphLQLNamedType);
70
+
71
+ if (
72
+ typeof category === "undefined" ||
73
+ typeof graphLQLNamedType === "undefined" ||
74
+ graphLQLNamedType === null
75
+ ) {
76
+ return fallback;
77
+ }
78
+
79
+ // special case for relation map
80
+ if (category === ROOT_TYPE_LOCALE.OPERATION) {
81
+ if (typeof operation === "undefined") {
82
+ return fallback;
83
+ }
84
+ category = operation;
85
+ }
86
+
87
+ const text = graphLQLNamedType.name || graphLQLNamedType;
88
+ const deprecated =
89
+ hasProperty(options, "printDeprecated") &&
90
+ options.printDeprecated === OPTION_DEPRECATED.GROUP &&
91
+ isDeprecated(type)
92
+ ? DEPRECATED
93
+ : "";
94
+ const group = hasProperty(options, "groups")
95
+ ? getGroup(type, options.groups)
96
+ : "";
97
+ const url = pathUrl.join(
98
+ options.basePath,
99
+ deprecated,
100
+ group,
101
+ category.plural,
102
+ toSlug(text),
103
+ );
104
+
105
+ return {
106
+ text: text,
107
+ url: url,
108
+ };
109
+ };
110
+
111
+ static getRelationLink = (category, type, options) => {
112
+ if (typeof category === "undefined") {
113
+ return "";
114
+ }
115
+ const link = Link.toLink(type, type.name, category, options);
116
+ return `[\`${link.text}\`](${link.url}) <Badge class="secondary" text="${category.singular}"/>`;
117
+ };
118
+
119
+ static printParentLink = (type, options) => {
120
+ return hasProperty(type, "type")
121
+ ? `<Bullet />${Link.printLink(type.type, {
122
+ withAttributes: true,
123
+ ...options,
124
+ })}`
125
+ : "";
126
+ };
127
+
128
+ static hasOptionWithAttributes = (options) =>
129
+ hasProperty(options, "withAttributes") === true &&
130
+ options.withAttributes === true;
131
+
132
+ static hasOptionParentType = (options) =>
133
+ hasProperty(options, "parentTypePrefix") &&
134
+ options.parentTypePrefix === true &&
135
+ hasProperty(options, "parentType") &&
136
+ typeof options.parentType !== "undefined" &&
137
+ options.parentType !== null;
138
+
139
+ static printLink = (type, options) => {
140
+ let text;
141
+
142
+ const link = Link.toLink(type, getTypeName(type), undefined, options);
143
+
144
+ if (Link.hasOptionWithAttributes(options) === false) {
145
+ text =
146
+ Link.hasOptionParentType(options) === true
147
+ ? `<code style={{ fontWeight: 'normal' }}>${options.parentType}.<b>${link.text}</b></code>`
148
+ : `\`${link.text}\``;
149
+ return `[${text}](${link.url})`;
150
+ }
151
+
152
+ text = Link.printLinkAttributes(type, link.text);
153
+
154
+ return `[\`${text}\`](${link.url})`;
155
+ };
156
+
157
+ static printLinkAttributes = (type, text) => {
158
+ if (typeof type === "undefined" || type === null) {
159
+ return text ?? "";
160
+ }
161
+
162
+ if (!isLeafType(type, text) && typeof type.ofType != "undefined") {
163
+ text = Link.printLinkAttributes(type.ofType, text);
164
+ }
165
+
166
+ if (isListType(type)) {
167
+ return `[${text}]`;
168
+ }
169
+
170
+ if (isNonNullType(type)) {
171
+ return `${text}!`;
172
+ }
173
+
174
+ return text;
175
+ };
176
+ }
177
+
178
+ module.exports = Link;
package/src/printer.js ADDED
@@ -0,0 +1,205 @@
1
+ const {
2
+ graphql: {
3
+ getTypeName,
4
+ isOperation,
5
+ isEnumType,
6
+ isUnionType,
7
+ isInterfaceType,
8
+ isObjectType,
9
+ isInputType,
10
+ isScalarType,
11
+ isDirectiveType,
12
+ hasDirective,
13
+ },
14
+ } = require("@graphql-markdown/utils");
15
+
16
+ const {
17
+ url: { pathUrl },
18
+ } = require("@graphql-markdown/utils");
19
+
20
+ const { printRelations } = require("./relation");
21
+ const { printDescription } = require("./common");
22
+ const {
23
+ printOperationMetadata,
24
+ printCodeOperation,
25
+ printCodeEnum,
26
+ printEnumMetadata,
27
+ printCodeUnion,
28
+ printUnionMetadata,
29
+ printCodeInterface,
30
+ printInterfaceMetadata,
31
+ printCodeObject,
32
+ printObjectMetadata,
33
+ printCodeInput,
34
+ printInputMetadata,
35
+ printCodeScalar,
36
+ printScalarMetadata,
37
+ printCodeDirective,
38
+ printDirectiveMetadata,
39
+ } = require("./graphql");
40
+ const {
41
+ MARKDOWN_EOP,
42
+ MARKDOWN_EOL,
43
+ MARKDOWN_SOC,
44
+ MARKDOWN_EOC,
45
+ FRONT_MATTER,
46
+ } = require("./const/strings");
47
+ const mdx = require("./const/mdx");
48
+
49
+ const { DEFAULT_OPTIONS, OPTION_DEPRECATED } = require("./const/options");
50
+
51
+ class Printer {
52
+ static options;
53
+
54
+ static init = (
55
+ schema,
56
+ baseURL,
57
+ linkRoot = "/",
58
+ { groups, printTypeOptions, skipDocDirective } = {
59
+ groups: undefined,
60
+ printTypeOptions: undefined,
61
+ skipDocDirective: undefined,
62
+ },
63
+ ) => {
64
+ if (typeof Printer.options !== "undefined" && Printer.options !== null) {
65
+ return;
66
+ }
67
+
68
+ Printer.options = {
69
+ ...DEFAULT_OPTIONS,
70
+ schema,
71
+ basePath: pathUrl.join(linkRoot, baseURL),
72
+ groups,
73
+ parentTypePrefix: printTypeOptions?.parentTypePrefix ?? true,
74
+ relatedTypeSection: printTypeOptions?.relatedTypeSection ?? true,
75
+ typeBadges: printTypeOptions?.typeBadges ?? true,
76
+ skipDocDirective: skipDocDirective ?? undefined,
77
+ printDeprecated:
78
+ printTypeOptions?.deprecated ?? OPTION_DEPRECATED.DEFAULT,
79
+ };
80
+ };
81
+
82
+ static printHeader = (id, title, options) => {
83
+ const { toc, pagination } = {
84
+ toc: true,
85
+ pagination: true,
86
+ ...(options.header ?? undefined),
87
+ };
88
+ const pagination_buttons = pagination
89
+ ? []
90
+ : ["pagination_next: null", "pagination_prev: null"];
91
+
92
+ const header = [
93
+ FRONT_MATTER,
94
+ `id: ${id}`,
95
+ `title: ${title}`,
96
+ `hide_table_of_contents: ${!toc}`,
97
+ ...pagination_buttons,
98
+ FRONT_MATTER,
99
+ ];
100
+
101
+ return header.join(MARKDOWN_EOL);
102
+ };
103
+
104
+ static printDescription = printDescription;
105
+
106
+ static printCode = (type) => {
107
+ let code = "";
108
+
109
+ switch (true) {
110
+ case isOperation(type):
111
+ code += printCodeOperation(type);
112
+ break;
113
+ case isEnumType(type):
114
+ code += printCodeEnum(type);
115
+ break;
116
+ case isUnionType(type):
117
+ code += printCodeUnion(type);
118
+ break;
119
+ case isInterfaceType(type):
120
+ code += printCodeInterface(type);
121
+ break;
122
+ case isObjectType(type):
123
+ code += printCodeObject(type);
124
+ break;
125
+ case isInputType(type):
126
+ code += printCodeInput(type);
127
+ break;
128
+ case isScalarType(type):
129
+ code += printCodeScalar(type);
130
+ break;
131
+ case isDirectiveType(type):
132
+ code += printCodeDirective(type);
133
+ break;
134
+ default:
135
+ code += `"${getTypeName(type)}" not supported`;
136
+ }
137
+
138
+ return MARKDOWN_SOC + code.trim() + MARKDOWN_EOC;
139
+ };
140
+
141
+ static printTypeMetadata = (type, options) => {
142
+ let metadata;
143
+
144
+ switch (true) {
145
+ case isScalarType(type):
146
+ return printScalarMetadata(type, options);
147
+ case isEnumType(type):
148
+ return printEnumMetadata(type, options);
149
+ case isUnionType(type):
150
+ return printUnionMetadata(type, options);
151
+ case isObjectType(type):
152
+ return printObjectMetadata(type, options);
153
+ case isInterfaceType(type):
154
+ return printInterfaceMetadata(type, options);
155
+ case isInputType(type):
156
+ return printInputMetadata(type, options);
157
+ case isDirectiveType(type):
158
+ return printDirectiveMetadata(type, options);
159
+ case isOperation(type):
160
+ return printOperationMetadata(type, options);
161
+ default:
162
+ return metadata;
163
+ }
164
+ };
165
+
166
+ static printRelations = (type, options) => {
167
+ if (Printer.options.relatedTypeSection !== true) {
168
+ return "";
169
+ }
170
+ return printRelations(type, options);
171
+ };
172
+
173
+ static printType = (name, type, options) => {
174
+ if (
175
+ typeof type === "undefined" ||
176
+ type === null ||
177
+ typeof name === "undefined" ||
178
+ name === null ||
179
+ hasDirective(type, Printer.options.skipDocDirective)
180
+ ) {
181
+ return undefined;
182
+ }
183
+
184
+ const printTypeOptions = {
185
+ ...DEFAULT_OPTIONS,
186
+ ...Printer.options,
187
+ ...options,
188
+ };
189
+
190
+ const header = Printer.printHeader(name, getTypeName(type), {
191
+ ...printTypeOptions,
192
+ header: options,
193
+ });
194
+ const description = Printer.printDescription(type);
195
+ const code = Printer.printCode(type);
196
+ const metadata = Printer.printTypeMetadata(type, printTypeOptions);
197
+ const relations = Printer.printRelations(type, printTypeOptions);
198
+
199
+ return [header, mdx, description, code, metadata, relations].join(
200
+ MARKDOWN_EOP,
201
+ );
202
+ };
203
+ }
204
+
205
+ module.exports = { Printer, DEFAULT_OPTIONS };
@@ -0,0 +1,82 @@
1
+ const {
2
+ graphql: {
3
+ isOperation,
4
+ getRelationOfReturn,
5
+ getRelationOfField,
6
+ getRelationOfImplementation,
7
+ },
8
+ } = require("@graphql-markdown/utils");
9
+
10
+ const { getRelationLink } = require("./link");
11
+ const {
12
+ ROOT_TYPE_LOCALE,
13
+ HEADER_SECTION_LEVEL,
14
+ MARKDOWN_EOP,
15
+ } = require("./const/strings");
16
+
17
+ const getRootTypeLocaleFromString = (text) => {
18
+ for (const [type, props] of Object.entries(ROOT_TYPE_LOCALE)) {
19
+ if (Object.values(props).includes(text)) {
20
+ return ROOT_TYPE_LOCALE[type];
21
+ }
22
+ }
23
+ return undefined;
24
+ };
25
+
26
+ const printRelationOf = (type, section, getRelation, options) => {
27
+ if (
28
+ typeof type === "undefined" ||
29
+ typeof getRelation !== "function" ||
30
+ typeof options === "undefined" ||
31
+ isOperation(type)
32
+ ) {
33
+ return "";
34
+ }
35
+
36
+ const relations = getRelation(type, options.schema);
37
+
38
+ if (typeof relations === "undefined") {
39
+ return "";
40
+ }
41
+
42
+ let data = [];
43
+ for (const [relation, types] of Object.entries(relations)) {
44
+ if (types.length === 0) {
45
+ continue;
46
+ }
47
+
48
+ const category = getRootTypeLocaleFromString(relation);
49
+ data = data.concat(types.map((t) => getRelationLink(category, t, options)));
50
+ }
51
+
52
+ if (data.length === 0) {
53
+ return "";
54
+ }
55
+
56
+ const content = [...data]
57
+ .sort((a, b) => a.localeCompare(b))
58
+ .join("<Bullet />");
59
+
60
+ return `${HEADER_SECTION_LEVEL} ${section}${MARKDOWN_EOP}${content}${MARKDOWN_EOP}`;
61
+ };
62
+
63
+ const printRelations = (type, options) => {
64
+ const relations = {
65
+ "Returned by": getRelationOfReturn,
66
+ "Member of": getRelationOfField,
67
+ "Implemented by": getRelationOfImplementation,
68
+ };
69
+
70
+ let data = "";
71
+ for (const [section, getRelation] of Object.entries(relations)) {
72
+ data += printRelationOf(type, section, getRelation, options);
73
+ }
74
+
75
+ return data;
76
+ };
77
+
78
+ module.exports = {
79
+ getRootTypeLocaleFromString,
80
+ printRelationOf,
81
+ printRelations,
82
+ };
package/src/section.js ADDED
@@ -0,0 +1,176 @@
1
+ const {
2
+ graphql: { isParametrizedField, hasDirective, isDeprecated },
3
+ } = require("@graphql-markdown/utils");
4
+ const { hasProperty } = require("@graphql-markdown/utils/src/object");
5
+
6
+ const {
7
+ HEADER_SECTION_LEVEL,
8
+ HEADER_SECTION_SUB_LEVEL,
9
+ HEADER_SECTION_ITEM_LEVEL,
10
+ MARKDOWN_EOL,
11
+ MARKDOWN_EOP,
12
+ } = require("./const/strings");
13
+ const { printDescription } = require("./common");
14
+ const { printBadges } = require("./badge");
15
+ const { printLink, printParentLink } = require("./link");
16
+ const { OPTION_DEPRECATED } = require("./const/options");
17
+
18
+ const sectionLevels = [
19
+ HEADER_SECTION_LEVEL,
20
+ HEADER_SECTION_SUB_LEVEL,
21
+ HEADER_SECTION_ITEM_LEVEL,
22
+ ];
23
+
24
+ const SHOW_DEPRECATED = `<><span className="deprecated">Show deprecated</span></>`;
25
+ const HIDE_DEPRECATED = `<><span className="deprecated">Hide deprecated</span></>`;
26
+
27
+ const printMetadataSection = (type, values, section, options) => {
28
+ switch (options.printDeprecated) {
29
+ case OPTION_DEPRECATED.GROUP: {
30
+ const { fields, deprecated } = values.reduce(
31
+ (res, arg) => {
32
+ isDeprecated(arg) ? res.deprecated.push(arg) : res.fields.push(arg);
33
+ return res;
34
+ },
35
+ { fields: [], deprecated: [] },
36
+ );
37
+
38
+ const meta = printSection(fields, section, {
39
+ ...options,
40
+ parentType: type.name,
41
+ });
42
+ const deprecatedMeta = printSection(deprecated, "", {
43
+ ...options,
44
+ parentType: type.name,
45
+ level: "",
46
+ collapsible: {
47
+ dataOpen: HIDE_DEPRECATED,
48
+ dataClose: SHOW_DEPRECATED,
49
+ },
50
+ });
51
+
52
+ return `${meta}${deprecatedMeta}`;
53
+ }
54
+
55
+ case OPTION_DEPRECATED.DEFAULT:
56
+ default:
57
+ return printSection(values, section, {
58
+ ...options,
59
+ parentType: type.name,
60
+ });
61
+ }
62
+ };
63
+
64
+ const printSection = (values, section, options) => {
65
+ const level =
66
+ hasProperty(options, "level") &&
67
+ typeof options.level !== "undefined" &&
68
+ options.level !== null
69
+ ? options.level
70
+ : HEADER_SECTION_LEVEL;
71
+
72
+ if (!Array.isArray(values) || values.length === 0) {
73
+ return "";
74
+ }
75
+
76
+ const levelPosition = sectionLevels.indexOf(level);
77
+
78
+ const [openSection, closeSection] = (() => {
79
+ if (
80
+ hasProperty(options, "collapsible") &&
81
+ hasProperty(options.collapsible, "dataOpen") &&
82
+ hasProperty(options.collapsible, "dataClose")
83
+ ) {
84
+ return [
85
+ `${MARKDOWN_EOP}<Details dataOpen={${options.collapsible.dataOpen}} dataClose={${options.collapsible.dataClose}}>${MARKDOWN_EOP}`,
86
+ `${MARKDOWN_EOP}</Details>${MARKDOWN_EOP}`,
87
+ ];
88
+ }
89
+ return [MARKDOWN_EOP, MARKDOWN_EOP];
90
+ })();
91
+
92
+ const items = printSectionItems(values, {
93
+ ...options,
94
+ collapsible: undefined, // do not propagate collapsible
95
+ level: levelPosition > -1 ? sectionLevels[levelPosition + 1] : undefined,
96
+ });
97
+
98
+ if (items === "") {
99
+ return ""; // do not print section is no items printed
100
+ }
101
+
102
+ return `${level} ${section}${openSection}${items}${closeSection}`;
103
+ };
104
+
105
+ const printSectionItems = (values, options) => {
106
+ const level =
107
+ hasProperty(options, "level") &&
108
+ typeof options.level !== "undefined" &&
109
+ options.level !== null
110
+ ? options.level
111
+ : HEADER_SECTION_SUB_LEVEL;
112
+
113
+ if (!Array.isArray(values) || values.length === 0) {
114
+ return "";
115
+ }
116
+
117
+ return values
118
+ .map(
119
+ (v) =>
120
+ v &&
121
+ printSectionItem(v, {
122
+ ...options,
123
+ level,
124
+ }),
125
+ )
126
+ .join(MARKDOWN_EOP);
127
+ };
128
+
129
+ const printSectionItem = (type, options) => {
130
+ const level =
131
+ hasProperty(options, "level") &&
132
+ typeof options.level !== "undefined" &&
133
+ options.level !== null
134
+ ? options.level
135
+ : HEADER_SECTION_SUB_LEVEL;
136
+
137
+ if (
138
+ typeof type === "undefined" ||
139
+ type === null ||
140
+ (hasProperty(options, "skipDocDirective") &&
141
+ hasDirective(type, options.skipDocDirective) === true)
142
+ ) {
143
+ return "";
144
+ }
145
+
146
+ const typeNameLink = printLink(type, { ...options, withAttributes: false });
147
+ const description = printDescription(type, "").replaceAll(
148
+ "\n",
149
+ `${MARKDOWN_EOL}> `,
150
+ );
151
+ const badges = printBadges(type, options);
152
+ const parentTypeLink = printParentLink(type, options);
153
+
154
+ let section = `${level} ${typeNameLink}${parentTypeLink} ${badges}${MARKDOWN_EOL}> ${description}${MARKDOWN_EOL}> `;
155
+ if (isParametrizedField(type)) {
156
+ section += printSectionItems(type.args, {
157
+ ...options,
158
+ level: HEADER_SECTION_ITEM_LEVEL,
159
+ parentType:
160
+ typeof options.parentType === "undefined"
161
+ ? type.name
162
+ : `${options.parentType}.${type.name}`,
163
+ });
164
+ }
165
+
166
+ return section;
167
+ };
168
+
169
+ module.exports = {
170
+ printSection,
171
+ printSectionItem,
172
+ printSectionItems,
173
+ printMetadataSection,
174
+ SHOW_DEPRECATED,
175
+ HIDE_DEPRECATED,
176
+ };